hdu 6197 贪心 最长单调递增子序列
来源:互联网 发布:手机太阳系模拟软件 编辑:程序博客网 时间:2024/06/16 04:13
array array array
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description
One day, Kaitou Kiddo had stolen a priceless diamond ring. But detective Conan blocked Kiddo's path to escape from the museum. But Kiddo didn't want to give it back. So, Kiddo asked Conan a question. If Conan could give a right answer, Kiddo would return the ring to the museum.
Kiddo: "I have an arrayA and a number k , if you can choose exactly k elements from A and erase them, then the remaining array is in non-increasing order or non-decreasing order, we say A is a magic array. Now I want you to tell me whether A is a magic array. " Conan: "emmmmm..." Now, Conan seems to be in trouble, can you help him?
Kiddo: "I have an array
Input
The first line contains an integer T indicating the total number of test cases. Each test case starts with two integers n and k in one line, then one line with n integers: A1,A2…An .
1≤T≤20
1≤n≤105
0≤k≤n
1≤Ai≤105
Output
For each test case, please output "A is a magic array." if it is a magic array. Otherwise, output "A is not a magic array." (without quotes).
Sample Input
34 11 4 3 75 24 1 3 1 26 11 4 3 5 4 6
Sample Output
A is a magic array.A is a magic array.A is not a magic array.
Source
2017 ACM/ICPC Asia Regional Shenyang Online
题意很绕口。
n长序列,n-k 长的子序列能否严格单调上升(或下降)
严格单调上升(或下降)子序列的长度 >=n-k 就满足
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.StringTokenizer;public class Main { public static void main(String[] args) { new Task().solve(); }}class Task { InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; void solve(){ int t = in.nextInt() ; while(t-- > 0){ int n = in.nextInt() ; int k = in.nextInt() ; int[] arr = new int[n+1] ; for(int i = 1 ; i <= n ; i++){ arr[i] = in.nextInt() ; } int[] ord = new int[n+1] ; int top = 0 ; ord[++top] = arr[1] ; for(int i = 2 ; i <= n ; i++){ int l = 1 , r = top , mid , s = -1 ; while(l <= r){ mid = (l + r) >> 1 ; if(ord[mid] >= arr[i]){ s = mid ; r = mid - 1 ; } else{ l = mid + 1 ; } } if(s == -1){ ord[++top] = arr[i] ; } else{ ord[s] = arr[i] ; } } if(top >= n-k){ out.println("A is a magic array.") ; continue ; } else{ top = 0 ; ord[++top] = arr[n] ; for(int i = n-1 ; i >= 1 ; i--){ int l = 1 , r = top , mid , s = -1 ; while(l <= r){ mid = (l + r) >> 1 ; if(ord[mid] >= arr[i]){ s = mid ; r = mid - 1 ; } else{ l = mid + 1 ; } } if(s == -1){ ord[++top] = arr[i] ; } else{ ord[s] = arr[i] ; } } if(top >= n-k){ out.println("A is a magic array.") ; } else{ out.println("A is not a magic array.") ; } } } out.flush() ; }}class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextInts(int n) { int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = nextInt(); } return nums; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } }
阅读全文
0 0
- hdu 6197 贪心 最长单调递增子序列
- nyoj 单调递增最长子序列(贪心||DP)
- 最长单调递增子序列
- 最长单调递增子序列
- 最长单调递增子序列
- 最长单调递增子序列
- 最长单调递增子序列
- 最长单调递增子序列
- 最长单调递增子序列
- 单调递增最长子序列
- 单调递增最长子序列
- 最长单调递增子序列
- 单调递增最长子序列
- 最长单调递增子序列
- 最长单调递增子序列
- 单调递增最长子序列
- 最长单调递增子序列
- 最长单调递增子序列
- Tomcat Manager用户配置详解
- 各类Http请求状态(status)及其含义详细解析
- java 微信支付与支付宝支付开发个人笔记
- Elam的caffe笔记之配置篇(一):CentOS6.5编译安装gcc4.8.2
- 笔记·Hibernate框架第四天
- hdu 6197 贪心 最长单调递增子序列
- 学习Angular4遇到问题,解决方法(一)
- 获取数组中最大值
- Hibernate :A different object with the same identifier value was already associated with the session
- js 首字母转换成小写或者大写
- Centos rpm包依赖
- 关于"极光"APP推送JPUSH的服务器端使用
- Redis Cluster处理MULTI/EXEC事务以及Multiple主键命令时的限制
- Android SVG和Lottie库播放动画(Json)