51nod - 1420 - 数袋鼠好有趣 - 贪心 - 二分
https://www.51nod.com/Challenge/Problem.html#!#problemId=1420
一开始乱搞了一发,每个袋鼠二分找最小的能放它的,然后二分的范围从下一个开始保证不会把两个小袋鼠装在同一个里面,还过了一半的数据……
然后才发现袋鼠并不能嵌套。想打vis标记大袋鼠跳过大袋鼠,然后样例都过不了。
又想了半天网络流,流个鬼鬼流。
看了一下别人的提示,贪心加二分。
好像我误解了别人的贪心加二分,跑得比别人还快快。
明显选的袋鼠一定是最小的那波,这样最优。
然后二分选的袋鼠的数量,套在我第一次交的那个里面,一发就过了,500ms,应该是正解的做法。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[500005];
int cpa[500005];
bool check(int m) {
    int be=m,en=n;
    for(int i=0; i<m; i++) {
        int res=lower_bound(a+be,a+en,2*a[i])-a;
        if(res==en)
            return false;
        else {
            be=res+1;
        }
    }
    return true;
}
int main() {
    while(~scanf("%d",&n)) {
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        for(int i=0;i<m;i++){
        }
        int ans=0;
        int l=0,r=n/2;
        while(1) {
            int m=(l+r)>>1;
            if(m==l){
                if(check(r)){
                    ans=n-r;
                }
                else{
                    ans=n-l;
                }
                break;
            }
            if(check(m)){
                l=m;
            }
            else{
                r=m-1;
            }
        }
        printf("%d\n",ans);
    }
}
然后看见一个更惊人的,从每个袋鼠开始选最大的能放进它的。好像没什么问题,这个贪心策略是对的。
2,4,5,8
从小的开始贪心,错在假如2用了4,虽然减少了一个,但假如2用了5,4用了8,那就减少两个。因为使用了4会影响4不能选8,所以不能这样贪心。
但是从大的开始贪心就没问题,例如8可以装4,可以装2,那装4一定更好。(对个鬼鬼,假如有3呢?把4吃了2放哪里?)。
其实这个策略的正确性应该在于先分成前后两部分(至于为什么要把奇数多出来的放在后部分,其实特判cnt不能超过n/2也可以,不然就有可能嵌套),前部分从后部分中选最合适的。这样不会有嵌套。但居然也是500ms,感觉被骗了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[500005];
int main() {
    while(~scanf("%d",&n)) {
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
        }
        sort(a,a+n,greater<int>());
        int cnt=0;
        for(int i=(n+1)/2;i<n;i++){
            if(a[cnt]>=2*a[i])
                cnt++;
        }
        printf("%d\n",n-cnt);
    }
}
或
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[500005];
int main() {
    while(~scanf("%d",&n)) {
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
        }
        sort(a,a+n,greater<int>());
        int cnt=0;
        for(int i=0;i<n;i++){
            if(a[cnt]>=2*a[i]){
                cnt++;
                if(cnt==n/2)
                    break;
            }
        }
        printf("%d\n",n-cnt);
    }
}
51nod - 1420 - 数袋鼠好有趣 - 贪心 - 二分的更多相关文章
- 51nod 1420 数袋鼠好有趣【贪心】
		1420 数袋鼠好有趣 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有n只袋鼠.每只袋鼠的大小用一个整数表示. ... 
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
		题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ... 
- poj 2782 Bin Packing (贪心+二分)
		F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ... 
- Card Game Cheater(贪心+二分匹配)
		Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ... 
- 51nod 1770 数数字
		1770 数数字 基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题 收藏 关注 统计一下 aaa ⋯ aaan个a × b 的结果里面 ... 
- BZOJ2796[Poi2012]Fibonacci Representation——贪心+二分查找
		题目描述 给出一个正整数x,问x最少能由多少个Fibonacci数加减算出. 例如1070=987+89-5-1,因此x=1070时答案是4. 输入 第一行一个正整数q (q<=10),表示有q ... 
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
		Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ... 
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
		The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ... 
- CodeForces - 1251D (贪心+二分)
		题意 https://vjudge.net/problem/CodeForces-1251D 您是一个大型企业的负责人.在您的企业当中共有n位员工为您工作,而且非常有趣的事是这个n是一个奇数(n不能被 ... 
随机推荐
- linux 中添加自己的库路径的方法 cannot open shared object file: No such file or directory
			本文转自:http://blog.csdn.net/maotianwang/article/details/44619197 库文档在连接(静态库和共享库)和运行(仅限于使用共享库的程式)时被使用,其 ... 
- Why containers? Why should we care? 新旧容器的对比
			https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/ The Old Way to deploy applications ... 
- android studio发布公共类库到服务器maven仓库
			在上一篇中提到了怎么创建私有maven库,这篇主要结合android studio的使用,直接进入正题,看以下步骤 1.创建android项目 创建Project,然后加一个library的modul ... 
- Bestcoder BestCoder Round #28 A Missing number(查找缺失的合法数字)
			Problem Description There is a permutation without two numbers in it, and now you know what numbers ... 
- Codeforces Round #394 (Div. 2)  C. Dasha and Password —— 枚举
			题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ... 
- 人生苦短之Python迭代器
			迭代 在Python中,如果给定一个list或者touple,我们可以通过for循环来遍历,将值依次取出,这种遍历称为迭代. 在Python中是通过for...in..来进行遍历的,在Java中则是 ... 
- “cannot be resolved to a type” 错误解决方法
			(1)jdk不匹配(或不存在) 项目指定的jdk为“jdk1.6.0_18”,而当前eclipse使用的是“jdk1.6.0_22”.需要在BuildPath | Libraries,中做简单调整. ... 
- shell动态变量
			面对变量中嵌套变量,可以这么做 other_devops_ip="......." options='_ip' tennat_name='other_devops' tennat_ ... 
- 实用jQuery代码片段
			maco精选的一些jQuery代码,也许你从中可以举一反三[代码] [JavaScript]代码001<p>002 <h3><span >★ 使用jQuery ... 
- codeforces   A. Punctuation   解题报告
			题目链接:http://codeforces.com/problemset/problem/147/A 题目意思:给定一篇文章,需要对这篇文章进行编辑,使得:(1)两个单词之间有一个空格分开 (2) ... 
