97 条题解
-
-1yaosir LV 6 @ 2017-03-05 21:53:59
输油管道的位置可能是多个,但输油管道总长固定。油井数目为奇数时,管道一定得建在最中间的那个上;数目为偶数时,管道可以建在最中间两个油井之间的任何位置,包括两个端点。
所以最后,油井位置按y值排序后,管道建在n/2的位置上,一定是最端管道和。求和即可。
java:import java.util.*;
import java.io.*;
public class game {
public static void main(String[]args)throws IOException{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int []x=new int[n];
int[]y=new int[n];
for(int i=0;i<n;i++)
{
x[i]=input.nextInt();
y[i]=input.nextInt();
}
Arrays.sort(y);
int sum=0;
for(int i=0;i<n;i++)
sum+=Math.abs(y[i]-y[n/2]);
System.out.println(sum);
}
} -
-12017-02-22 20:48:49@
#include <iostream> #include <stdio.h> #include <math.h> //快排+找中位数 using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int oil[10009]; int y[10009]; void sort(int lb,int ub){ int m=lb; if(lb>=ub){ return; } for(int i = lb+1;i<=ub;i++){ if(y[i]>=y[lb]){ continue; }else if(y[i]<y[lb]){ m++; swap(y[i],y[m]); } } swap(y[lb],y[m]); sort(lb,m-1); sort(m+1,ub); } int main(int argc, char** argv) { int n,temp,place,total=0; cin>>n; for(int i = 1 ; i<=n;i++){ scanf("%d",&temp); scanf("%d",&y[i]); } sort(1,n); place = y[(n-1)/2 + 1]; for(int i = 1 ; i<=n;i++){ total+=abs(y[i]-place); } cout<<total<<endl; return 0; }
-
-12017-02-22 20:48:17@
找中位数
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop /
int y[10009];
void sort(int lb,int ub){
int m=lb;
if(lb>=ub){
return;
}
for(int i = lb+1;i<=ub;i++){
if(y[i]>=y[lb]){
continue;
}else if(y[i]<y[lb]){
m++;
swap(y[i],y[m]);
}
}
swap(y[lb],y[m]);
sort(lb,m-1);
sort(m+1,ub);
}
int main(int argc, char* argv) {
int n,temp,place,total=0;
cin>>n;
for(int i = 1 ; i<=n;i++){
scanf("%d",&temp);
scanf("%d",&y[i]);
}
sort(1,n);
place = y[(n-1)/2 + 1];
for(int i = 1 ; i<=n;i++){
total+=abs(y[i]-place);
}
cout<<total<<endl;
return 0;
} -
-12017-02-19 13:36:34@
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
struct place{
int x,y;
friend bool operator < (place a,place b){
if(a.y>b.y)
return true;
else
return false;
}
friend bool operator > (place a,place b){
if(a.y<b.y)
return true;
else
return false;
}
};
int main ()
{
int n,i,count1=0,count2=0;
place p[10000]={0,0};
cin>>n;
for(i=0;i<n;i++)
cin>>p[i].x>>p[i].y;
sort(p,p+n);
for(i=0;i<n;i++)
count1+=abs(p[i].y-p[(n+1)/2].y);
for(i=0;i<n;i++)
count2+=abs(p[i].y-p[n/2].y);
if(count1>count2)
cout<<count2;
else
cout<<count1;
return 0;
} -
-12016-11-21 05:05:30@
#include <iostream> #include <cmath> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <climits> #include <algorithm> #include <set> using namespace std; int main() { int n; cin>>n; int temp=0; int sum=0; int meiyong; int b[10005]; for(int i=1;i<=n;i++){cin>>meiyong>>b[i];} sort(b+1,b+1+n); temp=b[n/2+1]; for(int i=1;i<=n;i++){sum=sum+abs(b[i]-temp);} cout<<sum; return 0; }
-
-12016-11-11 00:18:36@
用穷举做出来了...
-
-12016-10-19 14:06:28@
#include<cstdio>
#include<algorithm>
#include <iostream>
using namespace std;
int a[10005];
int main(void)
{
int n;
cin>>n;
int x;
for(int i=0;i<n;i++) scanf("%d%d",&x,&a[i]);
sort(a,a+n);
int k=n/2,ans=0;
for(int i=0;i<n;i++) ans+=abs(a[k]-a[i]);
cout<<ans<<endl;
return 0;
} -
-12016-10-05 10:26:02@
小学数学啊
var
i,j,n,x:longint;
a:array[1..10000000] of longint;
begin
readln(n);
for i:=1 to n do readln(x,a[i]);
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]<a[j] then
begin
x:=a[i];
a[i]:=a[j];
a[j]:=x;
end;
x:=0;
for i:=1 to n do
inc(x,abs(a[i]-a[(n+1)div 2]));//位置在中间那个点
writeln(x);
end. -
-12016-09-30 21:52:16@
刚开始还以为要线性回归方程做 不过后来看见是 东西向的管道···
-
-12016-08-28 18:49:41@
#include <iostream>
#include <cstring>using namespace std;
void ksort(int num[],int l,int r)
{
int mid=num[(l+r)/2];
int i=l,j=r,temp=0;
do
{
while(num[i]<mid)++i;
while(num[j]>mid)--j;
if(i<=j)
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
++i;
--j;
}
}while(i<=j);
if(l<j)ksort(num,l,j);
if(i<r)ksort(num,i,r);
return;
}int main()
{
int n;
cin>>n;
int num[10001];
memset(num,0,sizeof(num));
int i,j;
for(i=1;i<=n;++i)
{
cin>>j>>j;
num[i]=j;
}
ksort(num,1,n);
int mid=0;
if(n%2==0)
mid=(num[n/2]+num[n/2+1])/2;
else mid=num[n/2+1];
int ans2=0;
for(i=1;i<=n;++i)
{
if( num[i] < mid)ans2+=( mid - num[i]);
else ans2+=(num[i] - mid);
}
cout<<ans2;
//system("pause");
return 0;
} -
-12016-08-18 19:06:11@
var
y,ans:array[1..10000] of longint;
n,i,j,min:longint;
begin
readln(n);
for i:=1 to n do readln(y[i+1],y[i]);
for i:=1 to n do
for j:=1 to n do begin
ans[i]:=ans[i]+abs(y[i]-y[j]);
end;
min:=maxlongint;
for i:=1 to n do if ans[i]<min then min:=ans[i];
writeln(min);
end. -
-12016-05-01 09:29:39@
不要相信他的文件读写。。。根本不需要。。。害得我WA好几次。。。
-
-12016-03-29 18:36:50@
难啊,难得让我欣慰啊……
测试数据 #0: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 844 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 844 KiB, score = 10
测试数据 #9: Accepted, time = 15 ms, mem = 836 KiB, score = 10
测试数据 #10: Accepted, time = 0 ms, mem = 840 KiB, score = 10
Accepted, time = 15 ms, mem = 844 KiB, score = 110 -
-12015-04-26 12:50:17@
测试数据 #0: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 780 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 784 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 784 KiB, score = 10
测试数据 #10: Accepted, time = 15 ms, mem = 784 KiB, score = 10
Accepted, time = 30 ms, mem = 784 KiB, score = 110
短时间AC,新手第二题 -
-12014-07-13 15:13:25@
#include<stdio.h>
int a[10010],b[10010];
void zhao(int l,int n)
{
int i=l,j=n,mid,t;
mid=a[(l+n)/2];
while(i<=j)
{
while(a[i]<mid)
i++;
while(a[j]>mid)
j--;
if(i<=j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
i++;
j--;
}
}
if(l<j) zhao(l,j);
if(i<n) zhao(i,n);
}
int jue(int i,int j)
{
int sum;
sum=a[i]-a[j];
if(sum<0)
sum=-sum;
return sum;
}
int main ()
{
int n,i,x,p,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d %d",&x,&a[i]);
zhao(1,n);
for(i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
for(j=1;j<=n;j++)
{
b[i]=b[i]+jue(j,i);
}
}
p=b[1];
for(i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
if(p>b[i])
p=b[i];
}
printf("%d",p);
return 0;
}
不懂为啥第二个错啦、、、
快排没超时~~yeah -
-12009-11-05 13:39:40@
此题代码之长
十分之难~~~~~~~
所涵盖信息学内容之广甚至用到高阶数论知识~~~
确认已经超越 A+B Problem 建议新手不要轻易尝试~~~~~ -
-22014-03-16 13:40:59@
我去……交了四次还没过