洛谷 P1230 智力大冲浪(贪心)

题目链接:https://www.luogu.com.cn/problem/P1230

对于每一个时刻,肯定要让钱数最大的尽量做。可以先按钱数排序,然后倒序枚举每一个任务可以在哪个时刻完成,且时刻越晚越优。

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 int m,n;
 6 const int N=1005;
 7 struct node{
 8     int t,d;
 9 }a[N];
10 int vis[N];
11 bool cmp(node a,node b){
12     return a.d>b.d;
13 }
14 int main(){
15     scanf("%d%d",&m,&n);
16     for(int i=1;i<=n;i++) scanf("%d",&a[i].t);
17     for(int i=1;i<=n;i++) scanf("%d",&a[i].d);
18     sort(a+1,a+n+1,cmp);
19     for(int i=1;i<=n;i++){
20         for(int j=a[i].t;j>=1;j--){
21             if(!vis[j]){
22                 vis[j]=1;
23                 a[i].d=0;
24                 break;
25             }
26         }
27     }
28     for(int i=1;i<=n;i++) m-=a[i].d;
29     printf("%d",m);
30     return 0;
31 }
AC代码
原文地址:https://www.cnblogs.com/New-ljx/p/13416768.html