B. Game of the Rows(贪心)

题意:

有k种颜色,每种有\(a_i\)个,将这k种颜色放在一个\(n * 8格子里\)

放置规则不能出现两个不同颜色在相邻的格子里,相邻的定义为在同一行

出现在(1,2),(3,4),(4,5),(5,6)或者(7,8) 这样都算相邻

\(1 <= n <= 10000\)

\(a_i <= 1000\)

$ \sum a_i <= 8 * n$

思路:

这道题贪心很不错,我是yy了很久了

贪心策略应该是让每种颜色尽量填满中间,再填满两边,

然后再把三个的放到中间,放不下就放到两边,

再把两个的放到中间,最后再填一个的

#include<bits/stdc++.h>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
const int N = 1e6 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
int a[1100];
void can(int &x,int &y,int d){
if(!x || y < d) return ;
int res = min(x, y / d);
x -= res;
y -= res * d;
}
int main(){ int n,k;
cin>>n>>k;
int cnt2 = 2 * n,cnt4 = n;
for(int i = 0;i < k;i++){
scanf("%d",a + i);
}
for(int i = 0;i < k;i++){ ///首先尽量填满中间,然后填满两边
can(cnt4,a[i],4);
can(cnt2,a[i],2);
if(a[i] && (!cnt2 && !cnt4)) {
puts("NO");
return 0;
}
}
int c[4] = {0};
for(int i = 0;i < k;i++) c[a[i]]++;
int res3 = min(cnt4,c[3]);///把 3的 尽量放中间
c[3] -= res3;
cnt4 -= res3;
if(2 * c[3] > cnt2){ ///剩下的3 只能放两边了
puts("NO");
return 0;
}
cnt2 -= 2 * c[3];
int res2 = min(cnt4,c[2]); ///将 2 个的 放在中间的 剩余一个位置可以放其他
c[2] -= res2;
cnt4 -= res2;
if(2 * c[2] + c[1] <= res2 + cnt4 * 2 + cnt2) puts("YES");///计算所有能放一个的不同位置的个数
else puts("NO");
return 0;
}

Codeforces Round #428 (Div. 2) B的更多相关文章

  1. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  2. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  3. CodeForces 839B - Game of the Rows | Codeforces Round #428 (Div. 2)

    血崩- - /* CodeForces 839B - Game of the Rows [ 贪心,分类讨论] | Codeforces Round #428 (Div. 2) 注意 2 7 2 2 2 ...

  4. Codeforces Round #428 (Div. 2) 题解

    题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...

  5. Codeforces Round #428 (Div. 2) D. Winter is here 容斥

    D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...

  6. Codeforces Round #428 (Div. 2)E. Mother of Dragons

    http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...

  7. 【Codeforces Round #428 (Div. 2) B】Game of the Rows

    [Link]:http://codeforces.com/contest/839/problem/B [Description] 给你n排的如题目所示的位置; 同一排中(1,2) 算相邻; (3,4) ...

  8. 【Codeforces Round #428 (Div. 2) C】Journey

    [Link]:http://codeforces.com/contest/839/problem/C [Description] 给一棵树,每当你到一个点x的时候,你进入x的另外一每一个出度的概率都是 ...

  9. Codeforces Round #428 (Div. 2)A,B,C

    A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  10. Codeforces Round #428 (Div. 2)

    终于上蓝名了,hahahahaha,虽然这场的 B 题因为脑抽了,少考虑一种情况终判错了,还是很可惜的.. B题本来过来1500个人,终判之后只剩下了200多个,真的有毒!!!! A - Arya a ...

随机推荐

  1. 提高篇(1):RMQ问题与ST表

    RMQ是英文Range Minimum/Maximum Query的缩写,是询问某个区间内的最值,这里讲一种解法:ST算法 ST算法通常用在要多次(10^6级别)询问区间最值的问题中,相比于线段树,它 ...

  2. Python3.6+pyinstaller+Django

    方案(一)Python3.6+pyinstaller+windows服务 一.Python3.6(64位)环境清单 Django==1.11.7 django-windows-tools==0.2 P ...

  3. 【jQuery】阶段(插入、复制、替换、删除)

    <p>你好!</p> 你最喜欢的水果是? <ul> <li title="苹果">苹果</li> <li titl ...

  4. 用php读取xml数据

    parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签. 读取数据.结束标签. 也就是说在对xml进行处理的时候每当遇到起始标签.数据和结束标签的时候函数会做相应的动作 ...

  5. Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/config/springdemo-config.xml]

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...

  6. linxu信号种类

    使用kill -l 命令,可看到linux支持的信号列表: 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGB ...

  7. 17-比赛1 B - 子串计算 Chef and his string challenge (string的运用)

    Chef's best friend Jerry gives Chef a string A and wants to know the number of string A that can be ...

  8. spring源码学习中的知识点

    一.循环依赖 循环依赖就是循环引用,就是两个或多个bean之间互相持有对方. 1.构造器循环依赖 表示通过构造器注入造成的循环依赖,此依赖是无法解决的,只能抛出BeanCurrentlyInCreat ...

  9. Eclipse 工作空间(Workspace)---Eclipse教程第07课

    Eclipse 工作空间(Workspace) eclipse 工作空间包含以下资源: 项目 文件 文件夹 项目启动时一般可以设置工作空间,你可以将其设置为默认工作空间,下次启动后无需再配置: 工作空 ...

  10. druid sqlparser使用例子

    package com.alibaba.druid.bvt.sql.mysql; import java.util.List; import org.junit.Assert; import com. ...