思维训练
Uva11300 Spreading the Wealthe
【分类】思维(中位数),递推
题目描述
ACommunistregimeistryingtoredistributewealthinavillage. Theyhavehavedecidedtositeveryone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, suchthatintheend, everyonehasthesamenumberofcoins. Giventhenumberofcoinsofeachperson, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.
Input There is a number of inputs. Each input begins with n (n < 1000001), the number of people in the village. n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer. Output
Sample Input
3 100 100 100 4 1 2 5 4
Sample Output
0
4
最后每个人手中的金币数相同,都为M;
xi为收到了多少金币,-xi为给了多少金币
M=a[1]+x1-x2;
M=a[2]+x2-x3;
……
x1
x2=M-a[1]+x1
设c[]=M-a[]
c[i]=c[i-1]+M-a[i];//为前i个人的金币差值和
即问题转为一个有序序列,
找一个点使得他到所有点的距离和最小
奇数序列中,这个点即为中位数
偶数序列中,这个点即为中间两个点的任意值
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
const int N=1000010;
int n;
LL ans,a[N],c[N],M;
int main(){
freopen("Uva.in","r",stdin);
freopen("Uva.out","w",stdout);
while((scanf("%d",&n))==1&&n){
ans=0;
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(int i = 1; i<= n; i++)
scanf(AUTO,&a[i]),ans+=a[i];
M=ans/n;
for(int i = 1; i<= n; i++)
c[i]=c[i-1]+M-a[i];//前i个人金币的差值和
sort(c+1,c+n+1);
ans=0;
LL mid=c[(n+1)/2];//中位数
for(int i = 1; i<= n;i++)
ans+=abs(mid-c[i]);
printf(AUTO,ans);
putchar('\n');
}
return 0;
}
Uva11292
【贪心】
#include<iostream>
#include<cstdio>
#include<ctime>
#include<algorithm>
#include<queue>
using namespace std;
const int N=20010;
int n,A[N],B[N],m;
inline int readin(){
int res = 0;
char ch;
while((ch = getchar())>'9'||ch <'0');
res= ch-'0';
while((ch = getchar())>='0'&&ch <='9')
res= res* 10+ch -'0';
return res;
}
int a[N],b[N];
int main(){
freopen("Uva.in","r",stdin);
freopen("Uva.out","w",stdout);
while((scanf("%d%d",&n,&m))==2&&n&&m){
for(int i = 1; i <= n;i++)a[i]=readin();
for(int i = 1; i <= m;i++)b[i]=readin();
sort(a+1,a+n+1);
sort(b+1,b+m+1);
int j=1,ans=0;
for(int i = 1;i<=m;i++){
if(b[i]>=a[j])ans+=b[i],j++;
if(j==n+1)break;
}
if(j<n+1)printf("Loowater is doomed!\n");
else printf("%d\n",ans);
}
return 0;
}
Uva10881
题意:有很多只蚂蚁在一条直线上,每个蚂蚁移动速度都是1,并且有一个初始方向。并且当相邻两个蚂蚁相撞时转向。现在问t时间后各个蚂蚁的位置(按照读入的初始顺序输出)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10010;
struct ii{
int id;
int pos;
int p;
ii(int id=0,int pos=0,int p=0):id(id),pos(pos),p(p){}
bool operator <(const ii a)const{
return pos<a.pos;
}
}before[N],after[N];
int order[N],m;
int L,T,n,kase;
void init(){
memset(before,0,sizeof(before));
memset(after,0,sizeof(after));
scanf("%d%d%d",&L,&T,&n);
for(int i = 1; i <= n;i++){
int p;
char c;
scanf("%d %c",&p,&c);
int q = (c=='R')?1:-1;
before[i]=ii(i,p,q);
after[i]=ii(0,p+T*q,q);
}
}
int main(){
freopen("Uva.in","r",stdin);
freopen("Uva.out","w",stdout);
scanf("%d",&m);
for(int i = 1; i<=m;i++){
init();
kase++;
printf("Case #%d:\n",kase);
sort(before+1,before+n+1);
for(int i = 1; i<= n; i++)
order[before[i].id]=i;
sort(after+1,after+n+1);
for(int i = 1; i<= n;i++){//输入顺序
int a=order[i];//true postion
if(after[a].pos>L||after[a].pos<0)printf("Fell off\n");
else {printf("%d ",after[a].pos);
if(after[a].pos==after[a+1].pos||after[a].pos==after[a-1].pos)printf("Turning\n");
else{ char ch=after[a].p==1?'R':'L';
printf("%c\n",ch);}
}
}
printf("\n");
}
return 0;
}