2019 ICPC 南京网络赛 F Greedy Sequence
You're given a permutation aa of length nn (1 le n le 10^51≤n≤105).
For each i in [1,n]i∈[1,n], construct a sequence s_isi by the following rules:
- s_i[1]=isi[1]=i;
- The length of s_isi is nn, and for each j in [2, n]j∈[2,n], s_i[j] le s_i[j-1]si[j]≤si[j−1];
- First, we must choose all the possible elements of s_isi from permutation aa. If the index of s_i[j]si[j] in permutation aa is pos[j]pos[j], for each j ge 2j≥2, |pos[j]-pos[j-1]|le k∣pos[j]−pos[j−1]∣≤k (1 le k le 10^51≤k≤105). And for each s_isi, every element of s_isi must occur in aa at most once.
- After we choose all possible elements for s_isi, if the length of s_isi is smaller than nn, the value of every undetermined element of s_isi is 00;
- For each s_isi, we must make its weight high enough.
Consider two sequences C = [c_1, c_2, ... c_n]C=[c1,c2,...cn] and D=[d_1, d_2, ..., d_n]D=[d1,d2,...,dn], we say the weight of CC is higher thanthat of DD if and only if there exists an integer kk such that 1 le k le n1≤k≤n, c_i=d_ici=di for all 1 le i < k1≤i<k, and c_k > d_kck>dk.
If for each i in [1,n]i∈[1,n], c_i=d_ici=di, the weight of CC is equal to the weight of DD.
For each i in [1,n]i∈[1,n], print the number of non-zero elements of s_isi separated by a space.
It's guaranteed that there is only one possible answer.
Input
There are multiple test cases.
The first line contains one integer T(1 le T le 20)T(1≤T≤20), denoting the number of test cases.
Each test case contains two lines, the first line contains two integers nn and kk (1 le n,k le 10^51≤n,k≤105), the second line contains nn distinct integers a_1, a_2, ..., a_na1,a2,...,an (1 le a_i le n1≤ai≤n) separated by a space, which is the permutation aa.
Output
For each test case, print one line consists of nn integers |s_1|, |s_2|, ..., |s_n|∣s1∣,∣s2∣,...,∣sn∣ separated by a space.
|s_i|∣si∣ is the number of non-zero elements of sequence s_isi.
There is no space at the end of the line.
样例输入复制
2
3 1
3 2 1
7 2
3 1 4 6 2 5 7
样例输出复制
1 2 3
1 1 2 3 2 3 3
这题是队友出的,这个题好像线段树能做,但是我们是暴力加贪心做的,前几遍是枚举的区间位置纯暴力,后来是枚举左右区间长度,对于每个元素的下一个元素都是唯一固定的。之后记忆化搜索就可以求出每个序列。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define LL long long
#define MAXN 100100
int a[MAXN],pos[MAXN];
int s[MAXN],oo[MAXN];
int main()
{
int t;
scanf("%d",&t);
int n,k,l,r,maxx;
while(t--){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
pos[a[i]]=i;
s[i]=1;
}
s[0]=0;
for(int i=2;i<=n;i++){
int j=i-1;
while(j>0){
if(abs(pos[i]-pos[j])<=k){
s[i]+=s[j];
break;
}
j--;
}
}
for(int i=1;i<=n;i++){printf("%d",s[i]);if(i<n)printf(" ");}
printf("n");
}
}
- struts2: config-browser-plugin 与 convention-plugin 学习
- 千万级规模高性能、高并发的网络架构经验分享
- UE4新手之编程指南
- redis 学习笔记(5)-Spring与Jedis的集成
- Linq to SQL 查询Tips
- SQLite 预写式日志
- java调用.net asmx / wcf
- mybatis3.2.8 与 hibernate4.3.6 混用
- mybatis的物理分页:mybatis-paginator
- 使用 WMI 进行诊断WCF
- java:快速文件分割及合并
- 暴涨210倍的一个数字货币正悄无声息崛起
- QT Creator 快速入门教程 读书笔记(一)
- .NET程序优化(GCServer )
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Angular structural指令host element的定位调试
- Import-Module ServerManager Import-Module : 未能加载指定的模块“ServerManager”,因为在任何模块目录中都没有找到有效模块文件..
- Angular自定义structural指令的一个例子
- 2020国庆节Angular structual指令学习笔记
- Delegate
- 图像增强领域大突破!1.66ms处理4K图像,港理工提出图像自适应的3DLUT
- 详解Android studio 动态fragment的用法
- Java fastjson解析json字符串实现过程解析
- SpringMVC系列 MVC设计模式介绍+ SpringMVC的作用及其基本使用+组件解析+注解解析
- Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级
- Greenplum集群扩容总结
- Leetcode刷题 237. 删除链表中的节点 两行代码实现
- Leetcode刷题 206. 反转链表 递归迭代两种方法实现
- MySQL索引和查询优化
- Elasticsearch:Index 生命周期管理入门