D - Wave HDU - 6570
暴力求解
Avin is studying series. A series is called “wave” if the following conditions are satisfied:
- It contains at least two elements;
- All elements at odd positions are the same;
- All elements at even positions are the same;
- Elements at odd positions are NOT the same as the elements at even positions.
You are given a series with length n. Avin asks you to find the longest “wave” subseries. A subseries is a subsequence of a series.
Input
The first line contains two numbers n, c (1 ≤ n ≤ 100, 000, 1 ≤ c ≤ 100). The second line contains n integers whose range is [1, c], which represents the series. It is guaranteed that there is always a “wave” subseries.
Output
Print the length of the longest “wave” subseries.
Sample Input
5 3
1 2 1 3 2
Sample Output
4
分析
暴力求解枚举的话,复杂度为ncc~~=1e9。但是由于一次只需要关心两个数字,所以如果能记录两个数字的位置,跳过不相干的数字,复杂度就会大大减少,变为 (n/c)cc,约等于1e7,
所以:
1.利用vector存储各个数字在序列出现的位置。
2.枚举。
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;public class Main{public static void main(String[] args)throws IOException {Scanner sc=new Scanner(System.in);Vector<Integer>[]v=new Vector[101];int [][]dp=new int [101][101];int n,x,c;while(sc.hasNext()) {n=sc.nextInt();c=sc.nextInt();for(int i=0;i<=100;i++) v[i]=new Vector();for(int i=1;i<=n;i++) {x=sc.nextInt();v[x].add(i);}int ans=0;for(int i=1;i<=c;i++) {for(int j=1;j<=c;j++) {if(i==j) continue;int len1=v[i].size(),len2=v[j].size();if(len1==0||len2==0) continue;if(len1+len2<=ans) continue;int x1,x2,now,sum;x1=x2=now=sum=0;while(true) {while(x1<len1&&v[i].get(x1)<now) x1++;if(x1==len1) break;now=v[i].get(x1);sum++;while(x2<len2&&v[j].get(x2)<now) x2++;if(x2==len2) break;now=v[j].get(x2);sum++;}ans=Math.max(ans, sum);}}System.out.println(ans);}}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Vector;public class Main{static int N=(int)1e5+1,n,m,l,r,t,area=0;static int []a,b,sum;
// static boolean []a;static FastScanner sc = new FastScanner(System.in);// 快读static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));// 快速输出public static void main(String[] args)throws IOException {// Scanner sc=new Scanner(System.in);a=new int [N];b=new int [N];int [][]dp=new int [101][101];Vector<Integer>arr[]=new Vector[101];for(int i=0;i<=100;i++) arr[i]=new Vector();//这一个样例开始前都应该重新new一下int x;while(sc.hasNext()) {n=sc.nextInt();int c=sc.nextInt();for(int i=1;i<=n;i++) {a[i]=sc.nextInt();arr[a[i]].add(i);}int ans=0;for(int i=1;i<=c;i++) {for(int j=1;j<=c;j++) {if(j==i) continue;int len1=arr[i].size(),len2=arr[j].size();int x1=0,x2=0,now=0,sum=0;//模拟相交过程while(true) {while(x1<len1&&arr[i].get(x1)<now) {x1++;//找到比当前位置(now)大的。}if(x1==len1) break;now=arr[i].get(x1);//找到第一个比当前坐标大的sum++;while(x2<len2&&arr[j].get(x2)<now) {x2++;}if(x2==len2) break;now=arr[j].get(x2);sum++;}ans=Math.max(ans, sum);}}System.out.println(ans);}}static void a(int n,int []a) {for(int i=1;i<=n;i++) {a[i]=sc.nextInt();}}
}
class FastScanner {BufferedReader br;StringTokenizer st;public FastScanner(InputStream in) {br = new BufferedReader(new InputStreamReader(in), 16384);eat("");}public void eat(String s) {st = new StringTokenizer(s);}public String nextLine() {try {return br.readLine();} catch (IOException e) {return null;}}public boolean hasNext() {while (!st.hasMoreTokens()) {String s = nextLine();if (s == null)return false;eat(s);}return true;}public String next() {hasNext();return st.nextToken();}public int nextInt() {return Integer.parseInt(next());}public double nextDouble() {return Double.parseDouble(next());}
}
D - Wave HDU - 6570相关推荐
- 2019CCPC-江西省赛(重现赛)- 感谢南昌大学
趁着多校之际打了一下这个比赛,low的一批.做了七个题,被虐的不轻 Wave HDU - 6570 Avin is studying series. A series is called " ...
- HDU 3723 Delta Wave
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3723 Delta Wave Time Limit: 6000/3000 MS (Java/Others ...
- HDUOJ 6570 Wave
HDUOJ 6570 Wave 题目链接 Problem Description Avin is studying series. A series is called "wave" ...
- HDU 3723 Delta Wave(默慈金数)
传送门 Delta Wave Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3723 Delta Wave(卡特兰数+大数)
题意:从坐标(0, 0)到(n, 0)的折线,这条折线每向右延伸一个单位长度,高度要么不变,要么+1,要么-1,(不能到y=0以下)已知n,求这种折线种数 思路:我们知道上升和下降的次数要一样,而这就 ...
- hdu 4970 Killing Monsters(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 Problem Description Kingdom Rush is a popular TD ...
- HDU 4389 - X mod f(x)
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4389 2012多校,第9场,1010 . 问题是,询问区间内 存在多少个 哈沙德数(Harshad ...
- 卡特兰数 (hdu3723 Delta Wave)
初次接触组合数学,比赛做到了卡特兰数,看了看大牛的博客,算是初步了解了一下 dalao博客:http://www.cppblog.com/MiYu/archive/2010/08/07/122573. ...
- hdu 4389 囧,打表
http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 :一个数能被他各个位数之和整除则符合要求,给L,R,问区间里有多少个数符合要求. 囧,居然打表就能过 ...
最新文章
- 版本迭代规划的几大关键步骤
- 一句话总结贝叶斯分类器
- mysql pt_GitHub - hcymysql/pt-kill: Percona PT-kill重构版(PHP)
- poj1185炮兵阵地
- java opentsdb 链接 池_如何使用tCollector通过Java在OpenTSDB中添加数据
- git 怎么读_python3中开源代码怎么读?
- AI技术已达如此高度:去码、上色6到飞起
- 2篇word文档比较重复率_本科论文写作重复率高的原因,毕业论文降重技巧总结!...
- 高并发秒杀——SpringBoot集成redis
- PHP中对数组进行分页处理的原理及分页实例
- php formdata 多个图片保存_图片上传姿势以及你不知道的Typed Arrays
- [物理学与PDEs]第5章第3节 守恒定律, 应力张量
- SSH免密登陆:Win登陆linux
- Revit二次开发——族的基础
- 菲尔·席勒(Phil Schiller)卸任苹果全球营销高级副总裁,升任苹果研究员
- 登录时用户名或密码错误弹窗提醒重新登录
- centOS7搭建DNS服务器配置详解
- android一键刷机工具,刷机也能如此轻松 Android一键刷机工具
- 如何实现电脑通过手机上网?1分钟搞定!
- 《数学之美》中的模型及启示总结