1. 程式人生 > >CodeForces 931F Teodor is not a liar!

CodeForces 931F Teodor is not a liar!

max second -- HR ace mem type span spa

Teodor is not a liar!

題意:Teodor說他的圖片上沒有一個點被所有的線段包含,他把這個消息分給他的朋友Sasha, 但是Sasha不信, 所以給Teodor給出幾條染色片段, 讓Sasha來猜,現在求Sasha最多能猜多少次。

題解:對於每一個點, 找到從左到這個點的位置的最長非嚴格遞增子序列, 再找到從右到這個點的位置的最長肺炎個遞增子序列, 然後取最大那個就是答案了。 因為要猜的次數最多, 所以不能猜那些Teodor給的次數比旁邊前面(或後邊)小的點。

代碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
3 #define LL long long 4 #define ULL unsigned LL 5 #define fi first 6 #define se second 7 #define lson l,m,rt<<1 8 #define rson m+1,r,rt<<1|1 9 #define max3(a,b,c) max(a,max(b,c)) 10 const int INF = 0x3f3f3f3f; 11 const LL mod = 1e9+7; 12 typedef pair<int,int> pll; 13 const int
N = 100010; 14 int a[N], dp[N], cnt[N], pre[N], suf[N]; 15 int main(){ 16 int n, m, l, r; 17 scanf("%d%d",&n,&m); 18 for(int i = 1; i <= n; i++){ 19 scanf("%d%d",&l,&r); 20 cnt[l]++; 21 cnt[r+1]--; 22 } 23 for(int i = 1; i <= m; i++) 24 a[i] = a[i-1
]+cnt[i]; 25 memset(dp, INF, sizeof(dp)); 26 for(int i = 1; i <= m; i++){ 27 *upper_bound(dp, dp+m, a[i]) = a[i]; 28 pre[i] = lower_bound(dp, dp+m, INF) - dp; 29 } 30 memset(dp, INF, sizeof(dp)); 31 for(int i = m; i >= 1; --i){ 32 *upper_bound(dp, dp+m, a[i]) = a[i]; 33 suf[i] = lower_bound(dp, dp+m, INF) - dp; 34 } 35 int ans = 0; 36 for(int i = 1; i <= m; i++) ans = max(ans, pre[i]+suf[i+1]); 37 printf("%d",ans); 38 return 0; 39 }

CodeForces 931F Teodor is not a liar!