题目

题意:

一共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. P2979 [USACO10JAN]奶酪塔Cheese Towers(完全背包,递推)

    题目描述 Farmer John wants to save some blocks of his cows' delicious Wisconsin cheese varieties in his ...

  2. Jquery实现对Array数组实现类似Linq的Lambda表达式的Where方法筛选

    平时使用Linq,习惯了Lambda表达式,用着非常顺手,奈何在Jquery里面不能这样用,只能循环一个个判断.趁空闲时间找了找,自己写了这样的扩展方法.目前写出了三种方案,没有比较性能,觉得都可以用 ...

  3. 运用 pyinstaller 打包的python exe文件运行 去掉命令行窗口及其他参数汇总

    运行exe文件的时候,会弹出一个dos命令窗口,这个窗口可以看到一些打印信息,如果想只运行tkinter 页面,去掉dos窗口需要在打包的时候 加上 -w 参数 pyinstaller -F XX.p ...

  4. [阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本

    [阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本 目录 [阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本 0x00 摘要 0x01 背景 1.1 代码进化 1.2 Deep ...

  5. 使用bandit对目标python代码进行安全函数扫描

    技术背景 在一些对python开源库代码的安全扫描中,我们有可能需要分析库中所使用到的函数是否会对代码的执行环境造成一些非预期的影响.典型的例如python的沙箱逃逸问题,通过一些python的第三方 ...

  6. CISCO 如何重置3850交换机密码

    SUMMARY STEPS: Connect a terminal or PC to the switch. Set the line speed on the emulation software ...

  7. 编程小技巧之 Linux 文本处理命令(二)

    合格的程序员都善于使用工具,正所谓君子性非异也,善假于物也.合理的利用 Linux 的命令行工具,可以提高我们的工作效率. 本篇文章是<Linux 文本处理命令> 续篇,在前文的基础上再介 ...

  8. Correct the classpath of your application so that it contains a single, compatible version of org.thymeleaf.spring5.SpringTemplateEngine

    Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...

  9. Servlet中的一些注意事项

    servlet中的一些注意事项 1 什么是servlet? 1)Servlet是Sun公司制定的一套技术标准,包含与Web应用相关的一系列接口,是Web应用实现方式的宏观解决方案.而具体的Servle ...

  10. LOJ10068 秘密的牛奶运输

    LOJ10068秘密的牛奶运输 题目描述 Farmer John 要把他的牛奶运输到各个销售点.运输过程中,可以先把牛奶运输到一些销售点,再由这些销售点分别运输到其他销售点. 运输的总距离越小,运输的 ...