题目链接

将给出的已经有了的排序, 在前面加上0, 后面加上1e9+1。

然后对相邻的两项判断。 如果相邻两项之间的数的和小于m, 那么全都选上, m减去相应的值。

如果大于m, 那么二分判断最多能选多少个。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int ans[maxn], a[maxn], cnt;
int check(int l, int r, int sum) {
int len = r-l+1;
ll tmp = 1LL*(l+r)*len/2;
if(tmp<=sum)
return 1;
return 0;
}
void bin(int l, int r, int sum) {
int pos = l;
int tmpl = l;
while(l<=r) {
int m = l+r>>1;
if(check(tmpl, m, sum)) {
pos = m;
l = m+1;
} else
r = m-1;
}
for(int i = tmpl; i <= pos; i++)
ans[cnt++] = i;
}
int main()
{
int n, m;
cin>>n>>m;
a[0] = 0;
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
a[++n] = 1e9+1;
sort(a, a+n+1);
for(int i = 0; i < n; i++) {
int tmp = a[i+1]-a[i];
if(tmp==1)
continue;
if(tmp==2) {
if(m>=a[i]+1) {
m -= (a[i]+1);
ans[cnt++] = a[i]+1;
continue;
} else
break;
}
int a1 = a[i]+1, an = a[i+1]-1;
tmp--;
ll sum = 1LL*(a1+an)*tmp/2;
if(sum<=m) {
m -= sum;
for(int j = a[i]+1; j<=a[i+1]-1; j++) {
ans[cnt++] = j;
}
} else {
if(a1<=m) {
bin(a1, an, m); }
break;
}
}
cout<<cnt<<endl;
for(int i = 0; i < cnt; i++)
printf("%d ", ans[i]);
return 0;
}

codeforces 659C . Tanya and Toys 二分的更多相关文章

  1. codeforces 659C Tanya and Toys

    题目链接:http://codeforces.com/problemset/problem/659/C 题意: n是已经有的数字,m是可用的最大数字和 要求选自己没有的数字,且这些数字的数字和不能超过 ...

  2. codeforces 659C C. Tanya and Toys(水题+map)

    题目链接: C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. Codeforces Round #346 (Div. 2) C Tanya and Toys

    C. Tanya and Toys 题目链接http://codeforces.com/contest/659/problem/C Description In Berland recently a ...

  4. Codeforces Round #346 (Div. 2) C. Tanya and Toys 贪心

    C. Tanya and Toys 题目连接: http://www.codeforces.com/contest/659/problem/C Description In Berland recen ...

  5. codeforces 518B. Tanya and Postcard 解题报告

    题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...

  6. poj 2318 TOYS (二分+叉积)

    http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 101 ...

  7. CodeForces 377B---Preparing for the Contest(二分+贪心)

    C - Preparing for the Contest Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  8. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  9. 2016NEFU集训第n+3场 G - Tanya and Toys

    Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...

随机推荐

  1. JAVAscript——菜单下拉列表练习(阻止事件冒泡)

    下拉列表框,鼠标点击文本框,出现下拉,鼠标(离开的时候或者点击网页其他位置时)下拉列表消失.鼠标放到下拉列表的某一项上变背景色,点击下拉列表的某一项将该项的值显示在文本框内,然后下拉列表消失. < ...

  2. @property属性

    1. 读写属性(readwrite/ readonly) 默认为readwrite,表示该属性既可以读取,也可以给该属性变量赋值:readonly则表示只能读取该属性变量. 2. 原子属性 (atom ...

  3. EventBusException:xxxx has no methods onEvent

    使用第三方框架EventBus,在register时出现Exception:xxxx has no methods onEvent. 场景:在Activity中没有接收事件,只是需要发送事件,但是有对 ...

  4. js学习心得(一)(菜鸟)

    js基础已经打了好几次了,慕课跟着学了一遍,视频看了一些,还读了诸如 jsdom艺术,js精粹以及锋利jq(没读完). 这次再次重头读并写一遍代码,工具书是js,查缺补漏高级程序设计第二版,犀牛书有点 ...

  5. 下载PHPDroid: 基于WebView和PHP内置HTTP服务器开发Android应用

    基于Android上的PHP(比如我打包的PHPDroid),寥寥几行PHP代码,就能实现一个支持无线局域网用浏览器访问的Android手机的Shell,用于执行命令和PHP代码.       个人在 ...

  6. js调用打印机

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. cdoj Dividing Numbers 乱搞记忆化搜索

    //真tm是乱搞 但是(乱搞的)思想很重要 解:大概就是记忆化搜索,但是原数据范围太大,不可能记下所有的情况的答案,于是我们就在记下小范围内的答案,当dfs落入这个记忆范围后,就不进一步搜索,直接返回 ...

  8. php使用NuSoap调用java/C# webservice乱码问题

    今天调用了一个 NuSoap 的接口程序,一切流程操作都很正常,就是最后接收返回值的时候出现了乱码问题(我这边是做一个越南项目固然返回越南语,不过认为中文应该同样实用,需要的人可以尝试下)   许多使 ...

  9. 一个利用扩展方法的实例:AttachDataExtensions

    扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一.这是老赵自以为的一个简单却不失经典的实例: [AttributeUsage(AttributeTargets.All, Al ...

  10. .NET(C#):使用反射来获取枚举的名称、值和特性【转】

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enumMyEnum { AAA, BBB, CCC } 背后的IL是这样的: .class ...