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

 

转载于:https://www.cnblogs.com/liuweimingcprogram/p/6270808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值