【思维+贪心】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】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath> using namespace std;
const int maxn=1e4+;
int a[maxn];
int n,k;
int main()
{
while(~scanf("%d%d",&n,&k))
{
int cnt=;
for(int i=;i<k;i++)
{
scanf("%d",&a[i]);
cnt+=a[i]/;
}
cnt=min(cnt,n);
int cpcnt=cnt;
cnt=n-cnt;
for(int i=;i<k;i++)
{
if(cpcnt==) break;
int tmp=a[i]/;
tmp=min(tmp,cpcnt);
a[i]-=tmp*;
cpcnt-=tmp;
}
// cnt=n-cnt;
//剩下cnt个4,2*n个2
int cnt2=;
for(int i=;i<k;i++)
{
cnt2+=a[i]/;
}
cnt2=min(cnt2,*n+cnt);
int x=;
for(int i=;i<k;i++)
{
if(cnt2==) break;
int tmp=a[i]/;
tmp=min(tmp,cnt2);
x+=tmp;
a[i]-=tmp*;
cnt2-=tmp;
}
int sum=;
for(int i=;i<k;i++)
{
sum+=a[i];
}
if(x<=*n)
{
int tot=*n-x;
tot+=cnt*;
if(sum<=tot)
{
puts("YES");
}
else
{
puts("NO");
}
}
else
{
int d=x-*n;
int tot=d+(cnt-d)*;
if(sum<=tot)
{
puts("YES");
}
else
{
puts("NO");
}
}
}
return ;
}
贪心
【思维+贪心】codeforces Game of the Rows的更多相关文章
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心
2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心 [Proble ...
- E. The Contest ( 简单DP || 思维 + 贪心)
传送门 题意: 有 n 个数 (1 ~ n) 分给了三个人 a, b, c: 其中 a 有 k1 个, b 有 k2 个, c 有 k3 个. 现在问最少需要多少操作,使得 a 中所有数 是 1 ~ ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)
https://codeforces.com/contest/1065 题意 给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复 思路 假如只有一个数 ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
随机推荐
- Suricata的规则解读(默认和自定义)
不多说,直接上干货! 见suricata官网 https://suricata.readthedocs.io/en/latest/rules/index.html 一.Suricata的规则所放位置 ...
- 【转】javap -c命令详解
javap -c命令详解 一直在学习Java,碰到了很多问题,碰到了很多关于i++和++i的难题,以及最经典的String str = "abc" 共创建了几个对象的疑难杂症. 知 ...
- [BZOJ3527][ZJOI2014]力 FFT+数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 首先卷积的形式是$h(i)=\sum_{i=0}^jf(i)g(i-j)$,如果我们 ...
- mysql单向自动同步
mysql自动同步 以下教程均使用mysql自带的自动同步功能 全库单向自动同步 本例把192.168.3.45上名称为ewater_main的数据库自动同步到192.168.3.68的ewater_ ...
- 开发原生安卓cordova插件(有原生界面)
上文开发的插件没有调用原生界面,本文介绍开发带有activity的插件 本文很多操作与上文重复,重复部分会省略 首先打开plug1,先开发插件的原生代码 在以下命名空间创建一个activity 名称为 ...
- pickle 两个使用小方法
def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data ...
- JavaScript-基础类型和运算符
JavaScript-基础类型和运算符 P02.稍微了解 1.js代码需要编写到script标签中 <script type="text/javascript"> 此处 ...
- 7-Java-C(小题答案)
1:58497 2:171700 3:145 4:i + j+2 == k+1 || i + k+2 == j+1 || k + j+2 == i+1 5:s + " " + (c ...
- environ - 用户环境(变量)
SYNOPSIS 总览 extern char **environ; DESCRIPTION 描述 变量 environ 指向的是一个叫 'environment'(环境)的字符串数组 (这个变量必须 ...
- css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style
css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style .con1{ width: 100p ...