《UNIX环境高级编程》笔记--STREAMS

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5098    Accepted Submission(s): 3004


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.
 

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
 


题目大意:给你n件事情,然后给这n件事情的截止日期和如果不在截至日期之前完成会被扣掉的分数,问你被扣分数的最小值。

解题思路:感觉贪心靠得住,首先对事情的分数排个序,分数大的尽量先排,如果当天排了就往前面排。我自己在下面证明了觉得没问题。实在排不进去就扣分。

题目地址:Doing Homework again

AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int visi[1005];
struct node
{
    int time;
    int score;
}nod[1005];

int cmp(node a,node b)
{
    if(a.score>b.score) return 1;
    if(a.score==b.score&&a.time>b.time) return 1;
    return 0;
}

int main()
{
    int tes,n;
    cin>>tes;

    int i,j;
    int res;
    while(tes--)
    {
        res=0;
        cin>>n;
        for(i=0;i<n;i++)
            scanf("%d",&nod[i].time);
        for(i=0;i<n;i++)
            scanf("%d",&nod[i].score);

        sort(nod,nod+n,cmp);
        memset(visi,0,sizeof(visi));

        for(i=0;i<n;i++)
        {
            int flag=0;
            j=nod[i].time;
            while(j)
            {
                if(!visi[j])
                {
                    visi[j]=1;
                    flag=1;
                    break;
                }
                j--;
            }
            if(!flag)
                res+=nod[i].score;
        }

        cout<<res<<endl;
    }
    return 0;
}

/*
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
*/



《UNIX环境高级编程》笔记--STREAMS

上一篇:权限灵活可配-简单理解


下一篇:Paip.最佳实践-- Buildin variale 内建变量 ,魔术变量,预定义变量,系统常量,系统变量 1