标签:val ... sage make statement rate and rom seq
Chef and Mover Problem Code: CHEFMOVR
Chef‘s dog Snuffles has so many things to play with! This time around, Snuffles has an array A containing N integers: A1, A2, ..., AN.
Bad news: Snuffles only loves to play with an array in which all the elements are equal.
Good news: We have a mover of size D!
A mover of size D is a tool which helps to change arrays. Chef can pick two existing elements Ai and Aj from the array, such that i + D = j and subtract 1 from one of these elements, and add 1 to the other element. In effect, a single operation of the mover, moves a value of 1 from one of the elements to the other.
Chef wants to find the minimum number of times she needs to use the mover of size D to make all the elements of the array A equal. Help her find this out.
Input: 3 5 2 1 6 5 0 3 3 1 0 3 0 4 2 3 4 3 5 Output: 5 2 -1
Testcase 1:
Here is a possible sequence of usages of the mover:
At the end, the array becomes (3, 3, 3, 3, 3), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 5.
Testcase 2:
Here is a possible sequence of usages of the mover:
At the end, the array becomes (1, 1, 1), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 2.
Testcase 3:
It is impossible to make all the elements equal. Hence the answer is -1.
——————————————————————————————————————
这道题我居然WA了两次QAQ
其实题意就是给你n个数和一个d 相邻距离为d的数字可以进行你+1我-1的操作 求最少的操作次数使得全部的数相等
所以我们一开始就要把所有的数加起来得到sum 如果sum%n!=0 那么肯定不存在这么一个答案
因为你无论怎么加减 总和都是不变的
然后再判断相邻d的所有数加起来是否为0 不如!=0 说明答案也不存在 这个也很好证明
最后统计答案的时候 我一开始想的是把所有的正数加起来 但是发现不行
比如这样一个例子 d=1时 4 1 1
按上面来算答案应该是2但是答案其实是3
因为你只能相邻交换 所以应该是4给第一个1 2 然后 第一个1给第二个1 1
答案是3
所以我们统计答案的时候可以像均分纸牌一样 每次拿出第一个数(<=k)
他只能从下一个数拿 而下一个数不可能从这个数再拿回来 所以他只能从下一个拿
这样把值推过去再慢慢统计就好辣
这样这个问题就解决辣
#include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; const int inf=0x3f3f3f3f,M=1e6+7; LL read(){ LL ans=0,f=1,c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘) f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){ans=ans*10+(c-‘0‘); c=getchar();} return ans*f; } LL T,n,k,v[M],sum,ans,x,h; int main() { T=read(); while(T--){ memset(v,0,sizeof(v)); sum=0; ans=0; n=read(); k=read(); for(int i=1;i<=n;i++) v[i]=read(),sum+=v[i]; if(sum%n!=0){printf("-1\n"); continue;} sum=sum/n; for(int i=1;i<=n;i++) v[i]-=sum; for(int i=1;i<=k;i++){ x=i; h=0; while(x<=n){ h+=v[x]; if(x+k>n) break; x+=k; } if(h){printf("-1\n"); break;} } if(h) continue; for(int i=1;i<=k;i++){ x=i; while(x<=n){ ans=ans+abs(v[x]); if(x+k>n) break; v[x+k]+=v[x]; x+=k; } } printf("%lld\n",ans); } return 0; }
codechef AUG17 T2 Chef and Mover
标签:val ... sage make statement rate and rom seq
原文地址:http://www.cnblogs.com/lyzuikeai/p/7287431.html