codeforce 1070 E Getting Deals Done(二分求最大化最小值)
Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a task takes five minutes or less, do it immediately". Polycarp likes the new rule, however he is not sure that five minutes is the optimal value. He supposes that this value d
should be chosen based on existing task list.
Polycarp has a list of n
tasks to complete. The i-th task has difficulty pi, i.e. it requires exactly pi minutes to be done. Polycarp reads the tasks one by one from the first to the n-th. If a task difficulty is d or less, Polycarp starts the work on the task immediately. If a task difficulty is strictly greater than d
, he will not do the task at all. It is not allowed to rearrange tasks in the list. Polycarp doesn't spend any time for reading a task or skipping it.
Polycarp has t
minutes in total to complete maximum number of tasks. But he does not want to work all the time. He decides to make a break after each group of m consecutive tasks he was working on. The break should take the same amount of time as it was spent in total on completion of these m
tasks.
For example, if n=7
, p=[3,1,4,1,5,9,2], d=3 and m=2
Polycarp works by the following schedule:
- Polycarp reads the first task, its difficulty is not greater than d
(p1=3≤d=3) and works for 3 minutes (i.e. the minutes 1, 2, 3
- );
- Polycarp reads the second task, its difficulty is not greater than d
(p2=1≤d=3) and works for 1 minute (i.e. the minute 4
- );
- Polycarp notices that he has finished m=2
tasks and takes a break for 3+1=4 minutes (i.e. on the minutes 5,6,7,8); Polycarp reads the third task, its difficulty is greater than d (p3=4>d=3) and skips it without spending any time; Polycarp reads the fourth task, its difficulty is not greater than d (p4=1≤d=3) and works for 1 minute (i.e. the minute 9); Polycarp reads the tasks 5 and 6, skips both of them (p5>d and p6>d); Polycarp reads the 7-th task, its difficulty is not greater than d (p7=2≤d=3) and works for 2 minutes (i.e. the minutes 10, 11); Polycarp notices that he has finished m=2 tasks and takes a break for 1+2=3 minutes (i.e. on the minutes 12,13,14). Polycarp stops exactly after t minutes. If Polycarp started a task but has not finished it by that time, the task is not considered as completed. It is allowed to complete less than m tasks in the last group. Also Polycarp considers acceptable to have shorter break than needed after the last group of tasks or even not to have this break at all — his working day is over and he will have enough time to rest anyway.Please help Polycarp to find such value d, which would allow him to complete maximum possible number of tasks in t minutes.InputThe first line of the input contains single integer c (1≤c≤5⋅104) — number of test cases. Then description of c test cases follows. Solve test cases separately, test cases are completely independent and do not affect each other.Each test case is described by two lines. The first of these lines contains three space-separated integers n, m and t (1≤n≤2⋅105,1≤m≤2⋅105,1≤t≤4⋅1010) — the number of tasks in Polycarp's list, the number of tasks he can do without a break and the total amount of time Polycarp can work on tasks. The second line of the test case contains n space separated integers p1,p2,…,pn (1≤pi≤2⋅105) — difficulties of the tasks.The sum of values n for all test cases in the input does not exceed 2⋅105.OutputPrint c lines, each line should contain answer for the corresponding test case — the maximum possible number of tasks Polycarp can complete and the integer value d (1≤d≤t) Polycarp should use in time management rule, separated by space. If there are several possible values d for a test case, output any of them.
4
5 2 16
5 6 1 4 7
5 3 30
5 6 1 4 7
6 4 15
12 5 15 7 20 17
1 1 50
100
4 7
2 10
0 25
题意:有n个任务,每一个任务都有一个难度pi你有t个时间去完成这些任务,对于难度为pi的任务你需要pi个时间去完成,对于你要完成的任务有一个限制范围d(你只能完成难度小于等于d的任务),当你每完成m个任务时你需要休息;
一旦遇到难度比d小的任务你必须完成它;
题解:这道题的关键是找到d的最小下界,如果找到d这道题也就解决了,而这个题就转换成了二分找最大化最小值的问题;再不休息的情况下二分查找mid,找到符合条件的最小值就可以啦(符合条件:难度小于d的任务难度之和必须大于t)
这样我们就找到了d(由于mid,和mid-1不确定,所以我们要都搜索一下),输出任务的最大值,和d就可以啦;这道题由于数据量比较大,所以cin会超时,所以要用scanf;
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
ll m,n,k,kmp,ans1,ans2=,flag;
map<ll,pair<ll,ll> >mp;
vector<ll>str;
bool check(ll mid)
{
ll sum=,num=,cnt=,kk=,kmp;
for(ll i=; i<m; i++)
{
if(str[i]<=mid)
{
if(num==n)
{
num=;
kk+=sum*;
sum=;
}
num++;
sum+=str[i];
cnt++;
if(sum+kk<=k)
{
ans1=cnt;
ans2=mid;
flag=;
}
}
}
if(flag)//如果满足条件就存起来
{
mp[mid].first=ans1;
mp[mid].second=ans2;
}
return kk+sum>k;
}
void solve()
{
cin>>m>>n>>k;
ll p;
str.clear();
for(ll i=; i<m; i++)
{
scanf("%lld",&p);
str.push_back(p);
}
ll pos=k;
for(ll l=,r=k; l<=r;)
{
ll mid=(l+r)/;
if(check(mid))
{
r=mid-;
pos=mid;
}
else
{
l=mid+;
}
} check(pos-);
if(!flag)
cout<<ans1<<" "<<""<<endl;
else//找到可行解
{
if(mp[pos].first>mp[pos-].first)//我用了pair,只是秀一下操作,直接用map就可以啦
cout<<mp[pos].first<<" "<<mp[pos].second<<endl;
else
cout<<mp[pos-].first<<" "<<mp[pos-].second<<endl; } }
int main()
{
ll T;
scanf("%lld\n",&T);
while(T--)
{
solve();
mp.clear();
ans1=,ans2=;flag=;//一定要初始化
}
}
codeforce 1070 E Getting Deals Done(二分求最大化最小值)的更多相关文章
- POJ 3258(二分求最大化最小值)
题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...
- POJ_2456 Aggressive cows 【二分求最大化最小值】
题目: Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are l ...
- POJ-2456.Aggressivecows.(二分求解最大化最小值)
本题大意:在坐标轴上有n个点,现在打算在这n个点上建立c个牛棚,由于牛对厂主的分配方式表示很不满意,它很暴躁,所以它会攻击离它很近的牛来获得快感,这件事让厂主大大知道了,他怎么可能容忍?所以他决定有策 ...
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
- 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模
题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...
- hdu5256 二分求LIS+思维
解题的思路很巧,为了让每个数之间都留出对应的上升空间,使a[i]=a[i]-i,然后再求LIS 另外二分求LIS是比较快的 #include<bits/stdc++.h> #define ...
- 二分求幂/快速幂取模运算——root(N,k)
二分求幂 int getMi(int a,int b) { ; ) { //当二进制位k位为1时,需要累乘a的2^k次方,然后用ans保存 == ) { ans *= a; } a *= a; b / ...
- 二分求幂,快速求解a的b次幂
一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; ; cin >> a >> b; ; i < ...
- hdu 3641 数论 二分求符合条件的最小值数学杂题
http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...
随机推荐
- windows下gvim中文乱码解决方案
网罗了一些网上的解决windows下gvim中文乱码的解决方案,都试了一遍,可惜都不能完全解决我的所有问题,最后我综合一下网上的两种方案,得到了最后完全解决我的中文乱码问题的方案,配置很简单,就是把下 ...
- Python Tkinter 学习历程 一
一丶一个简单的程序 from tkinter import * #引入所有类#查看tk版本#tkinter._test() root = Tk(); #对这个类进行实例化 w1 = Label(roo ...
- RF设置全局变量
一般情况下,我们的测试用例会有很多公用数据,比如在测试购票功能的时候,可能是一直使用同一个列车号,这时候我们就没有必要在每一个Case中都去新建一个列车班次,而是设置一个全局变量: 1.Set Var ...
- Tornado部署与运行
运行多个Tornado实例 网页响应不是特别的计算密集型处理多个实例充分利用 CPU多端口怎么处理4.使用Supervisor监控Tornado进程安装(注意看是否需要指定使用python2版本) s ...
- Java 设计模式之单例模式(一)
原文地址:Java 设计模式之单例模式(一) 博客地址:http://www.extlight.com 一.背景 没有太多原由,纯粹是记录和总结自己从业以来经历和学习的点点滴滴. 本篇内容为 Java ...
- 队列模拟题——pat1026. Table Tennis
题意自己理解了,主要是两个队列维护,一个VIP队列,一个普通队列 搜集了一些坑(有些坑转自别的网站用于广大同学的测试之用) 普通人也有VIP的权益!!! 屌丝逆袭有木有!!! 920:52:00 10 ...
- jQuery实现页面监听(某一个值发生改变时触发)
使用jQuery实现页面中监听 页面中某一个值(如input输入框)发生改变时触发. <html> <head> <title>RunJS</title& ...
- 阻塞队列之五:LinkedBlockingQueue
一.LinkedBlockingQueue简介 LinkedBlockingQueue是一个使用链表完成队列操作的阻塞队列.链表是单向链表,而不是双向链表.采用对于的next构成链表的方式来存储对象. ...
- HTC8X V版 电信上网方法
原始V版电信上网设置,转自百度贴吧(http://tieba.baidu.com/p/3224177802). 修改SIM卡设置 修改MIP_MODE 转自贴吧:http://tieba.baidu. ...
- 【学徒日记】Unity 动画调用事件
http://note.youdao.com/noteshare?id=a15f965fc57a0b25c87ee09388cf0f4a 具体内容看上面的链接. 1. 在脚本里写一个函数,它的参数只能 ...