ZOJ 2319 最長上升子序列並輸出組成該序列的元素編號
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2319
The most prestigious sports club in one city has exactly N members. Each of its members is strong and beautiful. More precisely, i-th member of this club (members being numbered by the time they entered the club) has strength Si and beauty Bi. Since this is a very prestigious club, its members are very rich and therefore extraordinary people, so they often extremely hate each other. Strictly speaking, i-th member of the club Mr X hates j-th member of the club Mr Y if Si <= Sj and Bi >= Bj or if Si >= Sj and Bi <= Bj (if both properties of Mr X are greater then corresponding properties of Mr Y, he doesn��t even notice him, on the other hand, if both of his properties are less, he respects Mr Y very much).
To celebrate a new 2005 year, the administration of the club is planning to organize a party. However they are afraid that if two people who hate each other would simultaneouly attend the party, after a drink or two they would start a fight. So no two people who hate each other should be invited. On the other hand, to keep the club prestige at the apropriate level, administration wants to invite as many people as possible.
Being the only one among administration who is not afraid of touching a computer, you are to write a program which would find out whom to invite to the party.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line of the input file contains integer N - the number of members of the club. (2 <= N <= 100 000). Next N lines contain two numbers each - Si and Bi respectively (1 <= Si, Bi <= 109).
Output
On the first line of the output file print the maximum number of the people that can be invited to the party. On the second line output N integers - numbers of members to be invited in arbitrary order. If several solutions exist, output any one.
Sample Input
1
4
1 1
1 2
2 1
2 2
Sample Output
2
1 4
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=100005;
struct note
{
int x,y,ip;
bool operator < (const note &a) const
{
if(x==a.x)
return y>a.y;
return x<a.x;
}
}point[N];
int dp[N],pre[N],n,T,k,l,r,m;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
point[i].ip=i;
}
sort(point+1,point+n+1);
dp[1]=1;
k=1;
for(int i=2;i<=n;i++)
{
if(point[i].y>point[dp[k]].y)
{
pre[i]=dp[k];
dp[++k]=i;
}
else
{
l=1,r=k;
while(l<r){
m=(l+r)>>1;
if(point[dp[m]].y<point[i].y)l=m+1;
else r=m;
}
dp[l]=i;
pre[i]=dp[l-1];
}
}
printf("%d\n",k);
int i=dp[k--];
printf("%d",point[i].ip);
while(k--)
{
i=pre[i];
printf(" %d",point[i].ip);
}
printf("\n");
}
return 0;
}
相關文章
- 最長上升子序列
- 線性dp:最長上升子序列
- 動態規劃:最長上升子序列動態規劃
- 最長上升子序列動態規劃動態規劃
- 動態規劃-最長上升子序列模型動態規劃模型
- 最長上升子序列(LIS)的兩種演算法演算法
- 動態規劃7:最長上升子序列LIS動態規劃
- 線性dp--最長上升子序列變形
- 動態規劃求解最長上升子序列問題動態規劃
- [線性dp] 合唱隊形(最長上升子序列模型)模型
- 最長上升子序列LIS 詳解+變形+擴充
- 最長公共子序列
- DP筆記最長上升子序列(LIS)以及零件分組問題筆記
- 【部分轉載】:【lower_bound、upperbound講解、二分查詢、最長上升子序列(LIS)、最長下降子序列模版】
- 輸入n個元素組成的序列s,你需要找出一個乘積最大的連續子序列
- 最長公共子序列(JAVA)Java
- 【LeetCode動態規劃#14】子序列系列題(最長遞增子序列、最長連續遞增序列、最長重複子陣列、最長公共子序列)LeetCode動態規劃陣列
- LeetCode 1626. 無矛盾的最佳球隊---【動態規劃】最長上升子序列變換版-->最大上升子序列和LeetCode動態規劃
- Codeforces Round #323 (Div. 2) D LIS 最長上升子序列
- java 實現 最長公共子序列Java
- 最長公共子序列求方案數
- 線性dp:最長公共子序列
- 最長公共子序列的程式碼實現
- 動態規劃-最長公共子序列動態規劃
- 動態規劃——最長公共子序列動態規劃
- 最長等差數列;及子序列分析
- 演算法題:最長公共子序列演算法
- 北京大學郭煒-最長上升子序列 動態規劃講解動態規劃
- bzoj3173: [Tjoi2013]最長上升子序列(樹狀陣列)陣列
- 力扣1143. 最長公共子序列 動態規劃之最長公共子序列力扣動態規劃
- 動態規劃(最長公共子序列LCS)動態規劃
- LCS 演算法:Javascript 最長公共子序列演算法JavaScript
- NlogN 求最長不下降子序列(LIS)
- 最長遞增子序列
- Mysql實現自增長編號,日期+序列MySql
- 最長公共子序列,遞迴簡單程式碼遞迴
- 微軟演算法面試題:如何找最長的增長子序列微軟演算法面試題
- 字串篇(python)—兩個字串的最長公共子序列字串Python