快排 贪心 hdu 1789
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4263 Accepted Submission(s): 2487
Problem Description
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.
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
解题思路:把作业先按扣分大小排序,如果分数一样,按截止时间从前往后排序,按照贪心的思路,从截止日期往前找没有占用掉的时间,如果没有找到,则加到扣分里去;
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int t,v;
};
bool cmp(node a,node b)
{
return a.v>b.v;
}
node a[100005];
int b[100005];
int main()
{
int i,j,k,m,n,p,q,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&a[i].t);
int all=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i].v);
all+=a[i].v;
}
sort(a+1,a+n+1,cmp);
int sum=0;
memset(b,0,sizeof(b));
for(i=1;i<=n;i++)
{
m=a[i].t;
for(j=m;j>=1;j--)if(b[j]==0)break;
if(j>0)b[j]=1,sum+=a[i].v;
}
printf("%d\n",all-sum);
}
return 0;
}
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int t,v;
};
bool cmp(node a,node b)
{
return a.v>b.v;
}
node a[100005];
int b[100005];
int main()
{
int i,j,k,m,n,p,q,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&a[i].t);
int all=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i].v);
all+=a[i].v;
}
sort(a+1,a+n+1,cmp);
int sum=0;
memset(b,0,sizeof(b));
for(i=1;i<=n;i++)
{
m=a[i].t;
for(j=m;j>=1;j--)if(b[j]==0)break;
if(j>0)b[j]=1,sum+=a[i].v;
}
printf("%d\n",all-sum);
}
return 0;
}

浙公网安备 33010602011771号