题目

题意:

一共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 二分的更多相关文章

  1. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  2. CodeForces 670D1 暴力或二分

    今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1   This problem is given in two versions that diff ...

  3. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

  4. Codeforces 626C Block Towers(二分)

    C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  5. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

  6. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...

  7. Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]

    题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...

  8. Codeforces 1114E - Arithmetic Progression - [二分+随机数]

    题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...

  9. Codeforces 660C - Hard Process - [二分+DP]

    题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...

随机推荐

  1. MySQL 使用MD5对数据进行加密

    数据库MD5加密 -- ================ 测试 MD5 加密 ============== CREATE TABLE `testmd5`( id INT(11) NOT NULL AU ...

  2. springmvc 字符串转日期格式

    http://www.mamicode.com/info-detail-2485490.html

  3. python多线程和GIL全局解释器锁

    1.线程     线程被称为轻量级进程,是最小执行单元,系统调度的单位.线程切换需要的资源一般,效率一般.  2.多线程         在单个程序中同时运行多个线程完成不同的工作,称为多线程 3.并 ...

  4. Transformation-Based Error-Driven Learning and Natural Language Processing: A Case Study in Part-of-Speech Tagging

    http://delivery.acm.org/10.1145/220000/218367/p543-brill.pdf?ip=116.30.5.154&id=218367&acc=O ...

  5. BFS DFS与回溯

    https://blog.csdn.net/u014303647/article/details/88328526 cyc: https://github.com/CyC2018/CS-Notes/b ...

  6. Spring Cloud,Docker书籍资源、优秀博文等记录

    Spring Cloud,Docker书籍资源.优秀博文等记录 Spring Cloud,Docker书籍资源.优秀博文等记录 一.书籍 二.博文地址 三.思维导图Or图片 3.1一张图总结 Dock ...

  7. 单体架构、SOA架构、微服务架构

  8. sourcetree注册

    http://www.cnblogs.com/xiofee/p/sourcetree_pass_initialization_setup.html

  9. ceph --- (简单操作及openstack交接)

    部署ceph :https://www.cnblogs.com/cloudhere/p/10519647.html Centos7部署ceph:https://www.cnblogs.com/kevi ...

  10. trunk

    今天我们一起聊trunk(接vlan之后),一台switch我们用vlan就可以划分vlan(虚拟局域网),但是2台switch该怎么办呢? 实验环境搭建 switch0 : enable //切换到 ...