Codevs 24,857 Eve Festival Sol:
When the mean of a row is not an integer, it cannot be divided evenly.
Do a ring-sharing solitaire problem for the row and column separately.
AC CODE:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 100000 + 100; int read () {int X=0,f=1;char ch= "; while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch<= ' 9 ' &&ch>= ' 0 ') {x= (x<<3) + (x<<1) + (ch^ ' 0 '); Ch=getchar ();} return x*f;} typedef long Long Ll;int r[n],c[n];//r= the number of stalls interested in each row c= the number of stalls interested in each column ll sr,sc;//sr=r and Sc=c and LL sr[n],sc[n];//sr_i=sigma_{j=1}^ {i} R_j-aver;int main () {int n,m,t;n=read (); M=read (); T=read (); for (int i=1;i<=t;i++) {int x,y;x=read (); Y=read (); r[x]++,c[y]++;sr++;sc++; } bool Fr=1,fc=1; if (sr%n!=0) fr=0; if (sc%m!=0) fc=0; ll Resr=0,resc=0; if (FR) {ll aver=sr/n; for (int i=1;i<=n;i++) sr[i]=sr[i-1]+r[i]-aver; Sort (sr+1,sr+n+1); ll Mid=sr[n/2+1]; for (int i=1;i<=n;i++) resr+=abs (Sr[i]-mid); } if (FC) {ll avec=sc/m; for (int i=1;i<=m;i++) Sc[i]=sc[i-1]+c[i]-avec; Sort (sc+1, sc+m+1); ll Mid=sc[m/2+1]; for (int i=1;i<=m;i++) resc+=abs (Sc[i]-mid); } if ((!FR) && (!FC)) printf ("Impossible"); else if ((FR) && (!FC)) {printf ("row");p rintf ("%lld", RESR); } else if ((!FR) && (FC)) {printf ("column");p rintf ("%lld", Resc); } else if (FR&&FC) {printf ("both");p rintf ("%lld", Resr+resc); } return 0;}
Codevs 24,857 Eve Festival-greedy + median "ring-sharing solitaire problem"