# Doing homework again(贪心)

# Doing homework again(贪心)

 

题目链接:Click here~~

题意:

有 n 门作业,每门作业都有自己的截止期限,当超过截止期限还没有完成作业,就会扣掉相应的分数。问如何才能使扣分最少。

解题思路1:

把 n 门作业按分数从大到小排序,然后每次都把作业安排在离它的截止期限最近的一天(先安排在截止日期当天,如当天已有安排,则往前一天找),并把此天标记为已用,若不能安排,则扣分。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
bool vis[1000];
#define fre freopen("C:\\Users\\Dell\\Desktop\\in.txt", "r", stdin);
struct node{
	int dead;
	int sub;
	bool operator < (const node &a)const{
		return a.sub<sub;
	}
}stu[1005];
int main(){
	//fre;
	int t, n, totsub;
	cin >> t;
	while (t--){
		vis[0] = 1;
		totsub = 0;
		cin >> n;
		for (int i = 0; i<n; i++)cin >> stu[i].dead;
		for (int i = 0; i<n; i++)cin >> stu[i].sub, totsub += stu[i].sub;
		sort(stu, stu + n);
		for (int i = 0; i<n; i++){
			//int k = 0;
			for (int j = stu[i].dead; j >= 1; j--){
				if (vis[j] == 0){ vis[j] = 1; totsub -= stu[i].sub; break; }
			}
		}
		cout << totsub << endl;
		memset(vis, 0,sizeof(vis) );
	}
	return 0;
}
# Doing homework again(贪心)

解题思路2:

    先对日期从小到大排序,如果日期相同,则扣分多的排在前面。如果相同日期内有扣分多的,则就用前面做扣分少的作业的时间来做这门作业;如果没有比他小的,就扣这门作业的分。On,大大优于前面的算法。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<memory.h>
#include<queue>
#include <bits\stdc++.h>
using namespace std;
#define fre freopen("C:\\Users\\Dell\\Desktop\\in.txt", "r", stdin);
priority_queue<int, vector<int>, greater<int> >q;    //小的先出队列
struct in
{
	int d, s;//deadline,score
}c[1010];
bool cmp(in a, in b){
	return a.d<b.d;
}
int main()
{
	fre;
	int T, n, i, j, t, cnt, ans;
	scanf("%d", &T);
	while (T--){
		cnt = ans = 0;
		t = 1;
		while (!q.empty()) q.pop();
		scanf("%d", &n);
		for (i = 1; i <= n; i++) scanf("%d", &c[i].d);
		for (i = 1; i <= n; i++) scanf("%d", &c[i].s);
		sort(c + 1, c + n + 1, cmp);
		for (i = 1; i <= n; i++){
//放入从小到大排序的队列,等之后没时间做分值大的作业时,从队头(分值小的作业)开始放弃分数小的作业
			q.push(c[i].s);
//如果截止日期相同,也即某一天有不止一门课要交,则一定要从中选择一门放弃,选代价最小的,但是并未决
//定当天要做哪门
			if (c[i].d>=t)  t++;    
			else {                  
				ans += q.top(); q.pop();
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
# Doing homework again(贪心)

 

上一篇:1021 Fibonacci Again


下一篇:HDU 1702 ACboy needs your help again!(栈和队列的简单应用)