zoukankan      html  css  js  c++  java
  • 【思维+贪心】codeforces Game of the Rows

    http://codeforces.com/contest/839/problem/B

    【题意】

    • 给定n组人,告诉每组人的人数,这些人要在飞机上坐座位
    • 飞机上座位的分布看图可以知道,12  3456  78
    • 要求任意两个相邻座位不能是不同组的人
    • 问在满足约束的情况下能不能保证所有的人有座位

    【思路】

    • 首先先占用中间四连坐的,这时剩下cnt个四连坐的和2*n个两连坐的
    • 然后先满足剩下的a[i]中一对一对的,这时要优先选用两连坐的
    • 那么最后剩下的是一个一个单个坐的,这时cnt个四连坐的有x个坐了一对,那么这x个只能做x个1,剩下了cnt-x个四连坐的,那么可以坐2*(cnt-x)个

    【AC】

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<algorithm>
     6 #include<cmath>
     7 
     8 using namespace std;
     9 const int maxn=1e4+2;
    10 int a[maxn];
    11 int n,k;
    12 int main()
    13 {
    14     while(~scanf("%d%d",&n,&k))
    15     {
    16         int cnt=0;
    17         for(int i=0;i<k;i++)
    18         {
    19             scanf("%d",&a[i]);
    20             cnt+=a[i]/4;
    21         }
    22         cnt=min(cnt,n);
    23         int cpcnt=cnt;
    24         cnt=n-cnt;
    25         for(int i=0;i<k;i++)
    26         {
    27             if(cpcnt==0) break;
    28             int tmp=a[i]/4;
    29             tmp=min(tmp,cpcnt);
    30             a[i]-=tmp*4;
    31             cpcnt-=tmp;
    32         }
    33 //        cnt=n-cnt;
    34         //剩下cnt个4,2*n个2
    35         int cnt2=0;
    36         for(int i=0;i<k;i++)
    37         {
    38             cnt2+=a[i]/2;
    39         } 
    40         cnt2=min(cnt2,2*n+cnt);
    41         int x=0;
    42         for(int i=0;i<k;i++)
    43         {
    44             if(cnt2==0) break;
    45             int tmp=a[i]/2;
    46             tmp=min(tmp,cnt2);
    47             x+=tmp;
    48             a[i]-=tmp*2;
    49             cnt2-=tmp;
    50         }
    51         int sum=0;
    52         for(int i=0;i<k;i++)
    53         {
    54             sum+=a[i];
    55         }
    56         if(x<=2*n)
    57         {
    58             int tot=2*n-x;
    59             tot+=cnt*2;
    60             if(sum<=tot)
    61             {
    62                 puts("YES");
    63             }
    64             else
    65             {
    66                 puts("NO");
    67             }
    68         }
    69         else
    70         {
    71             int d=x-2*n;
    72             int tot=d+(cnt-d)*2;
    73             if(sum<=tot)
    74             {
    75                 puts("YES");
    76             }
    77             else
    78             {
    79                 puts("NO");
    80             }
    81         }
    82     }
    83     return 0;    
    84 }
    贪心
  • 相关阅读:
    linux下文件的复制、移动与删除
    Hbase万亿级存储性能优化总结-配置
    hbase 读写和优化
    hive数据倾斜定位及处理
    flink初识及安装flink standalone集群
    【Linux】用less查看日志文件
    sqoop的详细使用及原理
    HBase删除数据的原理
    hbase数据加盐(Salting)存储与协处理器查询数据的方法
    Hbase内存磁盘大致关系
  • 原文地址:https://www.cnblogs.com/itcsl/p/7355288.html
Copyright © 2011-2022 走看看