欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Save the Nature【Codeforces 1241 C】【二分】

程序员文章站 2024-03-17 16:52:40
...

Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) C


  题意:有两种销售模式,(没啥区别),一是利润x%,每a天受益;二是利润y%,每b天受益。我们有N天,N种原价,我们可以对这N种原价进行自拟定顺序来进行销售,于是,我们想知道,最少需要几天可以收益额达到k。

  思路:我们可以二分天数,然后让尽可能大的利润额去匹配尽可能大的原价,然后这里在二分答案中用一下快排就可以了(写了半天模拟最后发现竟然是一道二分QAQ大雾!)时间复杂度O(N logN logN)。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&( -x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define efs 1e-7
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 2e5 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
int N;
ll v[maxN], need;
pair<ll , ll> a[3];
ll f[maxN], g[maxN];
inline bool  cmp(ll e1, ll e2) { return e1 > e2; }
int main()
{
    int T; scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        for(int i=1; i<=N; i++) f[i] = 0;
        for(int i=1; i<=N; i++) scanf("%lld", &v[i]);
        for(int i=0; i<2; i++)
        {
            scanf("%lld%lld", &a[i].first, &a[i].second);
            for(int j=a[i].second; j<=N; j+=a[i].second)
            {
                f[j] += a[i].first;
            }
        }
        scanf("%lld", &need);
        sort(v + 1, v + N + 1, cmp);
        int l=1, r=N, mid, ans = -1;
        while(l <= r)
        {
            mid = (l + r) >> 1;
            for(int i=1; i<=mid; i++) g[i] = f[i];
            sort(g + 1, g + mid + 1, cmp);
            ll get = 0;
            for(int i=1; i<=mid; i++) get += g[i] * v[i] / 100LL;
            if(get >= need)
            {
                ans = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

相关标签: 二分答案