LOJ P10008 家庭作业 题解

每日一题 day45 打卡

Analysis

这道题跟LOJ P10004 一样,但是数据范围不同,不允许O(n²) 的贪心算法通过。

我们可以加一个limit 来判断这个截止期限已经不行了,所以以后出现截止期限<=limit的就不行了。

LOJ上数据很水,这一个小优化就可以通过此题。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define int long long
 6 #define maxn 1000000+10
 7 #define rep(i,s,e) for(register int i=s;i<=e;++i)
 8 #define dwn(i,s,e) for(register int i=s;i>=e;--i)
 9 using namespace std;
10 inline int read()
11 {
12     int x=0,f=1;
13     char c=getchar();
14     while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
15     while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
16     return f*x;
17 }
18 inline void write(int x)
19 {
20     if(x<0) {putchar('-'); x=-x;}
21     if(x>9) write(x/10);
22     putchar(x%10+'0');
23 }
24 int n,lim,ans;
25 int book[maxn];
26 struct node
27 {
28     int deadline,score;    
29 }x[maxn];
30 bool cmp(node x,node y)
31 {
32     return x.score>y.score;
33 }
34 signed main()
35 {
36     n=read();
37     rep(i,1,n) x[i].deadline=read(),x[i].score=read();
38     sort(x+1,x+n+1,cmp);
39     rep(i,1,n)
40     {
41         if(x[i].deadline<=lim) continue;
42         int flag=0;
43         dwn(j,x[i].deadline,1)
44             if(book[j]==0)
45             {
46                 book[j]=1;
47                 flag=1;
48                 ans+=x[i].score;
49                 break;
50             }
51         if(flag==0) lim=x[i].deadline;
52     }
53     write(ans);
54     return 0;
55 }

请各位大佬斧正(反正我不认识斧正是什么意思)

猜你喜欢

转载自www.cnblogs.com/handsome-zyc/p/11899688.html
今日推荐