当前位置:网站首页>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
边栏推荐
- mysql ”Invalid use of null value“ 解决方法
- 648. Word replacement: the classic application of dictionary tree
- Cargo placement problem
- 数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
- [high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
- Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
- Leetcode simple question sharing (20)
- 内存溢出和内存泄漏的区别
- UML 顺序图(时序图)
- Learning breakout 2 - about effective learning methods
猜你喜欢
高等數學---第八章多元函數微分學1
Details of redis core data structure & new features of redis 6
供应链供需预估-[时间序列]
TPG x AIDU | AI leading talent recruitment plan in progress!
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
Hands on Teaching: XML modeling
Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
Supply chain supply and demand estimation - [time series]
Vmware共享主机的有线网络IP地址
最长上升子序列模型 AcWing 482. 合唱队形
随机推荐
属性关键字Aliases,Calculated,Cardinality,ClientName
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
.net core 关于redis的pipeline以及事务
Cargo placement problem
Evolution of customer service hotline of dewu
Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
数据流图,数据字典
Excellent open source system recommendation of ThinkPHP framework
118. 杨辉三角
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
UML sequence diagram (sequence diagram)
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
Common response status codes
接口自动化测试-接口间数据依赖问题解决
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
2022-7-6 Leetcode 977. Square of ordered array
Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
When FC connects to the database, do you have to use a custom domain name to access it outside?