hdu 1025 lis 注意细节!!!【dp】
感觉这道题浪费了我半个小时的生命。。。。。。哇靠!原来输出里面当len=1时是road否则是roads!!!
其实做过hdu 1950就会发现这俩其实一样,就是求最长上升子序列。我用结构体记录要连线的两个city,对一个数组排序再求相应的另一个数组lis。 开始WA还以为我写错了, 造了数据测一下没错啊。。。又想是不是情况没考虑全,比如一个城市可以连好多城市,好多城市可以连一个城市???巴拉巴拉。。。后来发现只可以一对一啊。。。。死在这种细节上真的是欲哭无泪。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN = ;
struct Node{
int p,r;
};
Node a[MAXN];
int dp[MAXN],t[MAXN];
int len=; bool cmp(Node a,Node b)
{
if(a.p==b.p)
return a.r<b.r;
return a.p<b.p;
} int bin_search(int key)
{
int low,high,mid;
low=,high=len;
while(low<high)
{
mid=(low+high)>>;
if(t[mid]>=key)
high=mid;
else low=mid+;
}
return low;
} int main()
{
int N;
int cas=;
while (scanf("%d", &N) == )
{
for (int i = ; i < N; i++) {
scanf("%d%d",&a[i].p,&a[i].r);
dp[MAXN]=INF;
}
sort(a,a+N,cmp);
len=;
t[]=a[].r;
for(int i=;i<N;i++){
if(a[i].r>t[len-])
t[len++]=a[i].r;
else{
int pos=bin_search(a[i].r);
t[pos]=a[i].r;
}
}
if(len==) printf("Case %d:\nMy king, at most %d road "
"can be built.\n\n",cas++,len);
else
printf("Case %d:\nMy king, at most %d roads "
"can be built.\n\n",cas++,len);
}
return ;
}
hdu 1025 lis 注意细节!!!【dp】的更多相关文章
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- HDU 1025 (LIS+二分) Constructing Roads In JGShining's Kingdom
这是最大上升子序列的变形,可并没有LIS那么简单. 需要用到二分查找来优化. 看了别人的代码,给人一种虽不明但觉厉的赶脚 直接复制粘贴了,嘿嘿 原文链接: http://blog.csdn.net/i ...
- Hdu 1025(LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- HDU 4521 间隔》=1的LIS 线段树+dp
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11991119 题意: n个数 d个距离 下面n个数的序列,求序列中的最长单调递增 ...
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- Leetcode561.Array Partition I数组拆分1
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. 示例 ...
- 在Ubuntu Server 14.04上源码安装Odoo 9.0
1. 更新Ubuntu服务器软件源 sudo apt-get update #更新软件源 sudo apt-get dist-upgrade #更新软件包,自动查找依赖关系 sudo shutdown ...
- [转]JS设计模式-单例模式(二)
单例模式是指保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式是一种常用的模式,有一些对象往往只需要一个,比如线程池.全局缓存.浏览器中的window对象等.在javaScript开发中 ...
- WWDC 上讲到的 Objective C / LLVM 改进
https://developer.apple.com/wwdc/videos/ Advances in Objective-C What's New in the LLVM Compiler 下面是 ...
- spring boot指定外部配置的坑
外部配置文件所在目录path/to/dir 指定--spring.config.location=path/to/dir 项目启动,没有使用任何配置文件,项目外和jar包中的都没有使用 这是因为其把p ...
- HTML:把两张图片并排(行)显示
<table><tr><td><img src=pic1.jpg border=0></td><td><img src=p ...
- free内存监控
语 法: free [-bkmotV][-s <间隔秒数>] 补充说明:free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等. 参 ...
- NOIP模拟17.9.22
NOIP模拟17.9.22 前进![问题描述]数轴的原点上有一只青蛙.青蛙要跳到数轴上≥
- 举例分析private的作用【c/c++学习】
抛砖引玉: c++中private的用处 我知道我们可以用 public 中的值,把private中的数据给提出来,但是还是搞不懂private该怎么用,或者说在一个具体程序中,private有什么用 ...
- transact和onTransact的区别
转:http://blog.csdn.net/sergeycao/article/details/52585411 谈transact 和onTransact需要先聊聊iBinder IBinder是 ...