# Doing homework again(贪心)

# Doing homework again(贪心)

 

Topic links: the Click here Wallpaper ~ ~

Meaning of the questions:

There are n door operations, every job has its own door deadline, when the deadline has not been completed more than a job, it will be deducted proportionately. Q. How can I make the least penalty points.

1 problem-solving ideas:

The n door operations by scores descending order, then every time the job from its scheduled deadline recent day (the day of the first arrangement, the same day as the deadline has been scheduled, the forward one day to find), and this day is marked as the use, if not arranged, it points.

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

2 problem-solving ideas:

    First of dates from small to large, if the date is the same, the more points standing in the front. If you have more points, then do a little time in front of the door deduction job to do this job the same date; if not younger than him, and divide it by the door buckle job. On, much better than previous algorithms.

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

 

Guess you like

Origin www.cnblogs.com/sstealer/p/11130930.html
Recommended