贪心算法J题
Posted 偶尔爆零的蒟蒻
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了贪心算法J题相关的知识,希望对你有一定的参考价值。
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework… Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
Output
For each test case, you should output the smallest total reduced score, one line per test case.
Sample Input
3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4
Sample Output
0
3
5
题意
作业有两个属性,最晚截止时间和未完成扣的分数。一天只能完成一门作业,如何安排做作业的顺序使扣分尽量少。
经典的截止时间问题
思路
设辅助数组,标记改天是否已使用。
先从扣分最多的开始,把他安排在最晚那天完成
如果那一天被占用,则减一天,再看是否占用
如果减为0,则必须要扣分了
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct work
int d,s;
w[1005];
bool cmp(work a,work b)
return a.s>b.s;
int date[1005];
int finddate(int x)
if(date[x]==-1)return x;
return finddate(date[x]);
int main()
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t,n,sum=0;
cin>>t;
while(t--)
cin>>n;
for(int i=0;i<n;i++)
cin>>w[i].d;
for(int i=0;i<n;i++)
cin>>w[i].s;
sum+=w[i].s;//总共要扣的分
sort(w,w+n,cmp);
memset(date,-1,sizeof(date));
for(int i=0;i<n;i++)
int t=finddate(w[i].d);
if(t>0)
sum-=w[i].s;//减去不用扣的分
date[t]=t-1;
cout<<sum<<endl;
sum=0;
以上是关于贪心算法J题的主要内容,如果未能解决你的问题,请参考以下文章