当前位置:网站首页>最长上升子序列模型 AcWing 1012. 友好城市
最长上升子序列模型 AcWing 1012. 友好城市
2022-07-27 10:35: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, 无需新建结构体。运行时间一致
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
- Time and power allocation method to ensure fairness in sensor fusion system
- Play with the cluster configuration center and learn about the Taier console
- Review and Prospect of encrypted traffic identification based on deep learning
- Integrated design of communication perception based on CSI: problems, challenges and Prospects
- 迭代次数和熵之间关系的一个验证试验
- Problems and Countermeasures of minors' digital security protection
- Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated
- Yum source installation
- NFT leaderboard -nft real offer latest address: NFT leaderboard.com
猜你喜欢

Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated

DNS principle and resolution process
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

How to build a real-time development platform to deeply release the value of enterprise real-time data?

The second method of calculating overlapping integral

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

推导重叠积分的详细展开式 STO overlap integrals

IMG SRC is empty or SRC does not exist, and the picture has a white border

如何创建一个带诊断工具的.NET镜像

Use of beautifulsoup
随机推荐
Cancer DDD
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
Substr and substring function usage in SQL
Wilderness search --- search iterations
Object array de duplication
Tdengine business ecosystem partner recruitment starts
Use of parsel
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
tensorflow运行报错解决方法
Use of pyquery
antd中table hover上去的背景色样式修改
The difference of iteration number and information entropy
Problems and Countermeasures of minors' digital security protection
Neural network learning notes
DNS principle and resolution process
IO流_字符流、IO流小结、IO流案例总结
The article will not keep VIP charges all the time. It will be open for a period of time
A verification test of the relationship between iteration number and entropy
Overview of radar communication integrated waveform design
15th largest value of data flow