期末考试
时间限制:1000 ms | 内存限制:65535 KB
难度:2
- 描述
-
马上就要考试了,小T有许多作业要做,而且每个老师都给出来了作业要交的期限,如果在规定的期限内没交作业就会扣期末成绩的分数,假设完成每门功课需要一天的时间,你能帮助小T扣除的分数最小吗?
- 输入
- 输入n,表示n门功课(n<2000),接下来n行,每行两个数a,b,分别表示交作业的最后期限,迟交扣除的分数。
(以文件结尾) 输出 - 输出扣除的最小分数。 样例输入
-
3 3 10 3 5 3 1 3 1 6 3 2 1 3 7 1 3 4 2 6 1 4 7 2 6 4 5 3 4
样例输出 0 3 5心得体会: 跟杭电上 开门人关门人 有共通之处,就是直接比较一对(或更多)的数据,这就要用到 结构体了(很强大),接着 队列 使运算更快(比较的十分有逻辑)。以下是我的代码:
#include<cstdio> #include<queue> #include<algorithm> using namespace std; priority_queue< int ,vector<int>, greater <int> >s; struct node { int t,f; }c[2002]; bool cmp( node x,node y) { if(x.t < y.t ) return 1; if(x.t == y.t&& x.f < y.f) return 1; return 0; } int main() { int i,j,k,l,n,m; while(scanf("%d",&n)!=EOF) { while(!s.empty()) s.pop(); for(i=0;i<n;i++) scanf("%d %d",&c[i].t,&c[i].f); sort(c,c+n,cmp); int sum=0; for(i=0; i<n ;i++) { if(s.size() < c[i].t ) //s.size()可以理解为天数。如果当前天数小于最后完成的天数,证明还是有天数空间的,直接入队列 s.push(c[i].f); else //如果当前天数>=最后完成的天数,必须要扣除分数了,为了使得扣除的分数最小,比较s.top()和当前的c[i].cost,扣除最小的 { j=s.top(); if(j<c[i].f) { s.pop(); s.push(c[i].f); sum+=j; } else sum+=c[i].f; } } printf("%d\n",sum); } return 0; }
- 输入n,表示n门功课(n<2000),接下来n行,每行两个数a,b,分别表示交作业的最后期限,迟交扣除的分数。