本题使用贪心法。关键是考贪心策略。同一时候要求要细心,我提交的时候也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. DELPHI纤程的演示

    DELPHI纤程的演示 DELPHI7编译运行通过. 纤程实现单元: unit FiberFun; //Fiber(纤程测试Demo)//2018/04/11//QQ: 287413288 //参考 ...

  2. EntityFramework 7.0之初探【基于VS 2015】(

    前言 本篇作为EF 7.0的开篇也是Entity Framework目前系列末篇,因为关于EF 7.0学习资料实在是太少,我都是参考老外的资料花费了不少时间去研究去尝试同时也失败多次,个人觉得那是值得 ...

  3. B-树学习笔记

    转自:http://blog.csdn.net/acs713/article/details/6880375 B-tree(多路搜索树,并不是二叉的)是一种常见的数据结构.使用B-tree结构可以显著 ...

  4. Hadoop Combiners

    In the last post and in the preceding one we saw how to write a MapReduce program for finding the to ...

  5. [gevent源代码分析] 深度分析gevent执行流程

    一直对gevent执行流程比較模糊,近期看源代码略有所得.不敢独享.故分享之. gevent是一个高性能网络库,底层是libevent,1.0版本号之后是libev.核心是greenlet.geven ...

  6. Qt Creator怎样更改默认构建目录

    用过VS的朋友都知道,用VS编译工程时会将生成的可执行文件放在当前工程目录下,使每个工程独立地成为一个整体,管理起来颇为方便:而Qt Creator则不同,编译程序时会创建一个与当前工程目录同级的构建 ...

  7. LruCache DiskLruCache 缓存 简介 案例 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. 论#include

    1.#include " "与#include <> #include " "表示预编译命令源程序在当前项目下寻找头文件,如果找不到,再到标准头文件 ...

  9. 解决Asp输出乱码问题

    在一个Asp页面中页面正常的中文字符都没有问题,但如果用Asp程序输出的话就显示为乱码 终于在百度经验上找到解决方案: 1.将文件编码更改为Utf-8 2.在页头添加"<%@LANGU ...

  10. [Transducer] Make Transducer works for Iteratable collection and Object

    We've seen how we can transduce from arrays or other iterables, but plain objects aren't iterable in ...