首页 > 代码库 > [luoguP2782] 友好城市(DP)

[luoguP2782] 友好城市(DP)

传送门

 

转化成 lis 后 n2 搞就行

 

——代码

技术分享
 1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4  5 int n, max, ans; 6 int f[10001]; 7 struct node 8 { 9     int a, b;10 }q[10001];11 12 inline int read()13 {14     int x = 0, f = 1;15     char ch = getchar();16     for(; !isdigit(ch); ch = getchar()) if(ch == -) f = -1;17     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - 0;18     return x * f;19 }20 21 inline int Max(int x, int y)22 {23     return x > y ? x : y;24 }25 26 inline bool cmp(node x, node y)27 {28     return x.a < y.a;29 }30 31 int main()32 {33     int i, j, x, y;34     n = read();35     for(i = 1; i <= n; i++)36     {37         q[i].a = read();38         q[i].b = read();39     }40     std::sort(q + 1, q + n + 1, cmp);41     for(i = 1; i <= n; i++)42     {43         max = 0;44         for(j = 1; j < i; j++)45             if(q[j].b < q[i].b)46                 max = Max(max, f[j]);47         f[i] = max + 1;48         ans = Max(ans, f[i]);49     }50     printf("%d\n", ans);51     return 0;52 }
View Code

 

[luoguP2782] 友好城市(DP)