hdu 1025 dp 最长上升子序列

 1 //Accepted    4372 KB    140 ms
 2 //dp 最长上升子序列 nlogn
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 using namespace std;
 7 const int imax_n = 500005;
 8 int dp[imax_n];
 9 int d[imax_n];
10 int a[imax_n];
11 int n;
12 int len;
13 int max(int a,int b)
14 {
15     return a>b?a:b;
16 }
17 int binary_search(int k)
18 {
19     int l=0;
20     int r=len;
21     while (l<=r)
22     {
23         int mid=(l+r)/2;
24         if (d[mid]==k) return mid;
25         if (d[mid]>k) r=mid-1;
26         if (d[mid]<k) l=mid+1;
27     }
28     return l;
29 }
30 void Dp()
31 {
32     memset(dp,0,sizeof(dp));
33     memset(d,0,sizeof(d));
34     len=-1;
35     for (int i=0;i<n;i++)
36     {
37         int j=binary_search(a[i]);
38         //printf("j=%d\n",j);
39         if (j>len) len++;
40         dp[i]=j+1;
41         d[j]=a[i];
42     }
43     int ans=0;
44     for (int i=0;i<n;i++)
45     ans=max(ans,dp[i]);
46     if (ans==1)
47     {
48         printf("My king, at most 1 road can be built.\n");
49     }
50     else
51     {
52         printf("My king, at most %d roads can be built.\n",ans);
53     }
54 }
55 int main()
56 {
57     int t=0;
58     while (scanf("%d",&n)!=EOF)
59     {
60         int x,y;
61         for (int i=0;i<n;i++)
62         {
63             scanf("%d%d",&x,&y);
64             a[x-1]=y;
65         }
66         printf("Case %d:\n",++t);
67         Dp();
68         printf("\n");
69     }
70     return 0;
71 }

hdu 1025 dp 最长上升子序列

时间: 2024-10-24 20:08:21

hdu 1025 dp 最长上升子序列的相关文章

HDU 1069 dp最长递增子序列

B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1069 Appoint description: Description A group of researchers are designing an experiment to test the IQ of a monkey. They wi

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

poj1836——dp,最长上升子序列(lis)

poj1836——dp,最长上升子序列(lis) Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13767   Accepted: 4450 Description In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight

codeforces340D - Bubble Sort Graph dp + 最长上升子序列

题意:给你长为n的序列 ,每个节点都和在它前面且值比他大的点产生一条边,问你一个最大 两两点没有边的集合的 集合元素有多少 解题思路:想了半天才发现是最长上升子序列.. 解题代码: 1 // File Name: 340d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 12时31分24秒 4 5 #include<vector> 6 #include<list> 7 #include<map>

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible in

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

[ An Ac a Day ^_^ ] HDU 1257 基础dp 最长上升子序列

最近两天在迎新 看来只能接着水题了…… 新生培训的任务分配 作为一个有担当的学长 自觉去选了动态规划…… 然后我觉得我可以开始水动态规划了…… 今天水一发最长上升子序列…… kuangbin有nlogn的模板…… 自己写一发原来学的吧…… 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<string.h> 6

hdu 1160 排序 + 最长上升子序列

题意: 输出体重上升而速度下降的最长子序列 题意: 先按照结构体升序排序体重,之后用dp对速度求最长下降子序列即可. 代码: #include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <string> #include <vector> #include <cstdio> #include