题目链接

将给出的已经有了的排序, 在前面加上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. RDLC报表系列(三) 总计和折叠

    继续接上一篇的内容,本文主要是简述总计和折叠 1.在Dept列右击,选择插入行->组内部下方(每个部门的Total),插入新的一行,并拆分单元格,然后合并列 3.设置文本框属性,格式为Total ...

  2. HTML——表格与表单

    1.表格 <table></table> background:背景图片. 属性 值 描述 align left center right 不赞成使用.请使用样式代替. 规定表 ...

  3. jQuery Uploadify上传插件

    jQuery Uploadify在ASP.NET MVC3中的使用 1.Uploadify简介 Uploadify是基于jQuery的一种上传插件,支持多文件.带进度条显示上传,在项目开发中常被使用. ...

  4. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  5. (Qt 翻译) QGLAbstractScene

    QGLAbstractScene Class Reference #include <QGLAbstractScene> This class was introduced in Qt3D ...

  6. Powerdesigner逆向工程从sql server数据库生成pdm (完整版)

    第一步:打开"控制面板"中的"管理工具" 第二步:点击"管理工具"然后双击"数据源(odbc)" 第三步:打开之后,点击 ...

  7. 关于jQuery中的ajax的方法介绍

           jQuery提供一系列Ajax函数方便我们调用Ajax, 其中最核心也是最复杂的是jQuery.ajax(),所有的其他Ajax函数都是它的一个简化调用.当我们想要完全控制Ajax时可以 ...

  8. “如何稀释scroll事件”引出的问题

    背景:我在segmentfault提了个问题如何稀释onscroll事件,问题如下: 面试时问到这个问题,是这样的:    面试官问一个关于滚动到某个位置的时候出现一个顶部的导航栏,答完之后,她接着问 ...

  9. centos 添加epel、remi仓库和ELRepo仓库

    centos使用yum安装软件非常方便,yum会自动安装软件的相关依赖.但是centos自带的源仓库,软件相对老旧并且不太全,所以我们可以添加第三方仓库,可以安装较新的软件版本. epel是fedor ...

  10. DataTables warning 错误警告

    今天使用 Charisma 框架的 jquery datatable 插件时出现如下错误: 搜索才发现 DataTables 目前不支持有单元格合并的表格.而且只要单元格数目不同就会有错误,不能使用搜 ...