码迷,mamicode.com
首页 > 其他好文 > 详细

ACM-HDU1789 Doing Homework again(又是贪心- -、)

时间:2015-01-24 14:34:22      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

D - Doing Homework again
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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. 
 

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<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct e
{
    int day;
    int score;
}a[1005];
int cmp(e a,e b)
{
    if(a.score==b.score) return a.day<b.day;
    else return a.score>b.score;
}
int main()
{
    int i,q,w,j,n,p,u,o;
    scanf("%d",&n);
    while(n--)
    {
    int q,b[1005];
    memset(b,0,sizeof(b));
    scanf("%d",&q);j=0;
    for(i=0;i<q;i++) scanf("%d",&a[i].day);
    for(i=0;i<q;i++) {scanf("%d",&a[i].score);j+=a[i].score;}
    sort(a,a+q,cmp);
    i=0;w=0;
for(i=0;i<q;i++)
    {   o=a[i].day;
        if(b[o]==0)
    b[o]=a[i].score;
     else for(u=o;u>0;u--) if(b[u]==0)
        {b[u]=a[i].score;break;} else ;
    }
    for(i=1;i<=1000;i++) w+=b[i];
    printf("%d\n",j-w);
    }return 0;
}

第一次用天数排序,到第三个测试数据就有问题,用分数排序还是有问题,那么到底用啥!?
好吧,最后是用分数排序,策略比排序重要,其实策略是变相的排序,按照题意,保留分数最大的天数,当分数依旧很大的天数和它重复,就往前排一天。比如第四天有7分和6分,就把7分放在第四天,6分放在第三天,第三天如果也有了就重复。做到最后都对了,却又WA无数次- -、只能说数据太狠了,我需要让b来记录第几天是多少分,但是如果天数太大,那么我需要找到的就多了,最后一个for循环于是到了1000.也算AC了。


ACM-HDU1789 Doing Homework again(又是贪心- -、)

标签:

原文地址:http://blog.csdn.net/hy1405430407/article/details/43084695

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!