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 array A 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?
 

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,A2An.
1T20
1n105
0kn
1Ai105
 

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());        }        }    





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿子发烧39度该怎么办 孩子发烧39度7怎么办 7个月婴儿发烧怎么办 感冒了头发很油怎么办 5岁宝宝发烧39度怎么办 婴儿烧到39.5度怎么办 1岁多宝宝39.5度怎么办 宝宝发烧40多度怎么办 7岁宝宝发烧了怎么办 宝宝反复发烧39度怎么办 一岁半宝宝反复发烧39度怎么办 七岁发烧38度怎么办 小孩一直37度1怎么办 婴儿一直37度多怎么办 1岁半高烧39度怎么办 反复发烧39度多怎么办 孩子不爱喝水怎么办%3f 8岁儿童不爱喝水怎么办 儿子14岁了不爱说话怎么办 我孩子长得老慢怎么办 老公嫌你烦了怎么办 2岁宝宝吃饭不嚼怎么办 2岁宝宝挑食厌食怎么办 孩子不爱和家长交流怎么办 孩子发烧在医院查不出病因怎么办 宝宝乳牙长歪了怎么办 两岁宝宝不爱吃水果怎么办 两岁宝宝不吃水果怎么办 一岁的宝宝上火了怎么办 吃水果嘴唇肿了怎么办 二岁宝宝不爱吃饭怎么办 小婴儿便秘但不爱喝水怎么办 宝宝只吃水果不吃饭怎么办 一岁宝宝不喜欢吃水果怎么办 1岁宝宝不吃水果怎么办 一岁半宝宝吃水果拉肚子怎么办 大人发烧了怎么办如何退烧 怀孕后不爱吃水果怎么办 不敢吃水果了怕虫怎么办 宝宝发烧38度不出汗怎么办 1岁宝宝喜欢含饭怎么办