题意

https://vjudge.net/problem/CodeForces-1265D

a个0,b个1,c个2,d个3,问是否存在一种排列方案使得任意相邻两数之差==1

思路

分类讨论太麻烦了,直接暴力搞!

枚举0123每个数作为第一个数,然后优先看这个数-1还有没有,有的话就放进去,没有就看这个数+1,如果两个都没有了,那就break,最后判断0123的数量是否都为0即可。

为什么要先放x-1?这是一种贪心的思路,放了x+1的话那么x-1在后面可能就放不了了,所以优先放x-1。

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll p[N],q[N];
vector<int> ans;
int main()
{
std::ios::sync_with_stdio(false);
cin>>p[0]>>p[1]>>p[2]>>p[3];
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
q[j]=p[j];
int x=i;
if(!q[x]) continue;
ans.clear();
q[x]--;
ans.push_back(x);
while(1)
{
if(x&&q[x-1])
--x,q[x]--,ans.push_back(x);
else if(x!=3&&q[x+1])
++x,q[x]--,ans.push_back(x);
else
break;
}
if(!q[0]&&!q[1]&&!q[2]&&!q[3])
{
cout<<"YES"<<endl;
for(int it:ans)
{
cout<<it<<" ";
}
cout<<endl;
return 0;
}
}
cout<<"NO"<<endl;
return 0;
}

  

CodeForces - 1265D(贪心+暴力)的更多相关文章

  1. Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力

    A 水题 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace ...

  2. The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)

    传送门 •题意 二维平面上有 n 条线段,每条线段坐标为 $(l_i,i),(r_i,i)$: 平面上的每个整点坐标上都可以放置一枚硬币,但是要求任意两枚硬币的横坐标不相同: 问最多有多少条线段可以放 ...

  3. CodeForces 712C Memory and De-Evolution (贪心+暴力)

    题意:现在有一个长度为 x 的正三角形,每次可以把一条边减小,然后用最少的时间变成长度为 y 的正三角形. 析:一开始,正着想,然后有一个问题,就是第一次减小多少才能最快呢?这个好像并不好确定,然后我 ...

  4. CodeForces - 1250B The Feast and the Bus (贪心+暴力)

    题意 https://vjudge.net/problem/CodeForces-1250B 每个人属于队伍ai,汽车一次至多载两只队伍(全员),费用为车的容量*载人次数,问最少花费. 思路 k(队伍 ...

  5. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  6. Codeforces 734C [水][暴力][贪心]

    题意: 要生产n个物品,每个花费时间为x. 有两种魔法,每种最多使用1个. 其中第一种魔法可以使每个物品生产的花费时间变为ai,相应的花费是bi;第二种魔法可以减少ci个物品,相应的花费是di,并且保 ...

  7. Karen and Game CodeForces - 816C (暴力+构造)

    On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...

  8. CodeForces - 589B(暴力+排序)

    Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n ...

  9. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

随机推荐

  1. HDU5470 Typewriter (SAM+单调队列优化DP)

    Typewriter Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  2. CodeForces-999A-Mishka and Contest

    Mishka started participating in a programming contest. There are nn problems in the contest. Mishka' ...

  3. 纯手工搭建K8s(单节点)

    准备说明: 因为为纯手动搭建,所以针对安装时需要的一些安装包需提前下载好 cfssl_linux-amd64. cfssljson_linux-amd64. cfssl-certinfo_linux- ...

  4. 浅析Java常量池

    Java常量池 Java常量池其实分为两种:静态常量池和运行时常量池 1.静态常量池 所谓静态常量池,即*.class文件中的常量池,class文件中的常量池不仅仅包含字符串(数字)字面量,还包含类. ...

  5. LiteByte教程

    转载请标明原文地址:https://www.cnblogs.com/zhangyukof/p/12073041.html 简介 LiteByte是一种轻量级的二进制数据交换格式.体积小巧.简单易用是设 ...

  6. 由malloc和new引发的段错误

    class Queue{ private: struct node{ string data; struct node * next,*priv; } private: struct node * p ...

  7. DOM事件流的三个阶段

    事件发生时会在元素节点之间按照特定的顺序传播,这个传播过程即DOM事件流. DOM事件流分为三个阶段,分别为: 捕获阶段:事件从Document节点自上而下向目标节点传播的阶段: 目标阶段:真正的目标 ...

  8. Elasticsearch(GEO)数据写入和空间检索

    Elasticsearch简介 什么是 Elasticsearch? Elasticsearch 是一个开源的分布式 RESTful搜索和分析引擎,能够解决越来越多不同的应用场景. 本文内容 本文主要 ...

  9. Information Management System

    Information Management System 一.代码部分 #include <stdio.h> #include <stdlib.h> #include < ...

  10. jQuery实现回车触发登录按钮的功能

    jQuery实现回车触发登录按钮的功能,代码如下: $('body').keyup(function(e){ if(e.keyCode===13){ $('.login').click() } }) ...