洛谷 P2920 [USACO08NOV]时间管理Time Management

传送门

题目大意:

每个工作有截至时间和耗费时间,n个工作求最小开始时间。

题解:

贪心

从n-1安排,让结束时间尽量的晚。

注意:优先级

cout<<st<0?-1:st;  (X)

cout<<(st<0?-1:st);'

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1090
using namespace std;

int n,st;

struct T{
    int t,s;
}w[N];

bool cmp(T a,T b){
    return a.s>b.s;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&w[i].t,&w[i].s);
    sort(w+1,w+n+1,cmp);
    st=w[1].s-w[1].t;
    for(int i=2;i<=n;i++){
        int ed=min(w[i].s,st);
        st=ed-w[i].t;
    }
    //printf("%d
",st<0?-1:st);
    cout<<(st<0?-1:st);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zzyh/p/7792399.html