hdu 4104 Discount

http://acm.hdu.edu.cn/showproblem.php?pid=4104

一开始还以为这题是背包,然后优化下这个背包,但是一直都优化不出来。

然后题解是直接模拟而已,唉

先从小到大排序后

设【L, R】表示前i个数能组合成这个区间里面的任何一个数。

那么,枚举一个数a[i + 1]进来的时候,他就能表示【a[i + 1], a[i + 1] + R】中的所有数字,

关键是R和a[i + 1]是否能合并而已。

判断一下就好,。注意这题p有可能是0

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e3 + 20;
vector<int>a;
int n;
void work() {
    a.clear();
    a.push_back(0);
    for (int i = 1; i <= n; ++i) {
        int x;
        scanf("%d", &x);
        if (x == 0) continue;
        a.push_back(x);
    }
    sort(a.begin(), a.end());
    if (a.size() == 1 || a[1] != 1) {
        printf("1\n");
        return;
    }
    int L = 1, R = a[1];
    for (int i = 2; i < a.size(); ++i) {
        int newL = a[i];
        int newR = a[i] + R;
        if (R + 1 >= newL) {
            R = newR;
        } else {
            cout << R + 1 << endl;
            return;
        }
    }
    cout << R + 1 << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    while (scanf("%d", &n) != EOF) work();
    return 0;
}
View Code

 

posted on 2017-01-10 21:08  stupid_one  阅读(160)  评论(0编辑  收藏  举报

导航