当前位置:网站首页>最长上升子序列模型 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, 无需新建结构体。运行时间一致
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Transferring files between VMware and host
- Is the spare money in your hand better to fry stocks or buy financial products?
- 华为镜像地址
- 【日常训练--腾讯精选50】231. 2 的幂
- Flink | multi stream conversion
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- 请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
- Parameter keywords final, flags, internal, mapping keywords internal
- 118. Yanghui triangle
- 高等數學---第八章多元函數微分學1
猜你喜欢
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
Details of redis core data structure & new features of redis 6
Dry goods | summarize the linkage use of those vulnerability tools
2022-7-6 Leetcode 977.有序数组的平方
Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
带你掌握三层架构(建议收藏)
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
随机推荐
内存溢出和内存泄漏的区别
得物客服热线的演进之路
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
Clickhouse (03) how to install and deploy Clickhouse
【日常训练】648. 单词替换
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
Flink | multi stream conversion
华为镜像地址
[1] ROS2基础知识-操作命令总结版
Is the compass stock software reliable? Is it safe to trade stocks?
Thread pool reject policy best practices
C语言数组相关问题深度理解
566. 重塑矩阵
干货|总结那些漏洞工具的联动使用
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
. Net core about redis pipeline and transactions
How does MySQL control the number of replace?
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
ES日志报错赏析-Limit of total fields
toRaw和markRaw