【Codeforces 977F】Consecutive Subsequence
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
设f[i]表示i作为序列的最后一个数字,最长的连续序列的长度。
用f[i]和f[i-1]+1来转移即可
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)2e5;
static class Task{
int n;
HashMap<Integer,Integer> dic = new HashMap<Integer,Integer>();
int a[] = new int[N+10];
ArrayList<Integer> v = new ArrayList<Integer>();
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();
int len = 0,idx = -1;
for (int i = 1;i <= n;i++) {
a[i] = in.nextInt();
int x = a[i];
if (dic.containsKey(x)) {
int cur = dic.get(x);
int temp = 0;
if (dic.containsKey(x-1)) {
temp = dic.get(x-1);
}
cur = Math.max(cur, temp+1);
dic.put(x, cur);
}else {
int temp = 0;
if (dic.containsKey(x-1)) {
temp = dic.get(x-1);
}
dic.put(x, temp+1);
}
int temp0 = dic.get(x);
if (temp0>len) {
len = temp0;
idx = i;
}
}
out.println(len);
int now = a[idx];
for (int i = idx;i >= 1;i--) {
if (a[i]==now) {
v.add(i);
now--;
}
}
for (int i = (int)v.size()-1;i >= 0;i--) {
out.print(v.get(i)+" ");
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 977F】Consecutive Subsequence的更多相关文章
- 【Codeforces 632D】 Longest Subsequence
[题目链接] 点击打开链接 [算法] 设取的所有数都是k的约数,则这些数的lcm必然不大于k. 对于[1, m]中的每个数,统计a中有多少个数是它的约数即可. [代码] #include<bit ...
- 【29.41%】【codeforces 724D】Dense Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【38.02%】【codeforces 625B】War of the Corporations
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- 谈CSS布局中HTML标签语义化
很多人都在做前端,当然这里包括很多新手,也许在新手的想法就是在做布局的时候不要用font标签之类的就算是web标准了,这样一来,造成如今网上“div泛滥”,一看源文件,霍,除了div没其他标签了. 这 ...
- tyvj 2054 [Nescafé29]四叶草魔杖【克鲁斯卡尔+状压dp】
传送:http://www.joyoi.cn/problem/tyvj-2054 来自lyd课件: 所以先预处理出各个sum为0的块,然后状压dfs取min来得到答案 #include<iost ...
- golang——database/sql包学习
1.database/sql包 sql包提供了保证SQL或类SQL数据库的泛用接口. 使用sql包时必须注入(至少)一个数据库驱动. (1)获取mysql driver:go get -v githu ...
- css 实现 checkbox 大小调整
一般调整 checkbox 大小我们想到的是 width.height,可是设置后,发现是没有效的. 如微信小程序里面,checkbox 默认就很大,想设置小一点怎么办? transform: sca ...
- c++病毒函数
FreeConsole(); 屏蔽输出. BlockInput(); 阻止键盘和鼠标的工作. 所需头文件: #include <windows.h> #include <Winabl ...
- ROS-USB摄像头
前言:演示使用usb摄像头功能,推荐使用方法二. 首先要有一个usb摄像头,本次使用的是罗技(Logitech)摄像头. 一.使用软件库里的uvc-camera功能包 1.1 检查摄像头 lsusb ...
- [Qt及Qt Quick开发实战精解] 第1章 多文档编辑器
这一章的例子是对<Qt Creator快速人门>基础应用篇各章节知识的综合应用, 也是一个规范的实例程序.之所以说其规范,是因为在这个程序中,我们对菜单什么时候可用/什么时候不可用.关 ...
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- spring的依赖注入如何降低了耦合
依赖注入:程序运行过程中,如需另一个对象协作(调用它的方法.访问他的属性时),无须在代码中创建被调用者,而是依赖于外部容器的注入 看过一些比较好的回答 1.一个人(Java实例,调用者)需要一把斧子( ...
- 梦想CAD控件COM接口文字样式
增加文字样式 用户可以增加文字样式到数据库,并设置其字体等属性,具体实现c#代码如下: private void CreateText() { MxDrawApplication app = new ...