当前位置:网站首页>最长上升子序列模型 AcWing 1012. 友好城市
最长上升子序列模型 AcWing 1012. 友好城市
2022-07-07 12:08:00 【T_Y_F666】
最长上升子序列模型 AcWing 1012. 友好城市
原题链接
算法标签
DP 线性DP 最长上升子序列
思路
将友好城市一端进行排序, 另一端最长上升序列即为答案
代码
#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总代码
#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
双关键字排序, 使用pair, 无需新建结构体。运行时间一致
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- AI talent cultivation new ideas, this live broadcast has what you care about
- Beginner XML
- Laravel Form-builder使用
- Dry goods | summarize the linkage use of those vulnerability tools
- mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
- Clickhouse (03) how to install and deploy Clickhouse
- Help tenants
- 实现IP地址归属地显示功能、号码归属地查询
- 【网络安全】sql注入语法汇总
- flask session伪造之hctf admin
猜你喜欢

Evolution of customer service hotline of dewu

《厌女:日本的女性嫌恶》摘录
![[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?](/img/fb/17e029b1d955965d7e2e0f58701d91.png)
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?

Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)

AI talent cultivation new ideas, this live broadcast has what you care about

Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)

How to check the ram and ROM usage of MCU through Keil

Help tenants

2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置

2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
随机推荐
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
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)
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
toRaw和markRaw
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
The difference between memory overflow and memory leak
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
118. 杨辉三角
Is the compass stock software reliable? Is it safe to trade stocks?
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
[1] Basic knowledge of ros2 - summary version of operation commands
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
flask session伪造之hctf admin
566. Reshaping the matrix
Move base parameter analysis and experience summary
Es log error appreciation -limit of total fields
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
最长上升子序列模型 AcWing 1014. 登山