CodeForces - 1250J The Parade 二分
题意:
一共n种身高,每一个士兵有一个身高。你需要把他们安排成k行(士兵不需要全部安排),每一行士兵身高差距小于等于1.你要找出来最多能安排多少士兵
题解:
这道题很容易就能看出来就是一道二分,二分一行有多少士兵(假设二分出来的值为x)
因为题目上说明每一行士兵的身高差距不能大于1,所以只有输入这n个身高中相邻的才可以安排在一行
TLE代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 using namespace std;
7 const int maxn=30005;
8 const int INF=0x3f3f3f3f;
9 typedef long long ll;
10 ll v[maxn],n,k,w[maxn];
11 bool panduan(ll x)
12 {
13 for(ll i=1;i<=n;++i)
14 w[i]=v[i];
15 ll start=1,ci=k;
16 while(ci)
17 {
18 if(w[start]>=x)
19 {
20 w[start]-=x; //因为我这里是一次一次减x,所以会超时
21 ci--;
22 }
23 else
24 {
25 if(w[start]+w[start+1]>=x && start+1<=n)
26 {
27 w[start+1]=(w[start]+w[start+1])-x;
28 start++;
29 ci--;
30 }
31 else
32 {
33 start++;
34 }
35 }
36 if(start>n) break;
37 }
38 if(ci) return 0;
39 else return 1;
40 }
41 int main()
42 {
43 ll t;
44 scanf("%lld",&t);
45 while(t--)
46 {
47 ll sum=0;
48 scanf("%lld%lld",&n,&k);
49 for(ll i=1;i<=n;++i)
50 {
51 scanf("%lld",&v[i]);
52 sum+=v[i];
53 }
54 ll mid,ans=0,l=1,r=sum/k; //这里一定要给ans初始化为0
55 while(l<=r)
56 {
57 mid=(l+r)>>1;
58 if(panduan(mid))
59 {
60 ans=mid;
61 l=mid+1;
62 }
63 else r=mid-1;
64 }
65 printf("%lld\n",ans*k);
66 }
67 return 0;
68 }
正确代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 using namespace std;
7 const int maxn=30005;
8 const int INF=0x3f3f3f3f;
9 typedef long long ll;
10 ll v[maxn],n,k,w[maxn];
11 bool panduan(ll x)
12 {
13 for(ll i=1; i<=n; ++i)
14 w[i]=v[i];
15 ll start=1,ci=k;
16 while(ci>0)
17 {
18 if(w[start]>=x)
19 {
20 ci=ci-w[start]/x;
21 w[start]%=x;
22 }
23 else
24 {
25 if(start+1<=n && w[start]+w[start+1]>=x)
26 {
27 ci=ci-(w[start]+w[start+1])/x;
28 w[start+1]=(w[start]+w[start+1])%x;
29 start++;
30 }
31 else
32 {
33 start++;
34 }
35 }
36 if(start>n) break;
37 }
38 if(ci<=0) return 1;
39 else return 0;
40 }
41 int main()
42 {
43 ll t;
44 scanf("%lld",&t);
45 while(t--)
46 {
47 ll sum=0;
48 scanf("%lld%lld",&n,&k);
49 for(ll i=1;i<=n;++i)
50 {
51 scanf("%lld",&v[i]);
52 sum+=v[i];
53 }
54 ll mid,ans=0,l=1,r=sum/k; //一定要给ans初始化为0,我错了好几次。。
55 while(l<=r)
56 {
57
58 mid=(l+r)/2;
59 if(panduan(mid))
60 {
61 ans=mid;
62 l=mid+1;
63 }
64 else r=mid-1;
65 }
66 printf("%lld\n",ans*k);
67 }
68 return 0;
69 }
CodeForces - 1250J The Parade 二分的更多相关文章
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]
题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...
- Codeforces 1114E - Arithmetic Progression - [二分+随机数]
题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
随机推荐
- 为啥使用innodb_flush_method=o_direct 就能减轻io压力呢
为啥使用innodb_flush_method=o_direct 就能减轻io压力呢
- 【Linux】ntp的一些坑。你肯定遇到过
ntpdate提示 19 Jan 10:33:11 ntpdate[29616]: no server suitable for synchronization found 这种问题从下面几个点开始验 ...
- 【RAC】双节点RAC搭建
本文主要是双节点的RAC进行搭建,根据黄伟老师的视频进行总结和使用. 搭建环境: 1.两台安装好Linux_x64系统的服务器 2.IP设置 注意:Priv-IP的IP是自己一个网段,而剩下的SCAN ...
- oracle 释放表空间到OS(resize)
1.查看表空间里面的对象 SELECT OWNER AS OWNER, SEGMENT_NAME AS SEGMENT_NAME, SEGMENT_TYPE AS SEGMENT_TYPE, SUM ...
- oracle 12C单实例打PSU
前提: oracle不管打什么样的补丁,readme都是很好的参考资料. Oracle每季度都会更新一个最新的PSU,现在12.1.0.2.0的最新的PSU是Patch 26925311. 由于今天白 ...
- WCNSS_qcom_cfg.ini WIFI配置文件参数详细解析
STA相关的一般配置 参数 含义 最小值 最大值 默认值 gNeighborLookupThreshold 1 触发roam scan发生的条件在WCNSS_qcom_cfg.ini文件中gNeigh ...
- EFCore 5 新特性 —— Savepoints
EFCore 5 中的 Savepoints Intro EFCore 5中引入了一个新特性,叫做 Savepoints,主要是事务中使用,个人感觉有点类似于 Windows 上的系统还原点,如果事务 ...
- MYSQL基础知识的复习1
数据库(是存放数据的仓库) 1.根据存储量以及安全性上来划分: 大型数据库:DB2 Oracle(毕业) Hbase 银行 公安局(不加班 没网) 移动 中型数据库:mysql sqlserver(. ...
- 前端工程构建之谈:gulp3要不要升级到Gulp4
关于升级还是不升级,这是一个哲学问题. gulp4的语法更加现代,支持ES6的大部分写法,使用exports的方式去暴露任务组合,更加灵活和便捷. gulp4同时也提供了很多强大的API,例如para ...
- Failed to start ssh.service: Unit not found.
Failed to start ssh.service: Unit not found. 报错内容: [Centos7@localhost ~]$ service ssh start Redirect ...