当前位置:网站首页>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 
边栏推荐
- Local and overall differences between emergence and morphology
- Neural network learning notes
- Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
- Students, don't copy all my code, remember to change it, or we both want G
- Kangaroo cloud stack based on CBO in spark SQL optimization
- 4 search insertion location
- 14 check whether integers and their multiples exist
- IO流_字符流、IO流小结、IO流案例总结
- JVM judges that the object is dead, and practices verify GC recycling
- C语言 2:求三数字最大值,求三数字中间值,编写程序步骤
猜你喜欢

Use of beautifulsoup

Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review

SQL Server2000数据库错误

NFT leaderboard -nft real offer latest address: NFT leaderboard.com

2022牛客多校 (3)J.Journey

Use of pyquery

Introduction to software vulnerability analysis (I)

Derivation of the detailed expansion sto overlap integrals

What is the mystery of the gate of the meta universe?

背包模型 AcWing 1022. 宠物小精灵之收服
随机推荐
Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review
推导重叠积分的详细展开式 STO overlap integrals
A measurement method of 5g air interface one-way delay and its reliability
Neural network learning notes
15 design movie rental system
Internal and external troubles of digital collection NFT "boring ape" bayc
Introduction to software vulnerability analysis (I)
Application scenarios, key technologies and network architecture of communication perception integration
IMG SRC is empty or SRC does not exist, and the picture has a white border
如何组装一个注册中心
Kangaroo cloud stack based on CBO in spark SQL optimization
parsel的使用
发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
SQL Server2000数据库错误
The influence of the number of non-zero values in the picture on Classification
10 complete half of the questions
11 wrong set
Play with the cluster configuration center and learn about the Taier console
基于FPGA的ECG信号采集,存储以及传输系统verilog实现