Los Columbia Valley barrels questions - send barrels - solution to a problem

 

A reading problem, find and task scheduling greedy in somewhat similar. To ensure that the answer is greater than zero, the implication is, all tasks can be completed within the legal time. So do as long as the idea of ​​task scheduling on the line:

A structure (convenience sort) t array reads all ai, bi after the end time according to the descending order. Ans set up as an answer to the current task i to be processed in a number sorted. ans initially t [1] .b, i = 1,2, ..., n.

For each i: 1, if ans later than the deadline for the task No. i, then let ans equal to the task deadline;

      2, ans- = i-th time-consuming task.

Ans is the final value of the answer.

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cctype>
 6 using namespace std;
 7 struct tong{
 8     int a,b;
 9 }t[1000001];
10 int ans;
11 char ch;
12 inline int read()
13 {
14     ans=0;
15     ch=getchar();
16     while(!isdigit(ch)) 
17     ch=getchar();
18     while(isdigit(ch)) ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
19     return ans;
20 }
21 inline bool cmp(tong a,tong b)
22 {
23     return a.b>b.b;
24 }
25 int main()
26 {
27     int n;
28     n=read();
29     for(register int i=1;i<=n;i++)
30     {
31         t[i].a=read();t[i].b=read();
32     }
33     sort(t+1,t+n+1,cmp);
34     ans=t[1].b;
35     for(register int i=1;i<=n;i++)
36     {
37         if(ans>t[i].b) ans=t[i].b;
38         ans-=t[i].a;
39     }
40     printf("%d",ans);
41     return 0;
42 }

 

Guess you like

Origin www.cnblogs.com/InductiveSorting-QYF/p/10939668.html