【思维+贪心】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 ...
随机推荐
- [转]Android 完美退出 App (Exit)
本文转自:http://blog.csdn.net/zeus_9i/article/details/7259881 最近两天为了解决Android上面退出程序问题折腾了半死,在google & ...
- A8ERP管理系统(采购单管理)
花了一个星期的时间终于把采购模块完成了. 最近新开发的采购单管理,供大家参考学习,软件一步一步来.
- AJPFX总结OpenJDK 和 HashMap大量数据处理时,避免垃圾回收延迟的技巧二
HashMap简史 “Hash Code”这个概念第一次出现是在1953年1月的<Computing literature>中,H. P. Luhn (1896-1964) 在一篇 IB ...
- AJPFX关于java中可访问控制符和非访问控制符的详细总结
1.类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1 )公共类修饰符 pub ...
- HV000184: ParameterMessageInterpolator has been chosen, EL interpolation will not be supported问题解决
今天创建springboot项目的时候添加完依赖启动出现了这个错误 -- :: --- [ main] o.h.v.m.ParameterMessageInterpolator : HV000184: ...
- java 获取ip地址
1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...
- 在Apache服务器中禁用option
在apache禁止 http OPTIONS方法. apache disable http OPTIONS method 2013-04-17 09:27 4050人阅读 评论(1) 收藏 举报 分 ...
- JQuery 获得绝对,相对位置的坐标方法
获取页面某一元素的绝对X,Y坐标,可以用offset()方法:(body属性设置margin :0;padding:0;) var X = $('#DivID').offset().top; var ...
- largest rectangle in histogram leetcode
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【收藏】史上最全的浏览器 CSS & JS Hack 手册
浏览器渲染页面的方式各不相同,甚至同一浏览器的不同版本(“杰出代表”是 IE)也有差异.因此,浏览器兼容成为前端开发人员的必备技能.如果有一份浏览器 Hack 手册,那查询起来就方便多了.这篇文章就向 ...