P2782 友好城市
生活随笔
收集整理的這篇文章主要介紹了
P2782 友好城市
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
有一條橫貫東西的大河,河有筆直的南北兩岸,岸上各有位置各不相同的N個城市。北岸的每個城市有且僅有一個友好城市在南岸,而且不同城市的友好城市不相同。每對友好城市都向政府申請在河上開辟一條直線航道連接兩個城市,但是由于河上霧太大,政府決定避免任意兩條航道交叉,以避免事故。編程幫助政府做出一些批準和拒絕申請的決定,使得在保證任意兩條航道不相交的情況下,被批準的申請盡量多。
輸入輸出格式
輸入格式:
?
第1行,一個整數N,表示城市數。
第2行到第n+1行,每行兩個整數,中間用一個空格隔開,分別表示南岸和北岸的一對友好城市的坐標。
?
輸出格式:
?
僅一行,輸出一個整數,表示政府所能批準的最多申請數。
?
輸入輸出樣例
輸入樣例#1:?復制 7 22 4 2 6 10 3 15 12 9 8 17 17 4 2 輸出樣例#1:?復制 4說明
50% 1<=N<=5000,0<=xi<=10000
100% 1<=N<=2e5,0<=xi<=1e6
?
首先想的是二維
因為爆棧 n^2
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 2147483647 const ll INF = 0x3f3f3f3f3f3f3f3fll; #define ri register int template <class T> inline T min(T a, T b, T c) {return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) {return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) {return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) {return max(max(a, b), max(c, d)); } #define scanf1(x) scanf("%d", &x) #define scanf2(x, y) scanf("%d%d", &x, &y) #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X) #define pi acos(-1) #define me(x, y) memset(x, y, sizeof(x)); #define For(i, a, b) for (int i = a; i <= b; i++) #define FFor(i, a, b) for (int i = a; i >= b; i--) #define bug printf("***********\n"); #define mp make_pair #define pb push_back const int maxn = 10005; // name******************************* int t[5005];//映射 struct pos {int a,b; } p[5005]; int dp[5005][5005]; int ans=0; int n; // function****************************** bool cmp(pos x,pos y) {return x.a<y.a; } //*************************************** int main() { // ios::sync_with_stdio(0); // cin.tie(0); // freopen("test.txt", "r", stdin);// freopen("outout.txt","w",stdout);cin>>n;For(i,1,n){cin>>p[i].a>>p[i].b;}sort(p+1,p+1+n,cmp);For(i,1,n){dp[i][i]=max(dp[i][i],1);For(j,1,i){dp[i+1][j]=dp[i][j];if(p[i+1].b>p[j].b)dp[i+1][i+1]=max(dp[i+1][i+1],dp[i][j]+1);ans=max(ans,dp[i+1][j],dp[i+1][i+1]);}}cout<<ans;return 0; } RE+TLE?
壓成一維交
復雜度太大 TLE
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 2147483647 const ll INF = 0x3f3f3f3f3f3f3f3fll; #define ri register int template <class T> inline T min(T a, T b, T c) {return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) {return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) {return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) {return max(max(a, b), max(c, d)); } #define scanf1(x) scanf("%d", &x) #define scanf2(x, y) scanf("%d%d", &x, &y) #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X) #define pi acos(-1) #define me(x, y) memset(x, y, sizeof(x)); #define For(i, a, b) for (int i = a; i <= b; i++) #define FFor(i, a, b) for (int i = a; i >= b; i--) #define bug printf("***********\n"); #define mp make_pair #define pb push_back const int maxn = 10005; // name******************************* struct pos {int a,b; } p[200005]; int dp[200005]; int ans=0; int n; // function****************************** bool cmp(pos x,pos y) {return x.a<y.a; } //*************************************** int main() { // ios::sync_with_stdio(0); // cin.tie(0); // freopen("test.txt", "r", stdin);// freopen("outout.txt","w",stdout);cin>>n;For(i,1,n){scanf("%d %d",&p[i].a,&p[i].b);}sort(p+1,p+1+n,cmp);For(i,0,n)dp[i]=1;For(i,1,n){For(j,i+1,n){if(p[j].b>p[i].b)dp[j]=max(dp[j],dp[i]+1);}ans=max(ans,dp[i]);}cout<<ans;return 0; } TLE?
最后還是看題解發現原來是
最長上升子序列長度nlogn
#include<bits/stdc++.h> using namespace std; #define For(i,a,b) for(int i=a;i<=b;i++) #define FFor(i,a,b) for(int i=a;i>=b;i--) #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define mod 100000000 #define maxn 200005 #define inf 0x3f3f3f3fstruct node {int a,b; } p[maxn]; int f[maxn]; int n; bool cmp(node x,node y) {return x.a<y.a; } int s[maxn]; int top=0;int main() {cin>>n;For(i,1,n){cin>>p[i].a>>p[i].b;}sort(p+1,p+1+n,cmp);For(i,1,n){if(s[top]<p[i].b){s[++top]=p[i].b;}else{s[lower_bound(s+1,s+top+1,p[i].b)-s]=p[i].b;}}cout<<top;return 0; }?
轉載于:https://www.cnblogs.com/planche/p/8627402.html
總結
以上是生活随笔為你收集整理的P2782 友好城市的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最强数据库工具——IDEA
- 下一篇: c# json转对象