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;
    }
    
  • 相关阅读:
    3星|简·雅各布斯《经济的本质》:经济遵循与自然一样的发展规律:分化与组合、多样性、共同发展
    3星|《陈志武金融通识课》:从金融的角度看历史、文化、商业
    2018左其盛差评榜(截至7月6日)
    2018左其盛好书榜(截至7月6日)
    哈佛多是标题党。6本哈佛书点评
    OPENGL画图类库
    Html,Css,Javascript及其他的注释方法详解
    C#画图解决闪烁问题
    C#ShowCursor光标的显示与隐藏
    C#实现GDI+基本图的缩放、拖拽、移动
  • 原文地址:https://www.cnblogs.com/52dxer/p/10552734.html
Copyright © 2011-2022 走看看