本题使用贪心法。关键是考贪心策略。同一时候要求要细心,我提交的时候也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 贪心法的更多相关文章

  1. CodeForces - 401C Team(简单构造)

    题意:要求构造一个字符串,要求不能有连续的两个0在一起,也不能有连续的三个1在一起. 分析: 1.假设有4个0,最多能构造的长度为11011011011011,即10个1,因此若m > (n + ...

  2. 贪心法 codevs 1052 地鼠游戏

    1052 地鼠游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 王钢是一名学习成绩优异的学生,在平 ...

  3. codeforces 932E Team Work(组合数学、dp)

    codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...

  4. 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 ...

  5. LeetCode刷题总结-二分查找和贪心法篇

    本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...

  6. C 编译器的“贪心法”

    C语言中有单字符符号和多字符符号之分,那么,当C编译器读入一个字符‘/’后又跟了一个字符‘*’,那么编译器就必须做出判断:是将其作为两个分别的符号对待,还是合起来作为一个符号对待.C语言对这个问题的解 ...

  7. Codeforces Round #249 (Div. 2)B(贪心法)

    B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces 410C.Team[构造]

    C. Team time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  9. codeforces 757F Team Rocket Rises Again

    链接:http://codeforces.com/problemset/problem/757/F 正解:灭绝树. mdzz倍增lca的根节点深度必须是1..我因为这个错误调了好久. 我们考虑先求最短 ...

随机推荐

  1. SEPIC 单端初级电感转换器 稳压器 -- Zeta 转换器

    single ended primary inductor converter 单端初级电感转换器 SEPIC(single ended primary inductor converter) 是一种 ...

  2. Spring EL运算符实例

    Spring EL支持大多数标准的数学,逻辑和关系运算符. 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , l ...

  3. ubuntu上安装systemtap

    http://www.cnblogs.com/hdflzh/archive/2012/07/25/2608910.html

  4. hdu 2176 取石子游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=2176 提示:尼姆博弈,异或 #include <iostream> #include <cst ...

  5. Eclipse搭建Gradle环境

    转自:http://blog.sina.com.cn/s/blog_4b20ae2e0102uz4t.html 1.上Grandle官网下载Gradle,地址:http://www.gradle.or ...

  6. flume和kafka整合(转)

    原文链接:Kafka flume 整合 前提 前提是要先把flume和kafka独立的部分先搭建好. 下载插件包 下载flume-kafka-plus:https://github.com/beyon ...

  7. Informatica 常用组件Source Qualifier之五 User Defined Join

    User defined join :      输入用户定义的联接与输入自定义 SQL 查询类似.但是,只需输入 WHERE 子句的内容,而不是整个查询. 添加用户定义的联接时,源限定符转换包括默认 ...

  8. 织梦(Dedecms)select_soft_post.php页面变量未初始漏洞

    漏洞版本: Dedecms 5.5 漏洞描述: 漏洞产生文件位于include\dialog\select_soft_post.php,其变量$cfg_basedir没有正确初始化,导致可以饶过身份认 ...

  9. javascript实现浏览器窗口传递参数

    a.html <html> <head> <title>主页面</title> <script language="javascript ...

  10. jQuery调用WebService

    1.编写4种WebService方法     [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBindi ...