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 ...
随机推荐
- asp.net几种<% %>用法
在asp.net应用程序中,在asp.net页面常用的<%@ %>.<%# %>.<%= %>.在全球化的项目中使用<%$ %>绑定资源项目,在asp. ...
- beforefieldinit释义(2)
首先来看一段代码: using System; namespace BeforeFieldInit { internal class Foo { Foo(){ Console.WriteLine(&q ...
- poj3254Corn Fields题解
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9623 Accepted: 5092 Descr ...
- Web应用的部署
本文将介绍一些Web应用的部署: 部署规则 要成功地部署一个Web应用,必须遵循以下目录结构. 1.WEB-INF一定要直接放到应用上下文(Webapp)之下. 2.classes目录必须直接放在WE ...
- 关于ASP.Net的一些概念 转载
①理解 .NET Platform Standard 作者:田园里的蟋蟀 http://www.cnblogs.com/xishuai/archive/2016/05/24/understand-do ...
- Elevator
问题陈述: 杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1008 问题分析: 简单题. 代码详解: #include < ...
- Linux系统重启network服务失败
问题描述 使用KVM通过修改配置文件配置好网卡IP,使用命令行service network restart 重启网络服务失败. 如图: 使用图形化管理工具配置IP,在系统界面右上角可以看到网卡状态为 ...
- ubuntu 设置网卡为混杂模式 以及网络配置命令
1. ifconfig eth0 promisc 设置eth0为混杂模式. ifconfig eth0 -promisc 取消它的混杂模式 botnet@botnet-virtual-machine: ...
- Cannot access empty property
致命错误:不能够进入此空值,位于E:\sunlion\web\down\class\db_sql.php 代码 <?php Class TestClass1{ var $class2; publ ...
- 使用SuperWebSocket 构建实时 Web 应用
Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特别频繁的应用尚能相安无事, ...