守护 @ 2017-09-28 21:56:38
#include<stdio.h>
int a[10000],b[10000],z=100001,c,d,t;
int ans(int x,int y){
if(x<y)return y-x;
else return x-y;}
int main(){
int i,j=0,m,n;
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
scanf("%d",&a[i]);
for(j=1;j<=n;j++)
scanf("%d",&b[j]);
for(i=1;i<=n;i++){
z=100001;
for(j=1;j<=m;j++){
if(z>ans(a[j],b[i]))
z=ans(a[j],b[i]);}
t+=z;}
printf("%d",t);
return 0;}
by 守护 @ 2017-09-28 21:58:02
老师说要2分
by Tsukimaru @ 2017-09-28 21:58:40
233333
by 守护 @ 2017-09-28 21:58:58
@cutekibry 额
by Victorique @ 2017-09-28 21:59:44
能改进的可能不大,最好老老实实二分吧,又不是特别难的代码
by __世界第一弱__ @ 2017-09-28 22:00:38
by 守护 @ 2017-09-28 22:00:06
@吉尔伽美什 老师没教叫我自己看。。。。
by 守护 @ 2017-09-28 22:01:24
@世界第一弱 我说数据
by _KaQqi @ 2017-12-09 19:30:28
#include <stdio.h>
#include <stdlib.h>
void select_sort(long long* arr,int n) //选择排序,基于数组
{
for(int i = 0;i < n - 1;i++)
{
int position = i;
for(int j = i+1;j < n;j++)
{
if(arr[j] < arr[position])
{
position = j;
}
}
if(position == i)
{
continue;
}
long long middle = arr[position];
arr[position] = arr[i];
arr[i] = middle;
}
}
int absolute(int a)
{
if(a >= 0)
{
return a;
}
if(a < 0)
{
return -a;
}
return 0;
}
int find_min(const long long* arr,int len,int val) //查找范围,范围长度,待查找变量 (bug)
{
long long min = 99999999;
int middle = 0;
int low = 0;
int high = len - 1;
while(low <= high)
{
middle = (low + high)/2;
long long middle_val = arr[middle];
middle_val = middle_val - val;
middle_val = absolute(middle_val);
if(middle_val < min)
{
min = middle_val;
}
int last = middle - 1; //判断上一个的差值
long long last_val = arr[last];
last_val = last_val - val;
last_val = absolute(last_val);
int next = middle + 1;//判断下一个的差值
long long next_val = arr[next];
next_val = next_val - val;
next_val = absolute(next_val);
if(last_val < min) //如果上个差值比这个小
{
if(next_val < last_val)
{
min = next_val;
low = middle + 1;
continue;
}
min = last_val;
high = middle - 1;
continue;
}
if(next_val < min)
{
if(last_val < next_val)
{
min = last_val;
high = middle - 1;
continue;
}
min = next_val;
low = middle + 1;
continue;
}
/* int next = middle + 1;//判断下一个的差值
long long next_val = arr[next];
next_val = next_val - val;
next_val = absolute(next_val);
if(next_val < min)//如果下一个差值比这个小
{
min = next_val;
low = middle + 1;
continue;
}*/
return min; //返回最小差值
}
}
int main() //已测试,没问题
{
int m,n;
scanf("%d %d",&m,&n);
long long* dataSchool = (long long*)malloc(m*8);
for(int i = 0;i < m;i++)
{
scanf("%d",&dataSchool[i]);
}
select_sort(dataSchool,m);
long long count = 0;
for(int i = 0;i < n;i++)
{
long long now;
scanf("%d",&now);
count = count + find_min(dataSchool,m,now);
}
printf("%d",count);
return 0;
}
我也求救下...