美文网首页
杭电acm1789 Dong Homework again

杭电acm1789 Dong Homework again

作者: cwhong | 来源:发表于2018-06-28 09:28 被阅读0次

Doing Homework again

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

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

Solution

这是一个贪心问题,首先将作业按扣分大小从大到小排序先作扣分大的作业,然后从作业截止日期向前寻找空闲时间做作业

Code

/**
 * date:2017.11.21
 * author:孟小德
 * function:杭电acm1789
 *  Doing Homework again    贪心算法
 */
 
 
 
import java.util.*;
 
public class acm1789
{
    public static int[] date = new int[366];
 
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        int num = input.nextInt();
        boolean flag = false;
 
        for (int i=0;i<num;i++)
        {
            for (int j=0;j<366;j++)
            {
                date[j] = 0;
            }
            int N = input.nextInt();
            int[][] work = new int[N][2];
            int result = 0;
 
            // 输入作业截止时间
            for (int j=0;j<N;j++)
            {
                work[j][0] = input.nextInt();
            }
            //  输入作业未做扣分数
            for (int j=0;j<N;j++)
            {
                work[j][1] = input.nextInt();
            }
 
            sort(work);
            for (int j=0;j<N;j++)
            {
                flag = false;
                for (int k=work[j][0];k>0;k--)
                {
                    if (date[k] == 0)
                    {
                        date[k] = 1;
                        flag = true;
                        break;
                    }
                }
 
                if (flag == false)
                {
                    result += work[j][1];
                }
 
            }
            System.out.println(result);
 
        }
    }
 
    // 由扣分多少从大到小排序
    public static void sort(int[][] work)
    {
        boolean flag = true;
        int[] temp = new int[2];
        for (int i = 0;i<work.length && flag;i++)
        {
            flag = false;
            for (int j = 0;j<work.length-i-1;j++)
            {
                if (work[j][1] < work[j+1][1])
                {
                    temp[0] = work[j][0];
                    temp[1] = work[j][1];
 
                    work[j][0] = work[j+1][0];
                    work[j][1] = work[j+1][1];
 
                    work[j+1][0] = temp[0];
                    work[j+1][1] = temp[1];
 
                    flag = true;
                }
 
            }
        }
    }
}

相关文章

  • 杭电acm1789 Dong Homework again

    Doing Homework again Time Limit: 1000/1000 MS (Java/Other...

  • 2020-09-28

    joyside 与 Miumiu 《Dong dong dong》

  • 杭电助手

    杭电助手(服务号hduhelp,订阅号hduhelper)是隶属于杭州电子科技大学党委学工部的校级组织,我们有前端...

  • 杭电2015

    这道题看起来不复杂,但做起来还是挺费工夫的。里面要用很多的循环结构,很容易在些小地方出错。我就是因为那些小问题而搞...

  • 杭电打卡

    这题主要是数学方法求解,其他没什么难度,关键是得出递推公式。 假如第一个和最后一个格子能相同颜色,我们可以很快算出...

  • 杭电oj 第11页 java版答案

    杭电oj 第2000- 2099 题 全答案杭电oj 第十一页答案 具体路径在 src/main/java/com...

  • 你会在10秒爱上这些歌,然后单曲循环。

    Es rappelt im karton——Pixie Paris 开头富有旋律的DONG DONG DONG D...

  • dong

    不懂天的藍 不懂雨的美 不懂風的柔 不懂雷的烈

  • 手写Xpath

    xpath知识参考[参考文章2](https://www.cnblogs.com/dong-dong-dong/p...

  • 杭电ACM1001

    不再更新,杭电ACM的题转到csdn博客

网友评论

      本文标题:杭电acm1789 Dong Homework again

      本文链接:https://www.haomeiwen.com/subject/zdnwyftx.html