算法竞赛进阶指南 0x00 贪心

AcWing 110. 防晒

贪心,这里引用AcWing大雪菜的贪心证明。
算法竞赛进阶指南 0x00 贪心_第1张图片

//#define LOCAL
#include 
using namespace std;
#define DBG printf("%d %s\n",__LINE__,__FUNCTION__)
#define CLOSE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define abs(x) ((x) >= 0 ? (x) : -(x))
#define LF putchar('\n')
#define SP putchar(' ')
#define eb emplace_back
#define mk make_pair
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define eps 1e-6
#define ri register int
#define rl register long long
#define LL long long
#define ULL unsigned long long
 
template<typename T> 
void read(T &x) {x = 0;char ch = getchar();LL f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args> 
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
 
const LL MOD = 1e9 + 7;
const int MAXN = 2550;

int n, m, spf[MAXN], num[MAXN];

struct node{
    int l, r;
    bool operator < (const node a) {
        if(a.l == l) {
            return r > a.r;
        }
        return l > a.l;
    }
}Node[MAXN];

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    read(n, m);
    for(ri i = 1; i <= n; ++i) {
        read(Node[i].l, Node[i].r);
    }
    for(ri i = 1; i <= m; ++i) {
        read(spf[i], num[i]);
    }
    sort(Node + 1, Node + n + 1);
    int ans = 0;
    for(ri i = 1; i <= n; ++i) {
        int maxn = -1, pos = 0;
        for(ri j = 1; j <= m; ++j) {
            if(spf[j] >= Node[i].l && spf[j] <= Node[i].r && maxn < spf[j] && num[j] > 0) {
                pos = j, maxn = spf[j];
            }
        }
        if(pos) {
            num[pos]--;
            ans++;
        }
    }
    write(ans), LF;
    return 0;
}

你可能感兴趣的:(算法竞赛进阶指南)