https://codeforces.com/problemset/problem/1148/D
题目描述
You are given nn pairs of integers (a_1, b_1), (a_2, b_2), \ldots, (a_n, b_n)(a1,b1),(a2,b2),…,(an,bn) . All of the integers in the pairs are distinct and are in the range from 11 to 2 \cdot n2⋅n inclusive.
Let's call a sequence of integers x_1, x_2, \ldots, x_{2k}x1,x2,…,x2k good if either
- x_1 < x_2 > x_3 < \ldots < x_{2k-2} > x_{2k-1} < x_{2k}x1<x2>x3<…<x2k−2>x2k−1<x2k , or
- x_1 > x_2 < x_3 > \ldots > x_{2k-2} < x_{2k-1} > x_{2k}x1>x2<x3>…>x2k−2<x2k−1>x2k .
You need to choose a subset of distinct indices i_1, i_2, \ldots, i_ti1,i2,…,it and their order in a way that if you write down all numbers from the pairs in a single sequence (the sequence would be a_{i_1}, b_{i_1}, a_{i_2}, b_{i_2}, \ldots, a_{i_t}, b_{i_t}ai1,bi1,ai2,bi2,…,ait,bit ), this sequence is good.
What is the largest subset of indices you can choose? You also need to construct the corresponding index sequence i_1, i_2, \ldots, i_ti1,i2,…,it .
输入格式
The first line contains single integer nn ( 2 \leq n \leq 3 \cdot 10^52≤n≤3⋅105 ) — the number of pairs.
Each of the next nn lines contain two numbers — a_iai and b_ibi ( 1 \le a_i, b_i \le 2 \cdot n1≤ai,bi≤2⋅n ) — the elements of the pairs.
It is guaranteed that all integers in the pairs are distinct, that is, every integer from 11 to 2 \cdot n2⋅n is mentioned exactly once.
输出格式
In the first line print a single integer tt — the number of pairs in the answer.
Then print tt distinct integers i_1, i_2, \ldots, i_ti1,i2,…,it — the indexes of pairs in the corresponding order.
题意翻译
题目描述
有 nn 个整数对 (a_1, b_1), (a_2, b_2), \cdots, (a_n, b_n)(a1,b1),(a2,b2),⋯,(an,bn). 保证 a_1, b_1, a_2, b_2, \cdots, a_n, b_na1,b1,a2,b2,⋯,an,bn 两两不相等, 并且均在区间 [1, 2 \cdot n][1,2⋅n] 内.
好序列的定义:
对于一个序列 x_1, x_2, \cdots, x_{2k}x1,x2,⋯,x2k, 满足
- x_1 < x_2 > x_3 < \cdots < x_{2k - 2} > x_{2k - 1} < x_{2k}x1<x2>x3<⋯<x2k−2>x2k−1<x2k 或
- x_1 > x_2 < x_3 > \cdots > x_{2k - 2} < x_{2k - 1} > x_{2k}x1>x2<x3>⋯>x2k−2<x2k−1>x2k.
求一个序列 i_1, i_2, \cdots, i_ti1,i2,⋯,it 满足 a_{i_1}, b_{i_1}, a_{i_2}, b_{i_2}, \cdots, a_{i_t}, b_{i_t}ai1,bi1,ai2,bi2,⋯,ait,bit 是好序列.
输出 tt 的最大值以及对应的序列 i_1, i_2, \cdots, i_ti1,i2,⋯,it.
输入输出格式
输入格式
第一行包含一个整数 nn, 即数对的个数.
后面 nn 行为数对, 每行包含一个数对 (a_i, b_i)(ai,bi).
输出格式
第一行输出 tt, 即所求序列长度.
接着输出满足题意的序列 i_1, i_2, \cdots, i_ti1,i2,⋯,it.
说明
数据范围
2 \leq n \leq 3 \cdot 10^52≤n≤3⋅105
1 \leq a_i, b_i \leq 2 \cdot n1≤ai,bi≤2⋅n
并且所有 a_i, b_iai,bi 两两不相等.
样例解释
样例 1: 1 < 7 > 3 < 5 > 2 < 101<7>3<5>2<10.
样例 2: 6 > 1 < 3 > 2 < 5 > 46>1<3>2<5>4.
输入输出样例
输入 #1复制
5 1 7 6 4 2 10 9 8 3 5
输出 #1复制
3 1 5 3
输入 #2复制
3 5 4 3 2 6 1
输出 #2复制
3 3 2 1
说明/提示
The final sequence in the first example is 1 < 7 > 3 < 5 > 2 < 101<7>3<5>2<10 .
The final sequence in the second example is 6 > 1 < 3 > 2 < 5 > 46>1<3>2<5>4 .
题目想叉了模拟了半天心态炸裂quq
思路:最重要的是发现ai<bi和ai>bi这两种序列是不会同时出现在一个good里的。那么答案要不是第一种good,要不是第二种good,且在同一个good中,比如是ai<bi这种good,按照ai>aj去排序,这样必然能构造出整条都是good。[ai<bi>aj<bj]
然后看哪种good的数量多就好了
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+100;
typedef long long LL;
LL n,cnt1,cnt2;
struct node{
LL a,b,id;
}A[maxn],B[maxn];
bool cmpa(node A,node B){return A.a>B.a;}
bool cmpb(node A,node B){return A.b<B.b;}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n;
for(LL i=1;i<=n;i++){
LL u,v,id;cin>>u>>v;id=i;
if(u<v) A[++cnt1]={u,v,id};
else B[++cnt2]={u,v,id};
}
if(cnt1>cnt2){
cout<<cnt1<<endl;
sort(A+1,A+1+cnt1,cmpa);
for(LL i=1;i<=cnt1;i++){
cout<<A[i].id<<" ";
}
cout<<endl;
}
else{
cout<<cnt2<<endl;
sort(B+1,B+1+cnt2,cmpb);
for(LL i=1;i<=cnt2;i++){
cout<<B[i].id<<" ";
}
cout<<endl;
}
return 0;
}