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 }
    贪心
  • 相关阅读:
    R语言 which() 、 which.min() 、 which.max() 函数
    R rep() 函数
    R语言 一个向量的值分派给另一个向量
    R语言 sample抽样函数
    超参数 hyperparameters
    随机游走模型(Random Walk)
    随机数
    Lambda 函数与表达式
    static
    变量的申明定义
  • 原文地址:https://www.cnblogs.com/itcsl/p/7355288.html
Copyright © 2011-2022 走看看