zoukankan      html  css  js  c++  java
  • cf C. Dominoes

    http://codeforces.com/contest/394/problem/C

    题意:有n*m个骨牌,每个骨牌上有四种样式(“01,10,11,00”),让你重新排列成一个N*M的矩阵,使2*m列的每一列和的最大值最小。

    思路:先排序,先正着排全是‘11’的牌,如果在一行,没有排满,用剩下的牌补齐,补齐之后,剩下的每一行都倒着排牌就可以了;如果上面的牌是'10',那么下面的牌是‘01’,上面的牌是‘01‘,那么下面的牌是’10‘;

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #define ll long long
      5 #define maxn 1000100
      6 using namespace std;
      7 
      8 int n,m;
      9 struct node
     10 {
     11     int x,y;
     12     bool operator <(const node &a)const
     13     {
     14         return (x+y)>(a.x+a.y);
     15     }
     16 } p[maxn],q[maxn];
     17 char str[maxn];
     18 int g[1001][1001];
     19 
     20 int main()
     21 {
     22     while(scanf("%d%d",&n,&m)!=EOF)
     23     {
     24         int cnt=0;
     25         getchar();
     26         for(int i=1; i<=n; i++)
     27         {
     28             gets(str);
     29             int k=strlen(str);
     30             for(int j=0; j<k;)
     31             {
     32                 if(str[j]!=' ')
     33                 {
     34                     p[cnt].x=str[j]-'0';
     35                     p[cnt++].y=str[j+1]-'0';
     36                     j+=2;
     37                 }
     38                 else
     39                     j++;
     40             }
     41         }
     42         sort(p,p+cnt);
     43         int x=0,t1=0;
     44         bool flag=false;
     45         for(int i=1; i<=n; i++)
     46         {
     47             if(!flag)
     48             {
     49                 for(int j=0; j<m; j++)
     50                 {
     51                     if(p[x].x==1&&p[x].y==1)
     52                     {
     53                         q[t1].x=1;
     54                         g[i][j]=t1;
     55                         q[t1++].y=1;
     56                         x++;
     57                     }
     58                     else
     59                     {
     60                         flag=true;
     61                         for(int k=j; k<m; k++)
     62                         {
     63                             q[t1].x=p[x].x;
     64                             g[i][k]=t1;
     65                             q[t1++].y=p[x].y;
     66                             x++;
     67                         }
     68                         break;
     69                     }
     70                 }
     71             }
     72             else
     73             {
     74                 for(int j=m-1; j>=0; j--)
     75                 {
     76                     int xx=q[g[i-1][j]].x;
     77                     int yy=q[g[i-1][j]].y;
     78                     if(xx==1&&yy==0)
     79                     {
     80                         if((p[x].x==1&&p[x].y==0)||(p[x].x==0&&p[x].y==1))
     81                         {
     82                             q[t1].x=0;
     83                             g[i][j]=t1;
     84                             q[t1++].y=1;
     85                             x++;
     86                         }
     87                         else
     88                         {
     89                             q[t1].x=p[x].x;
     90                             g[i][j]=t1;
     91                             q[t1++].y=p[x].y;
     92                             x++;
     93                         }
     94                     }
     95                     else if(xx==0&&yy==1)
     96                     {
     97                         if((p[x].x==1&&p[x].y==0)||(p[x].x==0&&p[x].y==1))
     98                         {
     99                             q[t1].x=1;
    100                             g[i][j]=t1;
    101                             q[t1++].y=0;
    102                             x++;
    103                         }
    104                         else
    105                         {
    106                             q[t1].x=p[x].x;
    107                             g[i][j]=t1;
    108                             q[t1++].y=p[x].y;
    109                             x++;
    110                         }
    111                     }
    112                     else
    113                     {
    114                         q[t1].x=p[x].x;
    115                         g[i][j]=t1;
    116                         q[t1++].y=p[x].y;
    117                         x++;
    118                     }
    119                 }
    120             }
    121         }
    122         for(int i=1; i<=n; i++)
    123         {
    124             for(int j=0; j<m; j++)
    125             {
    126                 printf("%d%d ",q[g[i][j]].x,q[g[i][j]].y);
    127             }
    128             printf("
    ");
    129         }
    130     }
    131     return 0;
    132 }
    View Code
  • 相关阅读:
    Topics
    telnetlib-telnet客户端操作
    logging-日志信息管理
    B.2 工具spy++
    B.1 XPath 获取技巧
    pyinstaller-将Python程序打包成一个独立可执行软件包
    探讨HTTP中敏感数据的安全性传输方案
    shell->一个经典的shell脚本结构
    c->再次封装已有函数的快速方法
    c->推荐的更安全的代码写法
  • 原文地址:https://www.cnblogs.com/fanminghui/p/4252059.html
Copyright © 2011-2022 走看看