首页 技术 正文
技术 2022年11月14日
0 收藏 697 点赞 3,810 浏览 2931 个字

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025

题目:

Problem DescriptionJGShining’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.

杭电1025Constructing Roads In JGShining's Kingdom

In order to build as many roads as possible, the young and handsome king of the kingdom – JGShining needs your help, please help him. ^_^

 InputEach 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. OutputFor 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 Input2
1 2
2 1
3
1 2
2 3
3 1 Sample OutputCase 1:
My king, at most 1 road can be built.

Case 2:
My king, at most 2 roads can be built.

Hint

Huge input, scanf is recommended.

思路:dp题+二分查找。。。依旧没独立做出来,好惨,最近几个dp题全挂==

  题意是找最长上升子序列,

  懒得写题解了,不是自己想出来的,上一篇写的比较好的题解:http://blog.csdn.net/dangwenliang/article/details/5728363

  这题有两种算法n^2和nlogn的,这题用前一个果断会超时。。。

  在二分法上卡半天,我也是服了我的智商。。

  

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; #define N 500009
struct point
{
int x,y;
}; struct point city[N];
int a[N]; bool cmp(point &a,point &b)
{
return a.x<b.x;
} int found(int x,int len)
{
int l,r,mid;
l = ;
r = len;
mid = (l+r)/;
while(l <= r)
{
if(a[mid] < x)
{
l = mid+; }
else if(a[mid] > x)
{
r = mid-;
}
mid = (l+r)/;
}
return l;
}
int main (void)
{
int n,k=;
while(scanf("%d",&n) == )
{
int len;
k++;
for(int i = ;i<=n;i++)
{
scanf("%d%d",&city[i].x,&city[i].y);
a[i]= N + ;
}
sort(city+,city+n+,cmp);
a[] = -;
a[] = city[].y;
len = ;
for(int i = ;i<=n;i++)
{
int j = found(city[i].y,len);
a[j] = city[i].y;
if(len < j)
len = j;
}
if(len == )
printf("Case %d:\nMy king, at most %d road can be built.\n\n",k,len);
else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",k,len);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896