Codeforces 401C Team 贪心法
本题使用贪心法。关键是考贪心策略。同一时候要求要细心,我提交的时候也WA了几次。大意题目就是怎样依照给定的规则排列一个01字符串,引用原题例如以下:
C. Team
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Now it’s time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They’ve been best friends ever since primary school and hopefully, that can somehow help them in teamwork.
For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that:
there wouldn’t be a pair of any side-adjacent cards with zeroes in a row;
there wouldn’t be a group of three consecutive cards containing numbers one.
Today Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.
Input
The first line contains two integers: n (1 ≤ n ≤ 106) — the number of cards containing number 0; m (1 ≤ m ≤ 106) — the number of cards containing number 1.
Output
In a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1.
Sample test(s)
input
1 2
output
101
input
4 8
output
110110110101
input
4 10
output
11011011011011
input
1 5
output
-1
有几个坑,代码凝视的地方;推断条件和贪心策略能够依据题意多举例。然后总结出来:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
using std::string;
int main()
{
int zero, one;
scanf("%d %d", &zero, &one);
if (zero > one + 1 ||one > (zero << 1) + 2)///推断无解条件
{
printf("-1\n");
}
else
{
string str;
while (zero || one)
{
if (one + 1 == zero)
{
str.insert(str.end(), '0');
zero--;
}
else if ((zero << 1) + 1 <= one)///不要写成(zero<<1)+2 == one
{
if (one > 1)/// need to judge first, insure we have enough one
{
str += "11";
one -= 2;
}
else
{
str += "1";
one--;
}
}
else if (str.empty() || (str.back() == '1' && zero > 0))
{
str.insert(str.end(), '0');
zero--;
}
else
{
str.insert(str.end(), '1');
one--;
}
}
printf("%s\n", str.c_str());
}
return 0;
}
Codeforces 401C Team 贪心法的更多相关文章
- CodeForces - 401C Team(简单构造)
题意:要求构造一个字符串,要求不能有连续的两个0在一起,也不能有连续的三个1在一起. 分析: 1.假设有4个0,最多能构造的长度为11011011011011,即10个1,因此若m > (n + ...
- 贪心法 codevs 1052 地鼠游戏
1052 地鼠游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 王钢是一名学习成绩优异的学生,在平 ...
- codeforces 932E Team Work(组合数学、dp)
codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...
- Codeforces 932E Team work 【组合计数+斯特林数】
Codeforces 932E Team work You have a team of N people. For a particular task, you can pick any non-e ...
- LeetCode刷题总结-二分查找和贪心法篇
本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...
- C 编译器的“贪心法”
C语言中有单字符符号和多字符符号之分,那么,当C编译器读入一个字符‘/’后又跟了一个字符‘*’,那么编译器就必须做出判断:是将其作为两个分别的符号对待,还是合起来作为一个符号对待.C语言对这个问题的解 ...
- Codeforces Round #249 (Div. 2)B(贪心法)
B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 410C.Team[构造]
C. Team time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- codeforces 757F Team Rocket Rises Again
链接:http://codeforces.com/problemset/problem/757/F 正解:灭绝树. mdzz倍增lca的根节点深度必须是1..我因为这个错误调了好久. 我们考虑先求最短 ...
随机推荐
- 探究rh6上mysql5.6的主从、半同步、GTID多线程、SSL认证主从复制
http://407711169.blog.51cto.com/6616996/1203973/
- C# 读取EXCEL文件出现数据丢失问题,数据不完整
将EXCEL数据导出的时候如果同一列数据中既有文字,又有数字!读取时一列中要么文字丢失只剩下数字,要么数字丢失,只剩下文字,这是由第一行的数据类型决定的.出现这种问题是由于数据类型不统一造成的. 原来 ...
- QMsgPack简介
QMsgPack简介 首先,关于MessagePack协议,访问http://msgpack.org可以了解详细的格式约定及各种语言的实现. MessagePack协议号称比JSON快,但速度的快慢这 ...
- gitignore不起作用解决的方法
前面有文章介绍了使用gitignore文件的方法,该文件表示过滤规则,可是对已经增加版本号库的文件不能生效,因此须要利用命令将想要忽略的文件从版本号库中删除,比方说.我们对androidproject ...
- [Hive]使用HDFS文件夹数据创建Hive表分区
描写叙述: Hive表pms.cross_sale_path建立以日期作为分区,将hdfs文件夹/user/pms/workspace/ouyangyewei/testUsertrack/job1Ou ...
- Mac配置PHP开发环境
安装环境如下: Mac OS 10.10.1 Apache 2.4.9 PHP 5.5.14 MySQL 5.6.22 Apache配置 在Mac OS 10.10.1中是自带Apache软件的,我们 ...
- ORACLE 查看CPU使用率最高的语句及一些性能查询语句
select * from (select sql_text,sql_id,cpu_time from v$sql order by cpu_time desc) where rownum<=1 ...
- python的异常机制使用技巧
1.当你开发的接口被其他应用调用时,响应要及时,但是有些要触发的操作很耗时间. 比如下面需要通过被调用触发的函数create_job_1().但是这个函数执行会比较消耗时间 2.于是,我们可以利用异常 ...
- linux sendmail 邮件服务器架设
大家都知道架邮件服务器首先要架DNS服务.架设sendmail邮件服务器,以供大家一起学习探讨. 步骤一, 安装SNEDMAIL服务,查看你是否有安装SENDMAIL. #rpm -qa|grep b ...
- [Angular-Scaled web] 4. Using ui-router's named views
In the previous post, we use $stateProvider deriect to root url and using MainCtrl, categories.tmpl. ...