zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    TeamX 专为中小团队思考的...团队协作工具
    8 月直播课抢先看 | 代码质量实战 + 微服务项目实战课程报名中
    CODING DevOps 代码质量实战系列第一课,本周开讲!
    CODING 现已支持墨刀原型引入
    CODING 企业微信小程序上线了
    CODING DevOps + Nginx-ingress 实现自动化灰度发布
    第二届腾讯运维技术开放日来啦!
    前端智造,内容新生
    kafka的特性初探
    弄懂一致性哈希后我打通了redis分区集群的原理
  • 原文地址:https://www.cnblogs.com/52dxer/p/10552734.html
Copyright © 2011-2022 走看看