真的是一遇到二分答案就猝不及防丫。。回想起了那年的跳石头qaq
这种思维还真不好练,刷题并不能起到什么作用,估计我得再被坑害几次才会掌握吧qaq
这题的二分貌似是想了半小时才想出来,不过实现的时候发现自己已经长年没写二分,写起来手非常生疏,犯了好多傻逼错误,心态爆炸云云。。早知道当时应该直接推掉重写,而且还误解题意qaq靠样例看题不行蛙qaq
题目事实上只对分数作了要求,对题量并没有要求,那么,在保证做题数最大的情况下,题量越少,时间一般是越宽松的,这样我们可以肯定题量和分数相同的情况必定存在,这么取的话策略和分数就可以一起处理了。
直接把数组按时间排序,保证取的时候能从小到大,判断的时候把数组按ai和t的大小关系拆成2组,直接把能得分的题按时间从小到大填,填得进就好,不进就pass了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf (ll)10000000000
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define NM 200005
#define nm 100005
#define pi 3.141592653
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
struct tmp{
int s,t,num;
bool operator<(const tmp&o){return t<o.t;}
}a[NM],b[NM],c[NM];
int n,m,tot,cnt,s,k,l,r,ans;
bool check(int x){
tot=cnt=0;
inc(i,1,n)if(a[i].s<x)c[++cnt]=a[i];
else b[++tot]=a[i];
for(k=1,s=0;k<=tot&&b[k].t+s<=m;k++)s+=b[k].t;
k--;
if(k>=x)return true;else return false;
}
int main(){
n=read();m=read();
inc(i,1,n)a[i].s=read(),a[i].t=read(),a[i].num=i;
sort(a+1,a+1+n);
l=1;r=n;
inc(i,1,n){s+=a[i].t;if(s>m){r=i-1;break;}}
while(l<=r){
int t=l+r>>1;
if(check(t))l=t+1,ans=max(ans,t);
else r=t-1;
}
check(ans);
printf("%d\n%d\n",ans,ans);
inc(i,1,ans)printf("%d ",b[i].num);
return 0*putchar('\n');
}
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly ti milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.
Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer ai to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than ai problems overall (including problem i).
Formally, suppose you solve problems p1, p2, ..., pk during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≤ apj.
You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do.
The first line contains two integers n and T (1 ≤ n ≤ 2·105; 1 ≤ T ≤ 109) — the number of problems in the exam and the length of the exam in milliseconds, respectively.
Each of the next n lines contains two integers ai and ti (1 ≤ ai ≤ n; 1 ≤ ti ≤ 104). The problems are numbered from 1 to n.
In the first line, output a single integer s — your maximum possible final score.
In the second line, output a single integer k (0 ≤ k ≤ n) — the number of problems you should solve.
In the third line, output k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the indexes of problems you should solve, in any order.
If there are several optimal sets of problems, you may output any of them.
5 300 3 100 4 150 4 80 2 90 2 300
2 3 3 1 4
2 100 1 787 2 788
0 0
2 100 2 42 2 58
2 2 1 2
In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points.
In the second example, the length of the exam is catastrophically not enough to solve even a single problem.
In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.