【思维+贪心】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 ...
随机推荐
- Spring Boot学到的内容
Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- SpringMvc之参数绑定注解详解
引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/j ...
- webapi之fiddler头设置
Host: localhost:16648Connection: keep-aliveContent-Length: 36Accept: application/json, text/javascri ...
- UI常用字体定义和继承的实例,ResearchKitCode
#import <UIKit/UIKit.h> @interface UIFont (APCAppearance) + (UIFont*) appRegularFontWithSize: ...
- Long time no blogging
It is a long time before I posted the last blog on myspace and seems that all of my blogs/documents ...
- sqlserver2012 offset
/* * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public Lic ...
- CSS3实现单行、多行文本溢出(省略号的形式出现)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- (转)使用Spring配置文件实现AOP
http://blog.csdn.net/yerenyuan_pku/article/details/52880558 使用Spring配置文件实现AOP 前面我们已经学会了使用Spring的注解方式 ...
- Three.js模型隐藏或显示
材质属性.visible查看Three.js文档的基类Material,可以知道材质属性.visible的作用就是控制绑定该材质的模型对象是否可见,默认值是true,LineBasicMaterial ...