• Doing Homework again:贪心+结构体sort


    Doing Homework again

    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

    题目描述:t组数据,输入n,然后n个数代表要交作业的时间,后边n个数表示作业的分值,做完不扣分,在该交时没交上,要扣对应的分,求最优方案下的扣分。

          先按照分数排序,肯定先安排好分大的,然后再安排分小的。

    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    
    struct node {										//t代表时间,a代表分值
    	int t, a;
    }s[1010];
    
    int T, n, cnt;
    bool used[1010];									//标记某个时间段是否用了 
    bool cmp(node x, node y) {							
    	if (x.a == y.a)
    		return x.t<y.t;
    	else
    		return x.a>y.a;
    }
    
    int main() {
    	cin >> T;
    	while (T--) {
    		cin >> n;
    		for (int i = 1; i <= n; i++)
    			cin >> s[i].t;
    		for (int i = 1; i <= n; i++)
    			cin >> s[i].a;
    
    		sort(s + 1, s + n + 1, cmp);
    		memset(used, false, sizeof(used));				//不要忘了初始化
    		cnt = 0;
    
    		for (int i = 1; i <= n; i++) {					//枚举,先把分大的安排好
    			int ans = 1;
    			for (int j = s[i].t; j>0; j--) {			//如果分大的当前时间已经被安排了,那就向前找
    				if (!used[j]) {
    					used[j] = true;
    					ans = 0;
    					break;
    				}
    			}
    			if (ans)cnt += s[i].a;						//找不到,就只能加上
    		}
    
    		cout << cnt << "
    ";
    	}
    	return 0;
    }
    
  • 相关阅读:
    链接
    Oracle创建表空间
    C#中的全局异常捕捉
    软件架构入门
    Nginx安装及配置详解包括windows环境
    极路由4增强版(B70)HC5962离线ROOT通过Breed刷openwrt教程
    vue自定义全局指令v-emoji限制input输入表情和特殊字符
    【vue】@input
    【window】常用软件
    Vbox 虚拟机全屏
  • 原文地址:https://www.cnblogs.com/52dxer/p/10552734.html
Copyright © 2020-2023  润新知