BZOJ3032/COGS1045 [Nescafé 18] 七夕祭 【环形均分纸牌】
http://wulala.logdown.com/posts/208710-divide-the-problem-and-shared-the-ring-solitaire-solitaire-questions
其实是在书上看的,然而懒得打那么多字了= =
1 /* *********************************************** 2 Author :BPM136 3 Created Time :2018/7/15 15:02:24 4 File Name :3032.cpp 5 ************************************************ */ 6 7 #include<iostream> 8 #include<cstdio> 9 #include<algorithm> 10 #include<cstdlib> 11 #include<cmath> 12 #include<cstring> 13 #include<vector> 14 using namespace std; 15 16 typedef long long ll; 17 18 const int N = 200005; 19 20 int A[N]; 21 ll S[N]; 22 int n,m,k; 23 int X[N]; 24 int Y[N]; 25 26 ll calc(int num,int aver,int a[]) { 27 for(int i=1;i<=num;i++) A[i]=a[i]-aver; 28 for(int i=1;i<=num;i++) S[i]=S[i-1]+A[i]; 29 sort(S+1,S+num+1); 30 ll ret=0; 31 for(int i=1;i<=num;i++) ret+=abs(S[i]-S[(num+1)>>1]); 32 return ret; 33 } 34 35 int main() { 36 freopen("tanabata.in","r",stdin); 37 freopen("tanabata.out","w",stdout); 38 scanf("%d%d%d",&n,&m,&k); 39 for(int i=1;i<=k;i++) { 40 int x,y; 41 scanf("%d%d",&x,&y); 42 X[x]++; 43 Y[y]++; 44 } 45 ll ans_x=0,ans_y=0; 46 int flag_x=0,flag_y=0; 47 if(k%n==0) { 48 flag_x=1; 49 ans_x=calc(n,k/n,X); 50 } 51 if(k%m==0) { 52 flag_y=1; 53 ans_y=calc(m,k/m,Y); 54 } 55 56 if(flag_x && flag_y) { 57 cout<<"both"<<' '<<ans_x+ans_y<<endl; 58 return 0; 59 } 60 if(flag_x) { 61 cout<<"row"<<' '<<ans_x<<endl; 62 return 0; 63 } 64 if(flag_y) { 65 cout<<"column"<<' '<<ans_y<<endl; 66 return 0; 67 } 68 cout<<"impossible"<<endl; 69 return 0; 70 }