Constructing Roads In JGShining's Kingdom
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26560 Accepted Submission(s): 7548
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. ^_^
You should tell JGShining what's the maximal number of road(s) can be built.
2 1 2 2 1 3 1 2 2 3 3 1
Case 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads can be built.HintHuge input, scanf is recommended.
题意:
先给你一个数 n,表示 n 个贫困城市和 n 个富有城市,然后有 n 行,表示该编号的贫困城市与该编号的富有城市相连,然后问你在两条连线不能交叉的条件下,这样的连线最多能有多少条
题解:
以pp 表示贫困城市的编号,以 rr 表示富有城市的编号
其实在输入 n 行时,pp 是从 1 到 n 的,只有与之相连的 rr 才打乱了,那么,在纸上实现的时候,我们就能发现:由于 pp 是连续递增子序列,那么只要 rr 也是递增子序列,就可以符合题意,也就是说,只要求出 rr 的最大上升子序列,就可以完成该题。
这道题不难,希望看题解的童鞋们能自己完成。
注意:输出格式有点坑
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC万岁!!!!!!!!!!!!!!
const int maxn = 509000;
int maps[maxn] = {}, dp[maxn] = {};
int binary_searchs(int l, int r, int k)
{
if(l >= r) return l;
int mid = (l + r + 1) / 2;
if(dp[mid] <= k) return binary_searchs(mid, r, k);
else if(dp[mid] > k) return binary_searchs(l, mid - 1, k);
}
int main()
{
int n, kcase = 0;
while(~scanf("%d", &n))
{
int pp, rr;
for(int i = 1; i <= n; i++)
{
scanf("%d %d", &pp, &rr);
maps[pp] = rr;
dp[i] = 0;
}
dp[1] = maps[1];
int counter = 1;
for(int i = 2; i <= n; i++)
{
if(maps[i] > dp[counter])
{
dp[++counter] = maps[i];
//printf("dp[%d] = %d\n", counter, dp[counter]);
}
else
{
int kk = binary_searchs(1, counter, maps[i]);
if(dp[kk] < maps[i])
{
dp[kk + 1] = maps[i];
}
else dp[kk] = maps[i];
//printf("kk : dp[%d] = %d\n", kk, dp[kk]);
}
}
printf("Case %d:\n", ++kcase);
if(counter == 1)
{
printf("My king, at most 1 road can be built.\n\n");
}
else printf("My king, at most %d roads can be built.\n\n", counter);
}
}
/*
6
1 3
2 6
3 5
4 1
5 2
6 4
*/