Luo Gu P2782-friendly city

Topic Portal

Problem-solving ideas:

The title of each pair of cities to make a pair, according to the North Shore city from small to large, seeking the city's longest south bank does not drop sequence.

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 int n,f[200001],tot,g[200001];
 8 struct kkk{
 9     int a,b;
10 }e[200001];
11 
12 inline bool cmp(kkk s,kkk d) {
13     return s.a < d.a;
14 }
15 
16 int main() {
17     scanf("%d",&n);
18     for(int i = 1;i <= n; i++)
19         scanf("%d%d",&e[i].a,&e[i].b);
20     sort(e+1,e+1+n,cmp);
21     f[++tot] = e[1].b;
22     for(int i = 1;i <= n; i++)
23         g[i] = e[i].b;
24     for(int i = 2;i <= n; i++) {
25         if(g[i] >= f[tot])
26             f[++tot] = g[i];
27         else {
28             int u = upper_bound(f+1,f+1+tot,g[i]) - f;
29             f[u] = g[i];
30         }
31     }
32     printf("%d",tot);
33     return 0;
34 }

 

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/12319692.html