Codeforces1260D (simple binary)

The meaning of problems: there are m soldiers, t seconds, you want to bring as many soldiers from 0 to n + 1, and they can not be killed. On the way there are some traps, d [i] will kill smaller capacity than its soldiers, the trap position l [i], when you come to r [i] it can be removed. Each time you can move left or right. He will not be killed by a trap, the trap can go back again dismantled troops.

 

              (1): First, a very simple idea is great soldiers with ai [i], so a small band of soldiers more cost-effective than the ai [i], this should think you can know.

              (2): Since this is obviously a monotonic with l + 1 before time it will take a soldier must be greater than equal to the band before the soldiers l it takes time. So this way we can know f (l) (l front with l soldiers) on the l variable time spent is not reduced, then we can use half to do, this is actually very obvious, half monotonous of addressing (sometimes difficult to explain the real, can only say that the title feel)

              (3): So then back to a new problem, when we brought so many soldiers in the future, how to verify it? This is a very simple interval coverage problem, o (n) can be solved

Here is the code:

 

               

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <bitset>
 4 #include <algorithm>
 5 #include <iostream>
 6 typedef long long ll;
 7 using namespace std;
 8 const int maxn=201000;
 9 int m,n,k,t;
10 int ai[maxn];
11 struct node{
12     int li,ri,wi;
13 };
14 node sum[maxn];
15 
16 bool cmp(node n1,node n2){
17     return n1.li<n2.li;
18 }
19 
20 bool scmp(int a,int b){
21     return a>b;
22 }
23 
24 void init(){
25     scanf("%d%d%d%d",&m,&n,&k,&t);
26     for(int i=1;i<=m;i++) scanf("%d",&ai[i]);
27     sort(ai+1,ai+m+1, SCMP);
 28      for ( int I = . 1 ; I <= K; I ++) Scanf ( " % D% D% D " , & SUM [I] .li, & SUM [I] .ri, & SUM [I]. Wi);
 29      Sort (SUM + . 1 , SUM + K + . 1 , CMP);
 30  }
 31 is  
32  BOOL Check ( int MID) {
 33 is      // If no, return true directly; 
34 is      IF (MID == 0 ) return  true ;
 35      // this process is a big problem boundary
 36      // last outer loop count 
37 [      int RES = 0,l=-1,r=-1;
38     for(int i=1;i<=k;i++){
39        if(sum[i].wi<=ai[mid]) continue;
40        if(l==-1){ l=sum[i].li;r=sum[i].ri;continue; }
41        if(sum[i].li<=r){
42            r=max(sum[i].ri,r);
43        }else{
44            res+=(r-l+1);
45            r=sum[i].ri;l=sum[i].li;
46        }
47      }
 48      // this is outside the loop count of
 49      @ IF (! L = -. 1) is to prevent without operation outputs 
50      IF (L = -! . 1 ) RES + = (R & lt-L + . 1 );
 51 is      return RES * 2 + (n-+ . 1 ) <= T;
 52 is  }
 53 is  
54 is  void Solve () {
 55      // half wrong not set boundaries, do not set the wrong! ! ! ! ! ! ! ! 
56 is      int LB = 0 , m = RB, ANS = . 1 ;
 57 is      the while (LB <= RB) {
 58          int MID = (LB + RB) / 2 ;
 59         IF (Check (MID)) {
 60              ANS = MID;
 61 is              LB = MID + . 1 ;
 62 is          } the else RB = mid- . 1 ;
 63 is      }
 64      the printf ( " % D \ n- " , ANS);
 65  }
 66  
67  int main ( ) {
 68      // read the data 
69      the init ();
 70      // process 
71 is      Solve ();
 72      return  0 ;
 73 is }
View Code

Guess you like

Origin www.cnblogs.com/pandaking/p/12033891.html