codeforces 659C . Tanya and Toys 二分
题目链接
将给出的已经有了的排序, 在前面加上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 二分的更多相关文章
- codeforces 659C Tanya and Toys
题目链接:http://codeforces.com/problemset/problem/659/C 题意: n是已经有的数字,m是可用的最大数字和 要求选自己没有的数字,且这些数字的数字和不能超过 ...
- 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 ...
- 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 ...
- 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 ...
- codeforces 518B. Tanya and Postcard 解题报告
题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...
- poj 2318 TOYS (二分+叉积)
http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 101 ...
- CodeForces 377B---Preparing for the Contest(二分+贪心)
C - Preparing for the Contest Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- 2016NEFU集训第n+3场 G - Tanya and Toys
Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...
随机推荐
- Android EditText 取消复制粘贴功能,取消横向全屏编辑功能(一)
在做一些安全性的软件时候常常要考虑取消 EditText 上的复制粘贴功能以确保安全性.下面就记录了这个方法: 首先在API-11以下的版本很简单,只需要在Xml布局文件或者用代码把长按属性设置成fa ...
- hbase namespace问题
如果遇到进入shell之后HMaster自动挂掉的问题,并且master的log里出现“TableExistsException: hbase:namespace”字样,很可能是更换了Hbase的版本 ...
- java创建对象的几种常用方法
java几种常见的创建对象的方法: 1.使用new关键字创建对象 2.利用java的反射机制,通过java.lang.Class或者java.lang.reflect.Constructor创建对象 ...
- 前端开发面试题收集 HTML
Doctype是什么?说明特点. <!doctype>声明必须处于HTML文档的头部,在标签之前. <!doctype>声明不是一个HTML标签,是一个用于告诉浏览器使用哪种H ...
- linux下实现两人、三人无序对话功能
序:引子 对话功能实际上就是利用管道见得通信.最原始的是一方发另一方收,不能进项交互,发送方的代码如下: /*============================================ ...
- C#的DLL注册为COM,Delphi来调用
非常实用的东西!过去知道这个方法的话可以解决多少问题啊 首先建立一个C#的DLL工程,写一个类 //Test.csnamespace Test...{public class MyTest...{pu ...
- 百度静态资源(JS)公共库
例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps ...
- poj1503---大数加法
先讲一种错误的做法:WA了n次,大神一定帮我看一下//看到有说数组大小开到250,我改了之后还是不//思路是将arr这个数组的每一行附上输入的值,然后求每列所有数之和,当然进位 //maxlen记录这 ...
- 高效 Java Web 开发框架 JessMA v3.2.3 beta-2 发布
JessMA(原名:Portal-Basic)是一套功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hib ...
- Bone Collector(01背包+记忆化搜索)
Bone Collector Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...