【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 ...
随机推荐
- 01_c++下jni开发说明
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- SQL 存储过程语句拼接愁人的时间类型
http://www.cnblogs.com/yrup/p/4201329.html 解决方法: Select CONVERT(varchar(100), GETDATE(), 25): 2006-0 ...
- js原始数据类型和引用数据类型=>callback数据传输原理
摘要:js的数据类型有种划分方式为 原始数据类型和 引用数据类型. 原始数据类型 存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置.栈区包括了 变量的标识符和变量的值. ...
- Wannafly挑战赛29A御坂美琴
传送门 这套题很有意思2333 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些数,用map记录一下,判断是否所有数都能被分出来就是了 //m ...
- P3297 [SDOI2013]逃考
传送门 完全看不出这思路是怎么来的-- 首先对于两个亲戚,他们监视范围的边界是他们连线的中垂线.那么对于一个亲戚来说它能监视的范围就是所有的中垂线形成的半平面交 然后如果某两个亲戚的监视范围有公共边, ...
- Java多线程(九) synchronized 锁对象的改变
public class MyService { private String lock = "123"; public void testMethod() { synchroni ...
- 使用JS分页 <span> beta 3.0 完成封装的分页
<html> <head> <title>分页</title> <style> #titleDiv{ width:500px; backgr ...
- Java编程思想读书笔记_第8章
覆盖私有方法 class Father { private void f() { System.out.println("Father::f()"); } public stati ...
- PHP常见问题总结
1.为什么会出现这种情况?端口什么的都设置正确了. 解决方法: 请将本机的IIS服务关闭,开启Apache服务.IIS服务的关闭方法可参见 https://jingyan.baidu.com/arti ...
- iOS,Core Animation--负责视图的复合功能
简介 UIKit API UIKit是一组Objective-C API,为线条图形.Quartz图像和颜色操作提供Objective-C 封装,并提供2D绘制.图像处理及用户接口级别的动画. ...