当前位置:网站首页>最长上升子序列模型 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, 无需新建结构体。运行时间一致
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
- Is it safe to open an account online now? Which securities company should I choose to open an account online?
- THINKPHP框架的优秀开源系统推荐
- [1] Basic knowledge of ros2 - summary version of operation commands
- 为租客提供帮助
- 《厌女:日本的女性嫌恶》摘录
- Is the spare money in your hand better to fry stocks or buy financial products?
- Clickhouse (03) how to install and deploy Clickhouse
- Seven propagation behaviors of transactions
- Cesium 已知一点经纬度和距离求另一个点的经纬度
猜你喜欢
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
. Net core about redis pipeline and transactions
Excerpt from "misogyny: female disgust in Japan"
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
566. 重塑矩阵
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
Realize the IP address home display function and number home query
Custom thread pool rejection policy
Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
C语言数组相关问题深度理解
随机推荐
Laravel Form-builder使用
Beginner XML
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
648. 单词替换 : 字典树的经典运用
Supply chain supply and demand estimation - [time series]
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
Is the spare money in your hand better to fry stocks or buy financial products?
648. Word replacement: the classic application of dictionary tree
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
mysql ”Invalid use of null value“ 解决方法
How can the PC page call QQ for online chat?
flask session伪造之hctf admin
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
THINKPHP框架的优秀开源系统推荐
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
接口自动化测试-接口间数据依赖问题解决
得物客服热线的演进之路
干货|总结那些漏洞工具的联动使用