D - Lis on Circle Gym - 102441D (LIS + 线段树)
There are nn people at the round gaming table. Each of them has a set of cards. Every card contains some number xx. Players make turns consecutively, one after another, starting from the player number 1. A player in his turn can either skip his turn (to pass), or put (and leave on the table) a card with a number that is strictly greater than the previously played last number. No more than kk players in a row can pass the turn. All players know the numbers written on the other players cards and always play optimally. Help gamblers to assemble an increasing sequence of maximum length.
Input
The first line contains two numbers nn and kk — the number of players and the maximum possible amount of turn skipping in a row.
The next nn lines contain the description of the cards players have in their hands. The first number in the mimi is the number of cards the current player has in his hand. The following space separated miminumbers represent the written on the cards numbers xx.
Output
In the first line print the single number — the length of the maximum sequence. In the next lines print two space separated numbers: the player index number and the number written on the card he played. If several solutions exist, output any of them.
Example
3 1
4 1 10 12 20
2 11 21
4 3 5 15 22
9
1 1
3 3
1 10
2 11
1 12
3 15
1 20
2 21
3 22
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define lowbit(x) (x&(-x))
#define MID (l + r) / 2
#define ls pos*2
#define rs pos*2+1
#define pb push_back
#define ios() ios::sync_with_stdio(0) using namespace std;
typedef pair<int,int>pi;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + ; struct node{
int id,x;
bool operator < (const node & w)const{
return x < w.x || (x == w.x && id < w.id);
}
}arr[maxn];
pi dp[maxn],tree[maxn<<];
void pushup(int rt){
tree[rt] = max(tree[rt*],tree[rt*+]);
}
void build(int l,int r,int rt){
if(l == r){
tree[rt] = pi(,-);
return ;
}
int mid = (l + r) / ;
build(l,mid,rt*);
build(mid + ,r,rt*+);
pushup(rt);
}
pi query(int L,int R,int l,int r,int rt){//询问[L,R]内长度最长的上升子序列节点
if(L > R)return pi(,-);
if(L <= l && r <= R){
return tree[rt];
}
int mid = (l + r) / ;
pi ans = pi(,-);
if(L <= mid)ans = max(ans,query(L,R,l,mid,rt*));
if(R > mid)ans = max(ans,query(L,R,mid+,r,rt*+));
return ans;
}
void update(int pos,pi val,int l,int r,int rt){//pos位置更改最优值(和自身取最优值)
if(l == r){
tree[rt] = max(tree[rt],val);
return ;
}
int mid = (l + r) / ;
if(pos <= mid)update(pos,val,l,mid,rt*);
else update(pos,val,mid+,r,rt*+);
pushup(rt);
}
void dfs(int u,int dep){//dfs寻找路径,dp[u].second 代表下一个下标是几
if(u == -){
cout << dep << endl;
return ;
}
dfs(dp[u].second,dep+);
cout << arr[u].id << ' ' << arr[u].x << endl;
}
int main()
{
int n,k,top = ;
cin >> n >> k;
build(,n,);
rep(i,,n){
int m;
cin >> m;
rep(j,,m){
int x;
cin >> x;
arr[++top] = (node){i,x};
}
}
sort(arr+,arr++top);
int i;
for(i = ;i <= top;){
for(int j = i;j <= top;j++){//一个值的数据有多个的话,处理完,先不更新,因为题目保证严格递增
if(arr[i].x != arr[j].x)break;
int id = arr[j].id;
int pos = ((id - k - ) % n + n) % n + ;//最多可跳过k个,这个推一下就好了~
if(pos < id)dp[j] = query(pos,id-,,n,);
else dp[j] = max(query(pos,n,,n,),query(,id-,,n,));
}
int j;
for(j = i;j <= top;j++){
if(arr[i].x != arr[j].x)break;
if(dp[j].first == && arr[j].id > k + )continue;//第一个人特判
dp[j].first++;//以j为结尾的最长上升子序列加1
int pos = arr[j].id;
update(pos,pi(dp[j].first,j),,n,);//更新最优值
}
i = j;
}
int ans = ,rt = ;
for(int i = ;i <= top;i++){
if(dp[i].first > ans){
ans = dp[i].first;
rt = i;
}
}
if(rt == )cout << << endl;
else dfs(rt,);
return ;
}
/*
3 0
3 1 2 3
3 1 2 1
4 1 2 3 2
*/
D - Lis on Circle Gym - 102441D (LIS + 线段树)的更多相关文章
- cf1132G 线段树解分区间LIS(一种全新的线段树解LIS思路)+单调栈
/* 给定n个数的数列,要求枚举长为k的区间,求出每个区间的最长上升子序列长度 首先考虑给定n个数的数列的LIS求法:从左往右枚举第i点作为最大点的贡献, 那么往左找到第一个比a[i]大的数,设这个数 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Gym 101201J Shopping (线段树+取模)
题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...
- Hacker Cups and Balls Gym - 101234A 二分+线段树
题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置 ...
- Gym - 101982F 扫描线+线段树
题目链接:https://codeforces.com/gym/101982/attachments 要你求覆盖奇数次的矩形面积并,每次更新时减去原先的值即可实现奇数次有效,下推时为保证线段长度不变左 ...
- 洛谷P3928 Sequence2(dp,线段树)
题目链接: 洛谷 题目大意在描述底下有.此处不赘述. 明显是个类似于LIS的dp. 令 $dp[i][j]$ 表示: $j=1$ 时表示已经处理了 $i$ 个数,上一个选的数来自序列 $A[0]$ 的 ...
- Hdu 3564 Another LIS 线段树+LIS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU3564 --- Another LIS (线段树维护最值问题)
Another LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu_3564_Another LIS(线段树+LIS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意:给你N个数的位置.数i的位置为第i个数,比如 0 0 2,表示1插在第0个位置,此时数列为 ...
随机推荐
- Flink on yarn以及实现jobManager 高可用(HA)
on yarn https://ci.apache.org/projects/flink/flink-docs-release-1.8/ops/deployment/yarn_setup.html f ...
- qvector 转为数组
在 qt 中想要把 qvector 转化为原始数据构成的数组,有几种方法: 直接使用循环读取 double *bytes = new double[vec.size()]; for (int i = ...
- 七、React表单详解 约束性和非约束性组件 input text checkbox radio select textarea 以及获取表单的内容
一.约束性和非约束性组件: 非约束性组: MV: <input type="text" defaultValue="a" /> 这个 default ...
- ThinkPHP 5.0远程命令执行漏洞分析与复现
0x00 前言 ThinkPHP官方2018年12月9日发布重要的安全更新,修复了一个严重的远程代码执行漏洞.该更新主要涉及一个安全更新,由于框架对控制器名没有进行足够的检测会导致在没有开启强制路由的 ...
- POJ 1185:炮兵阵地
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21366 Accepted: 8280 Description ...
- 抗干扰性极强非接触式读卡13.56mhz芯片:SI522
由于智能门锁产品不断地火爆,市场上的不断出现破解的方法.对此中科微联合深圳市动能世纪科技有限公司不断满足市场需求,推出一款抗干扰性极强的13.56mhz芯片. 该芯片出了抗干扰性强以外还直接PIN2P ...
- Day1-T4
原题目 Describe:注意是“两次及以上”而不是“两种及以上”!! code: #include<bits/stdc++.h> using namespace std; int k,m ...
- sqlserver 联接查询的一些注意点
1.内连接的安全性 (1) inner join 是ANSI SQL-92 语法.等值联接是ANSI SQL-89 的语法 ,两者已相同方式解释.在性能上没有差别 (2)但是强烈建议使用ANSI SQ ...
- poj 3693 Maximum repetition substring
呵呵呵呵呵呵呵呵呵呵,sb(神犇)题看了一天,还是不懂 题目要求的是最多重复的,那么就来找重复的,可以先枚举一个重复的单元(比如ababab,就枚举ab)的长度, 然后再原串中,会有ch[0],ch[ ...
- GRADLE依赖的统一管理
参考链接:http://stormzhang.com/android/2016/03/13/gradle-config/ 我想大部分人应该都在使用Gradle来依赖管理,还没有使用的去面壁思过,Gra ...