题目链接: 传送门

Doctor

time limit per test:1 second     memory limit per test:256 megabytes

Description

There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home.
Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.

Input

The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).

Output

If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue.
Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.

Sample Input

3 3
1 2 1

4 10
3 3 2 1

7 10
1 3 3 1 2 3 1

Sample Output

2

-1

6 2 3

解体思路:

题目大意:n只动物排队看病,每只动物需要看病的次数不一,每次医生只给一只动物看病,未满足看病次数的动物在给医生看完病后需要排队尾再次等待。问K次后剩下的动物的编号序列。
通过二分找出动物们完成K次看病时单个动物看病次数的最大值,然后剔除已经满足看病次数的动物,最后对于未剔除的输出编号。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const LL INF = 0x3f3f3f3f;
LL ans[100005];
LL N,K;

bool OK(LL x)
{
    LL sum = 0;
    for (int i = 0;i < N;i++)
    {
        sum += min(x,ans[i]);
    }
    return sum >= K;
}

int main()
{
    while (~scanf("%I64d%I64d",&N,&K))
    {
        LL sum = 0;
        vector<LL>itv;
        vector<LL>::iterator it;
        memset(ans,0,sizeof(ans));
        for (int i = 0;i < N;i++)
        {
            scanf("%I64d",&ans[i]);
            sum += ans[i];
        }

        if (K > sum)
        {
            printf("-1\n");
            continue;
        }

        LL left = 0,right = INF;
        while (left < right - 1)
        {
            LL mid = left + ((right-left) >> 1);
            if (OK(mid))
            {
                right = mid;
            }
            else
            {
                left = mid;
            }
        }
        LL val = left;
        cout << right << endl;
        cout << val << endl;
        for (int i = 0;i < N;i++)
        {
            K -= min(ans[i],val);
            ans[i] -= min(ans[i],val);
        }
        for (int i = 0;i < N;i++)
        {
            if (K && ans[i])
            {
                ans[i]--;
                K--;
            }
            else if (!K && ans[i])
            {
                itv.push_back(i+1);
                ans[i] = 0;
            }
        }
        for (int i = 0;i < N;i++)
        {
            if (ans[i])
            {
                itv.push_back(i+1);
            }
        }
        bool first = true;
        for (it = itv.begin();it != itv.end();it++)
        {
            first?printf("%I64d",*it):printf(" %I64d",*it);
            first = false;
        }
        printf("\n");
    }
    return 0;
}

CF 84D Doctor(二分)的更多相关文章

  1. CF 706B 简单二分,水

    1.CF 706B  Interesting drink 2.链接:http://codeforces.com/problemset/problem/706/B 3.总结:二分 题意:给出n个数,再给 ...

  2. [CF#592 E] [二分答案] Minimizing Difference

    链接:http://codeforces.com/contest/1244/problem/E 题意: 给定包含$n$个数的数组,你可以执行最多k次操作,使得数组的一个数加1或者减1. 问合理的操作, ...

  3. CF 1042A Benches——二分答案(水题)

    题目:http://codeforces.com/problemset/problem/1042/A #include<iostream> #include<cstdio> # ...

  4. codeforces 700A As Fast As Possible 二分求和?我觉得直接解更好

    分析:一辆车最多载k个人,车的速度肯定比人快,所以想要到达时间最短,那么每个人必须做一次公交车.那么把n个人分成p=(n+k-1)/k组.设最短时间为t,每人乘车时间为t1,则t1*v2+(t-t1) ...

  5. Codeforces 846D Monitor(简单二分+二维BIT)

    D. Monitor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. 树链剖分-Hello!链剖-[NOIP2015]运输计划-[填坑]

    This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...

  7. CF 600B Queries about less or equal elements --- 二分查找

    CF 600B 题目大意:给定n,m,数组a(n个数),数组b(m个数),对每一个数组b中的元素,求数组a中小于等于数组该元素的个数. 解题思路:对数组a进行排序,然后对每一个元素b[i],在数组a中 ...

  8. 【Cf #503 B】The hat(二分)

    为什么Cf上所有的交互题都是$binary \; Search$... 把序列分成前后两个相等的部分,每一个都可以看成一条斜率为正负$1$的折线.我们把他们放在一起,显然,当折线的交点的横坐标为整数时 ...

  9. CF 1405E Fixed Point Removal【线段树上二分】

    CF 1405E Fixed Point Removal[线段树上二分]  题意: 给定长度为\(n\)的序列\(A\),每次操作可以把\(A_i = i\)(即值等于其下标)的数删掉,然后剩下的数组 ...

随机推荐

  1. Windbg调优Kafka.Client内存泄露

    从来没写过Blog,想想也是,工作十多年了,搞过N多的架构.技术,不与大家分享实在是可惜了.另外,从传统地ERP行业转到互联网,也遇到了很所前所未有的问题,原来知道有一些坑,但是不知道坑太多太深.借着 ...

  2. lecture12-玻尔兹曼机和受限玻尔兹曼机

    这是Hinton的第12课,结合前一课可以知道RBM是来自BM,而BM是来自Hopfield的,因为水平有限,是直译的,虽然有时候会看不懂,但是好歹不会曲解原来的本意,看的话:1.先看ppt:2.通读 ...

  3. Expression Blend4经验分享:文字公告无缝循环滚动效果

    这次分享一个类似新闻公告板的无缝循环滚动效果,相信很多项目都会应用到这个效果.之前我也百度了一下,网上的一些Silverlight的文字或图片滚动效果,都是一次性滚动的,如果要做到无缝循环滚动,多数要 ...

  4. Safari 下用 "location.href = filePath" 实现下载功能的诡异 bug

    Safari 下的一些诡异 bug 我们已经领教一二,比如前文中说的 无痕浏览模式下使用 localStorage 的 API 就会报错.今天我们要讲的是利用 location.href = file ...

  5. StackExchange.Redis--纯干货喂饱你

    Redis和StackExchange.Redis redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库 ...

  6. Linux进程间通信之信号量

    春节过去了,真的过去一年了.在公司待了快一年了.2016希望自己变得越来越好. ps:上面那句话是年前写的,中间隔了那么久,自己也变懒了. 一.信号量 1,信号量本质是一个计数器,控制访问共享资源的最 ...

  7. Eclipse自动补全功能管理

    #这种方法只适用于Eclipse Classic版本(这个版本带有插件的源码) 在使用Eclispe的过程,感觉自动补全做的不好,没有VS的强大.下面说两个增强自动补全的方法: 1.增加Eclipse ...

  8. 16-head 简明笔记

    显示文件的头部 head [options] [file-list] 参数 file-list 为要head显示的文件的路径名列表.当指定多个文件时,head在显示每个文件的前几行内容之前显示对应的文 ...

  9. navigationView 的使用和布局文件的绑定

    今天项目进行到了细化内容的部分啦- 需要美化侧滑菜单,并且填充数据.在博客上看了好久发现大家的都大同小异 而且很少有提到如何绑定内容各处求助终于在一片博客上发现了蛛丝马迹!!上大神的帖子:blog.c ...

  10. MapReduce编程示例

    1.将hadoop插件放入eclipse/plugins目录中 2.eclipse配置hadoop 依赖包目录 Window—Preferences 3.新建Map/Reduce Project项目 ...