Codeforces Round # 560 (Div. 3) F2. Microtransactions (complicated version) (二分)

Topic links: http://codeforces.com/contest/1165/problem/F2

Meaning of the questions: n kinds of items to purchase, each item need to buy a [i] pieces, the price of each item is 2bourle, there are m times promotions (di, ti): The first day items ti di value offers for 1bourle

You can get a day 1bourle, buying the minimum number of days required for all items.

Ideas: the number of articles is provided all CNT, a certain minimum number of days in [cnt, 2 * cnt] between, because the number of items is obtained with increasing number of days, so to find the optimal solution bipartite

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespaceSTD; 
typedef Long  Long LL;
 static  const  int N = 1005 ;
 static  const  int MAX_N = 2E5 + . 5 ;
 int A [MAX_N], B [MAX_N]; 
Vector < int > VEC [MAX_N << . 1 ];
 int n-;
 BOOL Judge ( int X, int CNT, Vector < int > VEC []) {
     int day = X;     // take several days (i.e. bourle takes as obtained 1bourle day) 
    for ( int I = . 1; I <= n-; I ++) B [I] = A [I];
     int CUR = 0 ;
     for ( int I = X; I> = . 1 ; Inc. (www.i-levelmedia.com)) {     // X days promotions 
        for ( int J = 0 ; J <VEC [I] .size (); ++ J) {
             int T = VEC [I] [J];
             the while (B [T] && Day) {      // as much as possible in Shipping purchased items 
                - B [T];
                 --cnt;   // Item Total 
                --day;   // number bourle decrease 
            } 
        } 
        the while(Day> = I) ++ CUR, --day;    // number borcle not exceed a few days, only to the excess portion of the article did not participate in promotions (herein is better controlled conditions, with also if line) 
    }
     return CUR> = 2 * CNT;   // item need not participate in promotions 2bourle 
}
 void Solve () {
 //     the freopen ( "input.txt", "R & lt", stdin);
 //     the freopen ( "output.txt", "W" , stdout);
     // iOS :: sync_with_stdio (to false); 
    int m;
     the while (Scanf ( " % D% D " , & n-, & m)! = the EOF) {
         int CNT = 0 ;
         for ( int I = 0; i < (MAX_N << 1); ++i) vec[i].clear();
        for(int i = 1; i <= n; ++i){
            scanf("%d", &a[i]);
            cnt += a[i];
        }
        for(int i = 0; i < m; ++i){
            int u, v;
            scanf("%d%d", &u, &v);
            vec[u].push_back(v);
        }
        int l = cnt, r = cnt << 1;
        int ans = 0;
        while(l <= r){
            int m = l + r >> 1;
            if(judge(m, cnt, vec)){
                r = m - 1;
                ans = m;
            }
            else l = m + 1;
        }
        printf("%d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/xorxor/p/10958911.html