【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 ...
随机推荐
- 【174】C#添加非默认字体
参考:C# WinForm程序安装字体或直接调用非注册字体 参考:百度知道 在Debug文件夹下面新建一个font的文件夹,然后将字体的文件复制到里面,使用的时候,直接调用字体文件! private ...
- IE6的3像素BUG产生条件及解决方法
1.IE6中第一个元素浮动第二个元素不浮动,这两个元素之间就会产生3像素BUG 2.解决方案: 2.1若若宽度一定则给第二个元素添加 float 样式即可: 2.2若宽度自适应: 2.2.1 _ma ...
- Newtonsoft.Json序列化日期时间去T的几种方式。
原文地址:MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式. http://www.cnblogs.com/wuball/p/4231343. ...
- bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】
正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...
- Akka源码分析-Cluster-Sharding
个人觉得akka提供的cluster工具中,sharding是最吸引人的.当我们需要把actor分布在不同的节点上时,Cluster sharding非常有用.我们可以使用actor的逻辑标识符与ac ...
- hexo简易脚本
!/bin/bash 检查是否为master分支.目录是否正确 function git-branch-name { git symbolic-ref --short -q HEAD } functi ...
- serlvet HttpServletRequest
1.http://localhost/az/servlet/TestResponse out.print("getServletPath:"+request.getServletP ...
- TCP流量控制与拥塞解决
滑动窗口 但要提高网络利用率: nagle算法 - 延迟 慢启动.拥塞避免 发送端主导cwnd init set ssthresh & cwnd = swnd loop : 网不阻塞 ...
- [ CodeForces 1059 C ] Sequence Transformation
\(\\\) \(Description\) 你现在有大小为\(N\)的一个数集,数字分别为 \(1,2,3,...N\) ,进行\(N\)轮一下操作: 输出当前数集内所有数的\(GCD\) 从数集中 ...
- Python学习日记之忽略删除字符串空白
使用Python自带的函数strip可以剔除字符串开头.结尾.两端的空白 使用场景:用户输入验证 strip : 去除字符串两端的空白 rstrip : 去除字符串末尾(右端)的空白 lstrip : ...