当前位置:网站首页>The longest ascending subsequence model acwing 1012 Sister cities
The longest ascending subsequence model acwing 1012 Sister cities
2022-07-07 14:13:00 【T_ Y_ F666】
Longest ascending subsequence model AcWing 1012. Friendly city
Original link
Algorithm tags
DP linear DP Longest ascending subsequence
Ideas
Sort friendly cities at one end , The longest ascending sequence at the other end is the answer 
Code
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 5005, INF = 0x3f3f3f3f;
int f[N], f1[N], a[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
struct Node{
int a,b;
}node[N];
bool cmp(Node A, Node B){
return A.b<B.b;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(),ans=0;
rep(i, 0, n){
node[i].a=read(),node[i].b=read();
}
sort(node, node+n, cmp);
rep(i, 0, n){
f[i]=1;
rep(j, 0, i){
if(node[j].a<node[i].a){
f[i]=max(f[i], f[j]+1);
}
}
ans=max(ans, f[i]);
}
printf("%lld\n", ans);
}
y Master code
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 5010;
int n;
PII city[N];
int f[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d%d", &city[i].first, &city[i].second);
sort(city, city + n);
int res = 0;
for (int i = 0; i < n; i ++ )
{
f[i] = 1;
for (int j = 0; j < i; j ++ )
if (city[i].second > city[j].second)
f[i] = max(f[i], f[j] + 1);
res = max(res, f[i]);
}
printf("%d\n", res);
return 0;
}
tips
Double keyword sorting , Use pair, There is no need to create a new structure . Consistent running time
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 请问指南针股票软件可靠吗?交易股票安全吗?
- Oracle advanced (V) schema solution
- MySQL "invalid use of null value" solution
- PC端页面如何调用QQ进行在线聊天?
- 2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
- Is it safe to open an account online now? Which securities company should I choose to open an account online?
- 2022-7-7 Leetcode 844. Compare strings with backspace
- c#通过frame 和 page 切换页面
- TPG x AIDU | AI leading talent recruitment plan in progress!
- 【网络安全】sql注入语法汇总
猜你喜欢

Redis 核心数据结构 & Redis 6 新特性详

The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)

"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
![[Reading stereo matching papers] [III] ints](/img/d3/4238432492ac3dc4ec14a971b8848d.png)
[Reading stereo matching papers] [III] ints

566. Reshaping the matrix

"New red flag Cup" desktop application creativity competition 2022

手把手教会:XML建模

.net core 关于redis的pipeline以及事务

常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码

最长上升子序列模型 AcWing 1014. 登山
随机推荐
供应链供需预估-[时间序列]
C # switch pages through frame and page
Leetcode simple question sharing (20)
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
Mysql怎样控制replace替换的次数?
Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
UML sequence diagram (sequence diagram)
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
属性关键字Aliases,Calculated,Cardinality,ClientName
3D detection: fast visualization of 3D box and point cloud
GVIM [III] [u vimrc configuration]
libSGM的horizontal_path_aggregation程序解读
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
648. Word replacement: the classic application of dictionary tree
IP and long integer interchange
Seven propagation behaviors of transactions
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
UML 顺序图(时序图)
IP address home location query full version