杭电acm1789 Dong Homework again

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;
                }
 
            }
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,438评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,868评论 0 23
  • 语文:复习了昨天错的字词。拔,满,是反复错的,我批评了孩子不用心,她很不服气。 阅读:她找同学借了《马小跳》,看得...
    薄荷家阅读 199评论 0 0
  • 三、成本、费用扣除的税务处理 四、计税成本的核算方法
    飞翔_9215阅读 128评论 0 0
  • 结束高中的忙碌生活,经过两个多月的停歇。如今,我已步入从有大学这个概念时起就憧憬的大学生活。军训,对于经历过初、高...
    哈哈看开就好阅读 251评论 0 1