当前位置:网站首页>Longest ascending subsequence model acwing 1012. Sister Cities
Longest ascending subsequence model acwing 1012. Sister Cities
2022-07-27 11: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 
边栏推荐
- Object array de duplication
- Introduction to software vulnerability analysis (I)
- 学习笔记-简易服务器实现
- How to assemble a registry
- 49字母异位分组和242有效的字母异位词
- Play with the cluster configuration center and learn about the Taier console
- 背包模型 AcWing 1024. 装箱问题
- MySQL installation (RPM package)
- Regular form form judgment
- 11 wrong set
猜你喜欢

背包模型 AcWing 1024. 装箱问题

Delete in MySQL: the difference between delete, drop and truncate

迭代次数和熵之间关系的一个验证试验

img src为空或者src不存在,图片出现白色边框

Kangaroo cloud stack based on CBO in spark SQL optimization

Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached

BeautifulSoup的使用

Use of beautifulsoup

Antd table+checkbox default value display

NFT leaderboard -nft real offer latest address: NFT leaderboard.com
随机推荐
SQL Server2000数据库错误
12 is at least twice the maximum number of other numbers
Kangaroo cloud stack based on CBO in spark SQL optimization
Antd table+checkbox default value display
How to build a data index system is the most effective. It will take you a quick start from 0 to 1
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
黑白像素分布对迭代次数的影响
正则form表单判断
Analysis of new communication security risks brought by quantum computer and Countermeasures
学习笔记-uni-app
迭代次数的差异与信息熵
洛谷P1441 砝码称重
Sorry, you guys have something to deal with in the bank recently, which has been delayed
[FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
tensorflow运行报错解决方法
Use of parsel
JVM judges that the object is dead, and practices verify GC recycling
Introduction to software vulnerability analysis (I)
c语言指针函数和函数指针的辨析