The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge
A sequence S = {s1, s2, ..., sn} is called heapable if there exists a binary tree T with n nodes such that every node is labelled with exactly one element from the sequence S, and for every non-root node si and its parent sj, sj ≤ si and j < i hold. Each element in sequence S can be used to label a node in tree T only once.
Chiaki has a sequence a1, a2, ..., an, she would like to decompose it into a minimum number of heapable subsequences.
Note that a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contain an integer n (1 ≤ n ≤ 105) — the length of the sequence.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
It is guaranteed that the sum of all n does not exceed 2 × 106.
Output
For each test case, output an integer m denoting the minimum number of heapable subsequences in the first line. For the next m lines, first output an integer Ci, indicating the length of the subsequence. Then output Ci integers Pi1, Pi2, ..., PiCi in increasing order on the same line, where Pij means the index of the j-th element of the i-th subsequence in the original sequence.
Sample Input
4
4
1 2 3 4
4
2 4 3 1
4
1 1 1 1
5
3 2 1 4 1
Sample Output
1
4 1 2 3 4
2
3 1 2 3
1 4
1
4 1 2 3 4
3
2 1 4
1 2
2 3 5 题意:序列 S={s1,s2,...,sn} 被称为 heapable ,当且仅当存在一个二叉树 T , n 个点均作为 T 的一个节点,且满足任意非根节点 si 和其父节点 sj , sj≤si 且 j<i 。
现在有一个序列 a1,a2,...,an ,相应将其分成若干个 heapable 的子序列,问使得子序列个数最少的策略
题解:参考自别人的代码 贪心+二分 用memset会TLE 直接for一遍清零。
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#pragma comment(linker, "/STACK:102400000,102400000")
int a[];
struct node
{
int f,s;
}p;
bool operator <(node x,node y)
{
if(a[x.s]==a[y.s]) return x.s<y.s;
return a[x.s]<a[y.s];
}
set<node> st;
set<node> :: iterator it;
vector<int> v[];
int dis[];
int main()
{
int t;
scanf("%d",&t);
for(int i=;i<=t;i++)
{
st.clear();
int cnt=;
int n;
scanf("%d",&n);
for(int k=;k<=n;k++)
dis[k]=;
for(int j=;j<=n;j++)
{
scanf("%d",&a[j]);
p.s=j;
it=st.upper_bound(p);
if(it==st.begin())
{
v[cnt].push_back(j);
p.f=cnt;
p.s=j;
st.insert(p);
cnt++;
}
else
{
it--;
p=*it;
dis[p.s]++;
if(dis[p.s]==)
st.erase(it);
p.s=j;
st.insert(p);
v[p.f].push_back(j);
}
}
printf("%d\n",cnt);
for(int j=;j<cnt;j++)
{
printf("%d",v[j].size());
for(int k=;k<v[j].size();k++)
printf(" %d",v[j][k]);
printf("\n");
v[j].clear();
}
}
return ;
}
The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分的更多相关文章
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - C  暴力 STL
		
What Kind of Friends Are You? Time Limit: 1 Second Memory Limit: 65536 KB Japari Park is a larg ...
 - ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp
		
Seven Segment Display Time Limit: 1 Second Memory Limit: 65536 KB A seven segment display, or s ...
 - The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club
		
Doki Doki Literature Club Time Limit: 1 Second Memory Limit: 65536 KB Doki Doki Literature Club ...
 - 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
		
我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...
 - The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - M Lucky 7
		
Lucky 7 Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found a positive integer se ...
 - The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - J CONTINUE...?
		
CONTINUE...? Time Limit: 1 Second Memory Limit: 65536 KB Special Judge DreamGrid has clas ...
 - The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - B King of Karaoke
		
King of Karaoke Time Limit: 1 Second Memory Limit: 65536 KB It's Karaoke time! DreamGrid is per ...
 - The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple -A  Peak
		
Peak Time Limit: 1 Second Memory Limit: 65536 KB A sequence of integers is called a peak, if ...
 - ZOJ 4033 CONTINUE...?(The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)
		
#include <iostream> #include <algorithm> using namespace std; ; int a[maxn]; int main(){ ...
 
随机推荐
- thinkphp5登录并保存session、根据不同用户权限跳转不同页面
			
本文讲述如何在thinkphp5中完成登录并保存session.然后根据不同的用户权限跳转相应页面功能的实现.我也在学习thinkphp源码的路上,记录一下并与大家分享.完成该步骤主要有以下三个步骤完 ...
 - 十几行代码带你用Python批量实现txt转xls,方便快捷
			
前天看到后台有一兄弟发消息说目前自己有很多txt 文件,领导要转成xls文件,问用python怎么实现,我在后台简单回复了下,其实完成这个需求方法有很多,因为具体的txt格式不清楚,当然如果是有明确分 ...
 - 4星|《财经》2018年第15期:电动飞机、无人小飞机、AI无人机
			
<财经>2018年第15期 总第532期 旬刊 本期主题是AI.有多篇国内AI行业的比较深入的调查报告,比较有意思的有:电动飞机.无人小飞机.AI无人机.欧盟通用数据保护条例.Amazon ...
 - c字符数组里的中文
			
char *p ="你abc"; strlen(p); //6 utf-8编码中
 - Hyperledger Fabric 1.0 从零开始(十三)——orderer分布式方案
			
简述 在搭建HyperLedger Fabric环境的过程中,我们会用到一个configtx.yaml文件(可参考Hyperledger Fabric 1.0 从零开始(八)——Fabric多节点集群 ...
 - IncDec序列:差分+贪心
			
IncDec序列 题目描述: 给定一个长度为 n 的数列 a1,a2,…,an,每次可以选择一个区间[l,r],使下标在这个区间内的数都加一或者都减一. 求至少需要多少次操作才能使数列中的所有数都一样 ...
 - mac react-native从零开始android真机测试
			
1. 安装android相关jdk,(https://blog.csdn.net/vvv_110/article/details/72897142) 2. 手机和mac使用usb连接, 手机开发者设置 ...
 - 172322 2018-2019-1 《Java软件结构与数据结构》实验一报告
			
172322 2018-2019-1 <Java软件结构与数据结构>实验一报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 张昊然 学号:20172322 实验教师 ...
 - struts2--文件上传类型3
			
拦截器栈在<package>标签内 <action>标签外配置 如上我们如果把它定义成默认拦截器的话就不需要在 <action>标签中引入,没有的话需要引入拦截器 ...
 - lintcode-433-岛屿的个数
			
433-岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...