zoukankan      html  css  js  c++  java
  • 洛谷 桶哥的问题——送桶——题解

     

    一读题,发现与贪心中的任务调度有点类似。保证答案大于等于零,言外之意即为所有任务都可以在合法时间内完成。那么只要按照任务调度的思路做就行了:

    用结构体(方便sort)数组t读入所有ai、bi后按照结束时间从大到小排序。设ans为答案,i为当前要处理的任务在排序后的编号。ans初始为t[1].b,i=1,2,…,n。

    对于每个i:1、若ans比第i号任务的截止时间晚,则让ans等于该任务的截止时间;

          2、ans-=第i号任务的耗时。

    最后ans的值即为答案。

    AC代码:

     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 }

     

  • 相关阅读:
    LeetCode Subsets II
    LeetCode Rotate Image
    LeetCode Palidrome Number
    LeetCode Generate Parentheses
    LeetCode Maximum Subarray
    LeetCode Set Matrix Zeroes
    LeetCode Remove Nth Node From End of List
    Linux Loop设备 使用
    Linux 文件系统大小调整
    LeetCode N-Queens II
  • 原文地址:https://www.cnblogs.com/InductiveSorting-QYF/p/10939668.html
Copyright © 2011-2022 走看看