现在的位置: 首页 > 综合 > 正文

hdu 1789 doing homework again

2013年03月14日 ⁄ 综合 ⁄ 共 2857字 ⁄ 字号 评论关闭

http://acm.hdu.edu.cn/showproblem.php?pid=1789

Doing Homework again

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

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.

思路,模拟,vector<int> vec[1001];  vec[i]保存期限i天的扣分,i从1到n遍历,若以某一天的期限为0,记录zero++,ruo大于1qie大于当前zero,则说明该天之前有写作业肯定不能完成,累加该天之前最小的,用优先级队列实现。

表示不理解的是在hdu上A了,在Hunnu的却一直WA

//过了HDU,却过不了hunnu,郁闷
#include<iostream>
#include<queue>
#include<cstdio>
#include<vector>
using namespace std;
struct node
{
    int day,score;
}home[1001];
vector< int > vec[1001];
int solve(int n)
{
    int ans=0,zero=0,i,non,j;
    priority_queue<int,vector<int>, greater<int> >q;
    for(i=1;i<=n;++i)
    {
        non=vec[i].size();
        if(non==0)
            zero++;
        else if(non==1)
            q.push(vec[i][0]);
        else
        {
            for(j=0;j<non;++j)
                q.push(vec[i][j]);
            if(non-1<=zero)
            {
                zero=zero-non+1;  //zero的更新,注意
                continue;
            }
            for(j=0;j<non-1-zero;++j)
            {
                ans+=q.top();
                q.pop();
            }
            zero=0;
        }
    }
    return ans;
}

int main()
{
    int t,n,i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        k=0;
        scanf("%d",&n);
        for(i=0,j=0;i<n;++i)
            scanf("%d",&home[i].day);
        for(i=0;i<n;++i)
            scanf("%d",&home[i].score);
        for(i=0;i<n;++i)
            if(home[i].day<=n)
                vec[home[i].day].push_back(home[i].score);
        printf("%d\n",solve(n));
        for(i=1;i<=n;++i)
            vec[i].clear();
    }
    return 0;
}


另一种思路,来自网上,将数据按score从大到小排列,score相等按deadline从小到大,然后贪心制定作业表,具体是指score[i]安排复习表在i前的非0的位置。
统计能获得的最大学分,与总学分相减记得到所扣成绩。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define MAXN 1005

using namespace std;

struct Node
{
    int deadline;
    int score;
    bool operator<(Node o) const
    {
        if(score == o.score)
        {
            return deadline < o.deadline;
        }
        else
        {
            return score > o.score;
        }
    }
} node[MAXN];

int map[MAXN];

void Set(int day, int score)
{
    while(day > 0)
    {
        if(map[day] == 0)
        {
            map[day] = score;
            return ;
        }
        else
        {
            day --;
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t --)
    {
        memset(map, 0, sizeof(map));
        int i, n;
        scanf("%d", &n);
        int total = 0;
        int max_deadline = -1;
        for(i = 0; i < n; i ++)
        {
            scanf("%d", &node[i].deadline);
            if(node[i].deadline > max_deadline)
            {
                max_deadline = node[i].deadline;
            }
        }
        for(i = 0; i < n; i ++)
        {
            scanf("%d", &node[i].score);
            total += node[i].score;
        }
        sort(node, node + n);
        for(i = 0; i < n; i ++)
        {
            Set(node[i].deadline, node[i].score);
        }
        int get = 0;
        for(i = 1; i <= max_deadline; i ++)
        {
            get += map[i];
        }
        printf("%d\n", total - get);
    }
    return 0;
}

抱歉!评论已关闭.