POJ--3614 Sunscreen(贪心)

记录
18:26 2024-2-15

http://poj.org/problem?id=3614

贪心法,将minspf从大到小排列,然后选取最大的spf

点击查看代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<stdio.h>
#include<string.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2505;
int C, L;
//int spf[MAXN], cover[MAXN];
typedef pair<int, int> pariInt;
// minspf maxspf;
vector<pariInt> cowSpf;
// spf cover
vector<pariInt> spf;

bool compare(pariInt lhs, pariInt rhs) {
    return lhs.first > rhs.first;
}

void solve() {
    int result = 0;
    //按minspf来排序 从大到小
    sort(cowSpf.begin(), cowSpf.end(), compare);
    //按spf值来排序 从大到小
    sort(spf.begin(), spf.end(), compare);
    // for(auto i : cowSpf) 
    //     cout << i.first << " " << i.second << endl;
    for(int i = 0; i < C; i++) {
        for(int j = 0; j < L; j++) {
            if(spf[j].second && spf[j].first >= cowSpf[i].first && spf[j].first <= cowSpf[i].second) {
                result++;
                spf[j].second--;
                break;
            }
        }
    }
    cout << result << endl;
}

int main() {
    cin >> C >> L;
    for(int i = 0; i < C; i++) {
        int min, max;
        cin >> min >> max;
        cowSpf.push_back(make_pair(min, max));
    }
    for(int i = 0; i < L; i++) {
        int s, cover;
        cin >> s >> cover;
        spf.push_back(make_pair(s, cover));
    }
    solve();
}
posted @ 2024-02-15 18:27  57one  阅读(6)  评论(0编辑  收藏  举报