CodeForces - 839B Game of the Rows 贪心+思维
2017-08-13 09:24
489 查看
http://codeforces.com/contest/839/problem/B
题意:有k支军队和一架有n排座位的飞机,每排有8座。飞机上的邻座是这样定义的{1,2},{3,4},{4,5},{5,6},{7,8}。要求坐在邻座上的必须属于同一支军队,求是否坐得下。
题解:代码写得还很乱,先贴出来日后再改,先表达一下逻辑。贪心思想,中间四个座的约束是最多的,所以对于人数大于等于4或者等于3的军队优先安排在中间的四个座。然后剩下的还满足题意,就只有1个人和2个人的情况。数出来,1与2可以凑对优先安排在中间四座,剩下的安排在左右。还有要考虑一个2可以拆成两个1与其他军队的2安排在中间四座。这么个逻辑,昨天半夜脑子也有点混,有些因素没有考虑。早上爬起来补题,代码实现也有点杂乱。
代码:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-8;
int a
;
int main(){
int n,k,x,cnt1=0,cnt2=0,ans;
cin>>n>>k;
//l表示左右的座位,m表示中间四座
int l=2*n,m=n;
for(int i=1;i<=k;i++){
cin>>a[i];
while(a[i]>=8){
a[i]-=8;
if(m>=2){//优先安排中间四座
m-=2;
}
else if(m==1){
m--,l-=2;
}
else{
l-=4;
}
}
if(a[i]>=4){
a[i]-=4;
if(m>=1){//优先安排中间四座
m--;
}
else{
l-=2;
}
}
if(a[i]==3){
a[i]-=3;
if(m>=1){//优先安排中间四座
m--;
}
else{
l-=2;
}
}
}
for(int i=1;i<=k;i++){
if(a[i]==1){
cnt1++;
}
if(a[i]==2){
cnt2++;
}
}
int tt=min(cnt1,cnt2);
if(tt<=m){//1,2凑对
m-=tt;
cnt1-=tt;
cnt2-=tt;
if(cnt1>0){
l=l+2*m;
}
}
else{
cnt1-=m;
cnt2-=m;
m=0;
}
//坐左右两边
if(l>=cnt2){
l-=cnt2;
cnt2=0;
}
else{
cnt2-=l;
l=0;
}
if(cnt2>0){//拆2
cnt2-=m;
cnt1+=cnt2*2;
cnt2=0;
l+=m;
m=0;
}
//坐左右两边
if(l>=cnt1){
l-=cnt1;
cnt1=0;
}
else{
cnt1-=l;
l=0;
}
if(cnt1<=0&&cnt2<=0){
puts("YES");
}
else{
puts("NO");
}
return 0;
}
题意:有k支军队和一架有n排座位的飞机,每排有8座。飞机上的邻座是这样定义的{1,2},{3,4},{4,5},{5,6},{7,8}。要求坐在邻座上的必须属于同一支军队,求是否坐得下。
题解:代码写得还很乱,先贴出来日后再改,先表达一下逻辑。贪心思想,中间四个座的约束是最多的,所以对于人数大于等于4或者等于3的军队优先安排在中间的四个座。然后剩下的还满足题意,就只有1个人和2个人的情况。数出来,1与2可以凑对优先安排在中间四座,剩下的安排在左右。还有要考虑一个2可以拆成两个1与其他军队的2安排在中间四座。这么个逻辑,昨天半夜脑子也有点混,有些因素没有考虑。早上爬起来补题,代码实现也有点杂乱。
代码:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-8;
int a
;
int main(){
int n,k,x,cnt1=0,cnt2=0,ans;
cin>>n>>k;
//l表示左右的座位,m表示中间四座
int l=2*n,m=n;
for(int i=1;i<=k;i++){
cin>>a[i];
while(a[i]>=8){
a[i]-=8;
if(m>=2){//优先安排中间四座
m-=2;
}
else if(m==1){
m--,l-=2;
}
else{
l-=4;
}
}
if(a[i]>=4){
a[i]-=4;
if(m>=1){//优先安排中间四座
m--;
}
else{
l-=2;
}
}
if(a[i]==3){
a[i]-=3;
if(m>=1){//优先安排中间四座
m--;
}
else{
l-=2;
}
}
}
for(int i=1;i<=k;i++){
if(a[i]==1){
cnt1++;
}
if(a[i]==2){
cnt2++;
}
}
int tt=min(cnt1,cnt2);
if(tt<=m){//1,2凑对
m-=tt;
cnt1-=tt;
cnt2-=tt;
if(cnt1>0){
l=l+2*m;
}
}
else{
cnt1-=m;
cnt2-=m;
m=0;
}
//坐左右两边
if(l>=cnt2){
l-=cnt2;
cnt2=0;
}
else{
cnt2-=l;
l=0;
}
if(cnt2>0){//拆2
cnt2-=m;
cnt1+=cnt2*2;
cnt2=0;
l+=m;
m=0;
}
//坐左右两边
if(l>=cnt1){
l-=cnt1;
cnt1=0;
}
else{
cnt1-=l;
l=0;
}
if(cnt1<=0&&cnt2<=0){
puts("YES");
}
else{
puts("NO");
}
return 0;
}
相关文章推荐
- Codeforces 349 B Color the Fence (思维+贪心)
- Codeforces 500C:New Year Book Reading(思维+贪心?)
- CodeForces - 818B Permutation Game(贪心+思维)
- Codeforces 526C - Om Nom and Candies (贪心 + 思维)
- codeforces 232A Cycles 图论 贪心 思维
- CodeForces - 827B High Load(贪心+思维)
- codeforces-232【A贪心、思维、图】
- Codeforces 672C Recycling Bottles【极限思维+贪心枚举】
- CodeForces - 808C-贪心-思维
- codeforces-508C-Anya and Ghosts【贪心】【思维】
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- codeforces 500C New Year Book Reading (贪心+思维)
- Codeforces-540B-School Marks(思维贪心)
- 【CodeForces - 798C Mike and gcd problem】 思维+贪心
- CodeForces 721D. Maxim and Array(贪心,思维题)
- CodeForces 707B Bakery(思维+贪心)
- Codeforces 478C Table Decorations【贪心】【思维】
- Codeforces 918C - The Monster (贪心 + 思维 + 栈模拟)
- Codeforces 344C Rational Resistance【思维+贪心】
- 【CodeForces】---Party Lemonade(贪心&&思维&&递归)