家庭作业
来源:互联网 发布:usb端口重置请求失败 编辑:程序博客网 时间:2024/04/26 14:06
Description
老师在开学第一天就把所有作业都布置了,每个作业如果在规定的时间内交上来的话才有学分。每个作业的截止日期和学分可能是不同的。例如如果一个作业学分为10,要求在6天内交,那么要想拿到这10学分,就必须在第6天结束前交。
每个作业的完成时间都是只有一天。例如,假设有7次作业的学分和完成时间如下:
作业号 1 2 3 4 5 6 7
期限 1 1 3 3 2 2 6
学分 6 7 2 1 4 5 1
最多可以获得15学分,其中一个完成作业的次序为2,6,3,1,7,5,4,注意可能d还有其他方法。
你的任务就是找到一个完成作业的顺序获得最大学分。
Input
第一行一个整数N,表示作业的数量。接下来N行,每行包括两个整数,第一个整数表示作业的完成期限,第二个数表示该作业的学分。
Output
输出一个整数表示可以获得的最大学分。保证答案不超过longint范围。
Sample Input
7
1 6
1 7
3 2
3 1
2 4
2 5
6 1
Sample Output
15
DataConstraint
Hint
【数据规模】
对于所有数据,N<=1000000,作业的完成期限均小于700000。
对于部分数据,N<=1000;
对于部分数据,N<=10000;
对于部分数据,N<=100000;
对于部分数据,作业的完成期限小于100;
对于部分数据,作业的完成期限小于1000;
分析:
一看题,我就有种熟悉的感觉(似曾相识啊!!!据说某学霸可以告诉你答案),?于是我又从贪心的角度思考,呵呵,好像贪心是可以对的,只需按时间排序,然后记录选了几个,如果在期限内就直接加入数组,如果不在就选数组内最小的数与之比较,将大的存入数组,最后求和即是答案。于是,我就加了堆来维护数组最小值。
程序:
const maxn=1000001;var a:array [0..maxn] of longint; x,y:array [0..maxn] of longint; ans,i,j,t,n:longint;procedure qsort(l,r:longint); var i,j,key,temp:longint; begin if l>=r then exit; i:=l;j:=r; key:=x[l+random(r-l+1)]; repeat while (x[i]<key) do inc(i); while (x[j]>key) do dec(j); if i<=j then begin temp:=x[i];x[i]:=x[j];x[j]:=temp; temp:=y[i];y[i]:=y[j];y[j]:=temp; inc(i);dec(j); end; until i>j; qsort(l,j); qsort(i,r); end;procedure insert1(x:longint);var i,temp:longint; begin inc(t); a[t]:=x; i:=t; while (i>1) and (a[i]>a[i shr 1]) do begin temp:=a[i]; a[i]:=a[i shr 1]; a[i shr 1]:=temp; i:=i shr 1; end; end;function delete1:longint;var i,temp,s:longint; flag:boolean;begin delete1:=a[1]; a[1]:=a[t]; dec(t); i:=1; flag:=true; while flag and (i shl 1<=t) do begin if (i shl 1+1>t) or (a[i shl 1]>a[i shl 1+1]) then s:=i shl 1 else s:=i shl 1+1; if a[s]>a[i] then begin temp:=a[i]; a[i]:=a[s]; a[s]:=temp; i:=s; end else flag:=false; end; end;begin readln(n); for i:=1 to n do readln(x[i],y[i]); qsort(1,n); j:=n; for i:=x[n] downto 1 do begin while (x[j]>=i) and (j>=1) do begin insert1(y[j]); dec(j); end; if t>=1 then ans:=ans+delete1; end; writeln(ans);end.
- 家庭作业
- 家庭作业
- 家庭作业
- 家庭作业几件事
- csapp2e家庭作业2.58
- csapp2e 家庭作业2.59
- csapp2e 家庭作业2.60
- csapp2e 家庭作业2.61
- csapp2e 家庭作业2.62
- csapp2e 家庭作业2.63
- csapp2e 家庭作业2.64
- csapp2e 家庭作业2.65
- csapp2e 家庭作业 2.66
- csapp2e 家庭作业 2.67
- csap2e 家庭作业 2.68
- csapp2e 家庭作业 2.69
- csapp2e 家庭作业 2.70
- csapp2e 家庭作业 2.71
- 欢迎使用CSDN-markdown编辑器
- 在WebView中使用JavaScript获取网页内容
- hdu4614 Vases and Flowers (简单线段树 + 二分)
- 提取不重复的整数
- 图-普利姆(Prime)算法
- 家庭作业
- HttpURLConnection GET 方式 请求网络数据
- 自定义标签在js拼html元素不起作用时的解决方案
- UVa 725 Division(例题7-1)
- cocos2dx3.12 cannot locate symbol "atof" referenced by "libcocos2djs.so"
- spark链接hbase
- POJ - 3185 The Water Bowls (反转)
- Mongodb的update用法
- ZooKeeper的一致性协议:Zab