本题使用贪心法。关键是考贪心策略。同一时候要求要细心,我提交的时候也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. 64位系统下同时使用64位和32位的eclipse

    eclipse.ini 文件使用说明 The -vm option and its value (the path) must be on separate lines. The value must ...

  2. Linux操作系统的种种集成开发环境

    Linux操作系统的种种集成开发环境 随着Linux的逐渐兴起,已经有为数众多的程序在上面驰骋了,许多开发环境(Development Environment)也应运而生.好的开发环境一定是集成了编辑 ...

  3. EntityFramework:我想我需要和 Session.Save 语义一样的方法

    背景 EntityFramework 中 DbSet.Add 方法不会导致立即执行 insert 语句,这在长事务中非常有用,不过多数用例都是短事务的,为何我需要一个立即执行 insert 语句的方法 ...

  4. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  5. windows服务的安装和卸载方法

    安装 创建“安装.bat”文件,用记事本打开此文件,内容为 c:\windows\microsoft.net\framework\v4.0.30319\InstallUtil.exe E:\progr ...

  6. django 实现上传文件功能

    需求:自己写一个文件上传功能 代码: urls.py from django.conf.urls import url from django.contrib import admin from ap ...

  7. Kotlin 特性 语法糖 优势 扩展 高阶 MD

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

  8. 130道ASP.NET面试题,我只会80道!

    1. 简述 private. protected. public. internal 修饰符的访问权限.答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成员 ...

  9. [C#.NET] X509 數位電子簽章

    摘自: http://www.dotblogs.com.tw/yc421206/archive/2012/06/30/73140.aspx 在上篇[C#.NET] 字串及檔案,利用 RSA 演算法加解 ...

  10. 【pyhon】理想论坛单帖爬虫取得信息存入MySql数据库

    代码: # 单帖爬虫,用于爬取理想论坛单个帖子得到发帖人,发帖时间和回帖时间并存入数据库,url例子见main函数 from bs4 import BeautifulSoup import reque ...