1. 程式人生 > 實用技巧 >codeforces 1260 D 二分

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 '\n' #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; }