Educational Codeforces Round 77 (Rated for Div. 2)D(二分+贪心)

这题二分下界是0,所以二分写法和以往略有不同,注意考虑所有区间,并且不要死循环。。。

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int m,n,k,t;
 5 int a[200007];
 6 int x[200007],y[200007],z[200007];
 7 int sum[200007];
 8 int check(int v){
 9     memset(sum,0,sizeof(sum));
10     for(int i=1;i<=k;++i)
11         if(z[i]>v)
12             ++sum[x[i]],--sum[y[i]+1];
13     int temp=0;
14     for(int i=1;i<=n;++i){
15         sum[i]+=sum[i-1];
16         if(sum[i])
17             temp+=2;
18     }
19     if(temp<=t)
20         return 1;
21     return 0;
22 }
23 int main(){
24     ios::sync_with_stdio(false);
25     cin.tie(NULL);
26     cout.tie(NULL);
27     cin>>m>>n>>k>>t;
28     for(int i=1;i<=m;++i)
29         cin>>a[i];
30     for(int i=1;i<=k;++i)
31         cin>>x[i]>>y[i]>>z[i];
32     t-=n+1;
33     sort(a+1,a+1+m);
34     int l=0,r=m;
35     int ans=0;
36     while(l<r){
37         int mid=(l+r+1)>>1;
38         if(check(a[m-mid+1])){
39             l=mid;
40             ans=max(ans,mid);
41         }
42         else
43             r=mid-1;
44     }
45     cout<<ans;
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11955436.html