zoukankan      html  css  js  c++  java
  • [Poetize II]七夕祭

    描述 Description
      TYVJ七夕祭和11区的夏祭的形式很像。矩 形的祭典会场由N排M列共计N×M个摊点组成。虽然摊点种类繁多,不过cl只对其中的一部分摊点感兴趣,比如章鱼烧、苹果糖、棉花糖、射的屋……什么的。 Vani预先联系了七夕祭的负责人zhq,希望能够通过恰当地布置会场,使得各行中cl感兴趣的摊点数一样多,并且各列中cl感兴趣的摊点数也一样多。
        不 过zhq告诉Vani,摊点已经随意布置完毕了,如果想满足cl的要求,唯一的调整方式就是交换两个相邻的摊点。两个摊点相邻,当且仅当他们处在同一行或 者同一列的相邻位置上。由于zhq率领的TYVJ开发小组成功地扭曲了空间,每一行或每一列的第一个位置和最后一个位置也算作相邻。现在Vani想知道他 的两个要求最多能满足多少个。在此前提下,至少需要交换多少次摊点。
    题解:
    看了题目感觉好神,真的是第一题?
    然后就不会做了,膜拜题解,顿时醒悟:
     实际上就是环形均分纸牌,贪心,减去平均值后构造前缀和数列取中位数即可。想不出贪心,枚举起始点O(n^2)也能70分。

    好神!如果只有行,那么就是一个显然的环形均分纸牌,如果加上列的话,我们发现行列之间互不影响!所以在列上也做一遍就行了。
    orz 好题,怒赞!
    代码:
     1 #include<cstdio>
     2 
     3 #include<cstdlib>
     4 
     5 #include<cmath>
     6 
     7 #include<cstring>
     8 
     9 #include<algorithm>
    10 
    11 #include<iostream>
    12 
    13 #include<vector>
    14 
    15 #include<map>
    16 
    17 #include<set>
    18 
    19 #include<queue>
    20 
    21 #include<string>
    22 
    23 #define inf 1000000000
    24 
    25 #define maxn 150000
    26 
    27 #define maxm 500+100
    28 
    29 #define eps 1e-10
    30 
    31 #define ll long long
    32 
    33 #define pa pair<int,int>
    34 
    35 #define for0(i,n) for(int i=0;i<=(n);i++)
    36 
    37 #define for1(i,n) for(int i=1;i<=(n);i++)
    38 
    39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    40 
    41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    42 
    43 #define mod 1000000007
    44 
    45 using namespace std;
    46 
    47 inline int read()
    48 
    49 {
    50 
    51     int x=0,f=1;char ch=getchar();
    52 
    53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    54 
    55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    56 
    57     return x*f;
    58 
    59 }
    60 ll n,m,t,a[maxn],b[maxn],c[maxn],ans;
    61 
    62 int main()
    63 
    64 {
    65 
    66 
    67 
    68     n=read();m=read();t=read();
    69     for1(i,t)a[read()]++,b[read()]++;
    70     if(t%n==0)
    71     {
    72         ll tmp=t/n;
    73         c[1]=0;
    74         for2(i,2,n)c[i]=c[i-1]+a[i-1]-tmp;
    75         sort(c+1,c+n+1);
    76         ll x=c[n>>1];
    77         for1(i,n)ans+=abs(c[i]-x);
    78     }
    79     if(t%m==0)
    80     {
    81         ll tmp=t/m;
    82         c[1]=0;
    83         for2(i,2,m)c[i]=c[i-1]+b[i-1]-tmp;
    84         sort(c+1,c+m+1);
    85         ll x=c[m>>1];
    86         for1(i,m)ans+=abs(c[i]-x);
    87     }
    88     if(t%n==0)
    89     {
    90         if(t%m==0)printf("both %lld
    ",ans);else printf("row %lld
    ",ans);
    91     }
    92     else 
    93     {
    94         if(t%m==0)printf("column %lld
    ",ans);else printf("impossible
    ");
    95     }
    96 
    97     return 0;
    98 
    99 }
    View Code
    为毛一直WA了10分!
    发现我WA的原因是我中位数找错了:
    中位数应该是 a[(n+1)>>1]
    而我是这样的 a[n>>1]
    唉。。。
    代码:
      1 #include<cstdio>
      2 
      3 #include<cstdlib>
      4 
      5 #include<cmath>
      6 
      7 #include<cstring>
      8 
      9 #include<algorithm>
     10 
     11 #include<iostream>
     12 
     13 #include<vector>
     14 
     15 #include<map>
     16 
     17 #include<set>
     18 
     19 #include<queue>
     20 
     21 #include<string>
     22 
     23 #define inf 1000000000
     24 
     25 #define maxn 150000
     26 
     27 #define maxm 500+100
     28 
     29 #define eps 1e-10
     30 
     31 #define ll long long
     32 
     33 #define pa pair<int,int>
     34 
     35 #define for0(i,n) for(int i=0;i<=(n);i++)
     36 
     37 #define for1(i,n) for(int i=1;i<=(n);i++)
     38 
     39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
     40 
     41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
     42 
     43 #define mod 1000000007
     44 
     45 using namespace std;
     46 
     47 inline int read()
     48 
     49 {
     50 
     51     int x=0,f=1;char ch=getchar();
     52 
     53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     54 
     55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
     56 
     57     return x*f;
     58 
     59 }
     60 inline ll f(ll x){return x>0?x:-x;}
     61 int n,m;
     62 ll t,a[maxn],b[maxn],c[maxn],ans;
     63 
     64 int main()
     65 
     66 {
     67 
     68     freopen("input.txt","r",stdin);
     69 
     70     freopen("output.txt","w",stdout);
     71 
     72     n=read();m=read();t=read();
     73     for1(i,t)a[read()]++,b[read()]++;
     74     if(t%n==0)
     75     {
     76         ll tmp=t/n;
     77         c[1]=0;
     78         for2(i,2,n)c[i]=(ll)c[i-1]+a[i-1]-tmp;
     79         sort(c+1,c+n+1);
     80         ll x=c[n+1>>1];
     81         for1(i,n)ans+=f(c[i]-x);
     82     }
     83     if(t%m==0)
     84     {
     85         ll tmp=t/m;
     86         c[1]=0;
     87         for2(i,2,m)c[i]=(ll)c[i-1]+b[i-1]-tmp;
     88         sort(c+1,c+m+1);
     89         ll x=c[m+1>>1];
     90         for1(i,m)ans+=f(c[i]-x);
     91     }
     92     if(t%n==0)
     93     {
     94         if(t%m==0)printf("both %lld
    ",ans);else printf("row %lld
    ",ans);
     95     }
     96     else 
     97     {
     98         if(t%m==0)printf("column %lld
    ",ans);else printf("impossible
    ");
     99     }
    100 
    101     return 0;
    102 
    103 }
    View Code
  • 相关阅读:
    jQuery自定义插件
    jQuery基础入门学习
    jQuery事件机制
    css实现块级元素的水平居中的2种常用方法
    文本编辑的css常用属性
    js的事件的绑定
    js正则表达式(常用)
    汇编初入门debug实操
    JAVA新手笔记 Intent对象和Bundle对象
    OS X运行AFNI的AlphaSim提示libgomp.1.dylib找不到的解决办法
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4043802.html
Copyright © 2011-2022 走看看