当前位置:网站首页>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 
边栏推荐
- How to check the ram and ROM usage of MCU through Keil
- Horizontal of libsgm_ path_ Interpretation of aggregation program
- Environment configuration
- IP address home location query
- Dry goods | summarize the linkage use of those vulnerability tools
- Did login metamask
- Supply chain supply and demand estimation - [time series]
- Oracle advanced (V) schema solution
- Help tenants
- Assign a dynamic value to the background color of DataGrid through ivalueconverter
猜你喜欢

Transferring files between VMware and host

Flask session forged hctf admin

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1

2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
![Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]](/img/d3/20674983717d829489149b4d3bfedf.png)
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
![Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]](/img/35/5224252624cc76f42cbd0fd5c81d8c.png)
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]

最长上升子序列模型 AcWing 1014. 登山

Vmware 与主机之间传输文件
随机推荐
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
libSGM的horizontal_path_aggregation程序解读
参数关键字Final,Flags,Internal,映射关键字Internal
Help tenants
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
手里的闲钱是炒股票还是买理财产品好?
Hands on Teaching: XML modeling
请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
Environment configuration of lavarel env
.net core 关于redis的pipeline以及事务
股票开户首选,炒股交易开户佣金最低网上开户安全吗
现在网上开户安全么?那么网上开户选哪个证券公司?
Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
Assign a dynamic value to the background color of DataGrid through ivalueconverter
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
118. 杨辉三角
Lavarel之环境配置 .env
. Net core about redis pipeline and transactions
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
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