当前位置:网站首页>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 
边栏推荐
- Use of pyquery
- Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
- Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
- Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
- Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
- IO stream_ Character stream, IO stream summary, IO stream case summary
- Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
- What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
- Antd table+checkbox default value display
- I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
猜你喜欢

FAQs of "relay chain" and "dot" in Poka ecosystem

Derive the detailed expansion of STO double center kinetic energy integral

DNS principle and resolution process

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

49字母异位分组和242有效的字母异位词

2022牛客多校 (3)J.Journey

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?

Cancer DDD

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

背包模型 AcWing 1024. 装箱问题
随机推荐
The difference of iteration number and information entropy
An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
Play with the cluster configuration center and learn about the Taier console
pyquery 的使用
Object array de duplication
Use of parsel
Students, don't copy all my code, remember to change it, or we both want G
学习笔记-minio
Substr and substring function usage in SQL
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Shortest moving distance and entropy of morphological complex
[QNX Hypervisor 2.2用户手册]9.9 logger
IMG SRC is empty or SRC does not exist, and the picture has a white border
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
MIMO array 3D imaging technology based on mobile terminal
[QNX hypervisor 2.2 user manual]9.9 logger
Opengauss kernel analysis - statistics and row count estimation
Analysis of C language pointer function and function pointer
Self optimization of wireless cell load balancing based on machine learning technology
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?