Problem Description
JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.
Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.
With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.
Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II.
The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones.
But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.
For example, the roads in Figure I are forbidden.
In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
Input
Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.
Output
For each test case, output the result in the form of sample.
You should tell JGShining what's the maximal number of road(s) can be built.
Sample Input
2
1 2
2 1
3
1 2
2 3
3 1
Sample Output
Case 1:
My king, at most 1 road can be built.
Case 2:
My king, at most 2 roads can be built.
题意:有n条路,接下来有n行,每行有两个整数p,r,表示城市p到城市r可以修路,问在不每两条路都不想交的条件下最多能修几条路。
分析:LIS+二分(nlogn).(n^2)的超时
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int zu[50*10000],dp[50*10000];
int Search(int len,int k)
{
int R=len,L=1;
while(L<=R)
{
int mid=(L+R)>>1;
if(k<=dp[mid])
R=mid-1;
else
L=mid+1;
}
return L;
}
int main()
{
int n;
int T=1;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
zu[a]=b;
}
dp[1]=zu[1];
int len=1;
for(int i=2;i<=n;i++)
{
int j=Search(len,zu[i]);
dp[j]=zu[i];
if(j>len)
len++;
}
printf("Case %d:\n",T++);
if(len==1)
printf("My king, at most %d road can be built.\n\n",len);
else
printf("My king, at most %d roads can be built.\n\n",len);
}
}
'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connecting the ports of two functional blocks cross each other all over the place. At this late stage of the process, it is too
expensive to redo the routing. Instead, the engineers have to bridge the signals, using the third dimension, so that no two signals cross. However, bridging is a complicated operation, and thus it is desirable to bridge as few signals as possible. The call for a computer program that finds the maximum number of signals which may be connected on the silicon surface without rossing each other, is imminent. Bearing in mind that there may be housands of signal ports at the boundary of a functional block, the problem asks quite a lot of the programmer. Are you up to the task?
Figure 1. To the left: The two blocks' ports and their signal mapping (4,2,6,3,1,5). To the right: At most three signals may be routed on the silicon surface without crossing each other. The dashed signals must be bridged.
A typical situation is schematically depicted in figure 1. The ports of the two functional blocks are numbered from 1 to p, from top to bottom. The signal mapping is described by a permutation of the numbers 1 to p in the form of a list of p unique numbers in the range 1 to p, in which the i:th number pecifies which port on the right side should be connected to the i:th port on the left side.
Two signals cross if and only if the straight lines connecting the two ports of each pair do.
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
10
1
8
8
7
6
5
4
3
2
1
9
5
8
9
2
3
1
7
4
6
9
1
4
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int NB=40000+100;
int dp[NB],zu[NB];
int Search(int Len,int m)
{
int L=1,R=Len;
while(L<=R)
{
int mid=(L+R)>>1;
if(m<=dp[mid])
R=mid-1;
else
L=mid+1;
}
return L;
}
int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&zu[i]);
dp[1]=zu[1];
int len=1;
for(int i=2;i<=n;i++)
{
int j=Search(len,zu[i]);
dp[j]=zu[i];
len=max(len,j);
}
printf("%d\n",len);
}
}
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. TSample Inputhe second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.
Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
Ab3bd
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=5000+100;
#define eps 0.00001
int dp[2][N];
char str[N],str1[N];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
memset(dp,0,sizeof(dp));
scanf("%s",str);
for(int i=n-1,j=0; i>=0&&j<n; i--,j++)
str1[j]=str[i];
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(str[i-1]==str1[j-1])
dp[i%2][j]=max(dp[(i-1)%2][j-1]+1,dp[i%2][j]);///滚动数组
else
dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
}
printf("%d\n",n-dp[n%2][n]);
}
}
The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them.
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property.
A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.
Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.
Input
Input is terminated by end of file.
Output
For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.
Sample Input
ananas banana
pear peach
Sample Output
bananas
pearch
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
char str1[100+50],str2[100+50];
int F[100+0][100+50];
int dp[100+50][100+50],L1,L2;
void init()
{
L1=strlen(str1);
L2=strlen(str2);
//
memset(dp,0,sizeof(dp));
memset(F,0,sizeof(F));
//
for(int i=strlen(str1); i>0; i--)
str1[i]=str1[i-1];
for(int i=strlen(str2); i>0; i--)
str2[i]=str2[i-1];
//
for(int i=0;i<=L1;i++)
F[i][0]=1;
for(int i=0;i<L2;i++)
F[0][i]=2;
}
void fun(int i,int j)
{
if(i==0&&j==0)
return;
if(F[i][j]==1)
{
fun(i-1,j);
printf("%c",str1[i]);
}
else if(F[i][j]==2)
{
fun(i,j-1);
printf("%c",str2[j]);
}
else
{
fun(i-1,j-1);
printf("%c",str1[i]);
}
}
int main()
{
while(scanf("%s %s",str1,str2)==2)
{
init();
for(int i=1; i<=L1; i++)
for(int j=1; j<=L2; j++)
{
if(str1[i]==str2[j])
dp[i][j]=dp[i-1][j-1]+1;
else if(dp[i-1][j]>dp[i][j-1])
{
dp[i][j]=dp[i-1][j];
F[i][j]=1;
}
else
{
dp[i][j]=dp[i][j-1];
F[i][j]=2;
}
}
fun(L1,L2);
printf("\n");
}
}