两条航线交叉的充要条件是x1>x2,y1<y2。以x为关键字排序以后,满足条件的序列一定是单调的……发现了什么?原来此题就是排序+最大单调子序列问题。
#include<iostream>
using namespace std;
const int MaxN = 2004;
typedef struct harbor{int A,B;}harbor;
void Sort(harbor s[MaxN],int harbors);
int main()
{
harbor s[MaxN];
int DP[MaxN];
int harbors,length,k,i,j,temp;
while(cin>>harbors>>length>>k)
{
for(i=0;i<harbors;i++) cin>>s[i].A>>s[i].B;
Sort(s,harbors);
for(i=0;i<harbors;i++)
{
temp = 1;
for(j=0;j<i;j++)
if(s[i].B > s[j].B && DP[j] >= temp)
temp = DP[j] + 1;
DP[i] = temp;
}
for(temp=DP[0],i=1;i<harbors;i++)
if(DP[i] > temp) temp = DP[i]; cout<<temp<<endl;
}
return 0;
}
void Sort(harbor s[MaxN],int harbors)
{
harbor temp;
int i,j,swap;
for(i=0;i<harbors;i++)
{
swap = i;
for(j=i+1;j<harbors;j++)
{
if(s[swap].A > s[j].A)
swap = j;
}
temp = s[i];
s[i] = s[swap];
s[swap] = temp;
}
}
using namespace std;
const int MaxN = 2004;
typedef struct harbor{int A,B;}harbor;
void Sort(harbor s[MaxN],int harbors);
int main()
{
harbor s[MaxN];
int DP[MaxN];
int harbors,length,k,i,j,temp;
while(cin>>harbors>>length>>k)
{
for(i=0;i<harbors;i++) cin>>s[i].A>>s[i].B;
Sort(s,harbors);
for(i=0;i<harbors;i++)
{
temp = 1;
for(j=0;j<i;j++)
if(s[i].B > s[j].B && DP[j] >= temp)
temp = DP[j] + 1;
DP[i] = temp;
}
for(temp=DP[0],i=1;i<harbors;i++)
if(DP[i] > temp) temp = DP[i]; cout<<temp<<endl;
}
return 0;
}
void Sort(harbor s[MaxN],int harbors)
{
harbor temp;
int i,j,swap;
for(i=0;i<harbors;i++)
{
swap = i;
for(j=i+1;j<harbors;j++)
{
if(s[swap].A > s[j].A)
swap = j;
}
temp = s[i];
s[i] = s[swap];
s[swap] = temp;
}
}