Codevs 24,857 XI Matsuri-greedy + median "ring-sharing solitaire Problem"

Source: Internet
Author: User

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&AMP;&AMP;FC) {printf ("both");p rintf ("%lld", Resr+resc); } return 0;}

Codevs 24,857 Eve Festival-greedy + median "ring-sharing solitaire problem"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.