codeforces 1260 D 二分

题意:给了m个士兵,每个士兵有个敏捷值。还有k个陷阱,每个陷阱有三个属性,l,r,d。分别为陷阱的位置,接触陷阱的位置,陷阱的难度(如果第i个陷阱未解除,敏捷值小于di的士兵走上去,会死亡)。只有你可以走过任意陷阱,当你走到ri位置,i陷阱解除。士兵只有和你处于同一位置的时候才可以移动,你一次可以移动一格。不让任何士兵死亡,在t次移动内最多可以带多少个士兵。

思路:二分答案x,计算最少需要多少步能带领x个士兵到n-1。画个图能发现最少步实际是个覆盖问题。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector> 
// #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sp ' '
#define endl '
'
#define inf  0x3f3f3f3f;
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
#define P pair<int, int>
#define fi first
#define se second
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define sca2(a,b) scanf("%d %d",&(a),&(b))
#define sca(a) scanf("%d",&(a));
#define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
#define scall(a) scanf("%lld",&(a));


using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}

const double Pi = acos(-1.0);
const double epsilon = Pi/180.0;
const int maxn = 2e5+10;
int n,m,k,t;
int a[maxn],l[maxn],r[maxn],d[maxn];
bool judge(int pos)
{
    int x = a[pos];
    std::vector<pair<int,int> > v;
    rep(i,1,k){
        if(d[i] <= x) continue;
        v.pb(mp(l[i],r[i]));
    }
    sort(v.begin(), v.end());
    int ed = 0;
    int sum = 0;
    for(auto i : v){
        if(i.se <= ed) continue;
        if(ed < i.fi) ed = i.fi-1;
        sum += i.se - ed;
        ed = i.se;
    }
    ll tmp = sum*2 + n+1;
    if(tmp <= t) return 1;
    else return 0;
//    return (sum*2 + n+1 <= t);
}
int main()
{
    //freopen("input.txt", "r", stdin);
    cin>>m>>n>>k>>t;
    rep(i,1,m) cin>>a[i];
    rep(i,1,k) cin>>l[i]>>r[i]>>d[i];
    sort(a+1,a+1+m, [](int &a, int &b){return (a > b);});
    int L = 1, R = m;
    int ans = 0;
    while(L <= R){
        int mid = (L+R)/2;
        if(judge(mid)){
            ans = max(ans,mid);
            L = mid+1;
        }
        else R = mid - 1;
    }
    cout<<ans<<endl;
}
原文地址:https://www.cnblogs.com/jrfr/p/13298282.html