• 题目来源:array array array
  • Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 0 Accepted Submission(s): 0
  • 题目描述和输出:
    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,A2…An.
    1≤T≤20
    1≤n≤10^5
    0≤k≤n
    1≤Ai≤10^5
  • 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
    3
    4 1
    1 4 3 7
    5 2
    4 1 3 1 2
    6 1
    1 4 3 5 4 6
  • Sample Output
    A is a magic array.
    A is a magic array.
    A is not a magic array.
  • 题目分析:
    有T个测试样例,对于每个测试样例,给定两个整数,分别是数组元素个数n和需要删除的元素个数k,如果这个数组在删除了k个元素之后,是递增或者递减的,就称这个数组是magic array,否则不是。
  • 做法:这道题是个经典的DP,换句话说就是模板题。
  • 完整代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define MAX 100005
#define INF 100000000
int dp[MAX];
int a[MAX];
int k, T, n, flag;
int main()
{
int temp;
cin >> T;
while (T-- > 0)
{
cin >> n >> k;
memset(a, 0, sizeof(int)*MAX);
for (int i = 0; i < n; i++)
cin >> a[i];
fill(dp, dp + n, INF);
for (int i = 0; i < n; i++)
{
*upper_bound(dp, dp + n, a[i]) = a[i];
}
temp = 0;
while (dp[temp]<INF)
{
temp++;
}
if (temp >= n - k)
printf("A is a magic array.\n");
else
printf("A is not a magic array.\n");
}
return 0;
}

最新文章

  1. JS区分移动端和PC
  2. echarts在.Net中使用实例(一) 简单的Demo
  3. 如何静态添加toolbar到datagrid
  4. asp.net生成随机密码
  5. ajaxfileupload回到json带&lt;pre&gt;
  6. jQuery uploadify上传文件404,500错误
  7. JavaScript运算符
  8. 介绍NSURLSession网络请求套件
  9. CSS Devices可以让你在线直接获取使用CSS写的Mobile外形。
  10. 推荐《Linux 多线程服务器端编程》
  11. 执行计划之CONCATENATION
  12. iOS之UISearchBar实时显示结果
  13. maven GroupId 和ArtifactId的含义
  14. html5 css练习,弹性三栏布局
  15. saltstack高效运维
  16. [Maven]Maven如何得到单独的单元测试报告
  17. hdu5517 二维树状数组
  18. JAVA面对对象(四)——抽象类
  19. Linux系统查看日志信息总结
  20. java资料共享

热门文章

  1. 注意mysql connector的版本
  2. LitJson(读Exce文件写入到json文件):
  3. 案例53-crm练习修改客户功能实现
  4. HDU 3829——Cat VS Dog——————【最大独立集】
  5. dt4.0上传图片总是压缩解决办法,为什么我设置了不压缩图片,程序还是压缩呢?
  6. SpingCloud微服务架构学习(二)之Actuator监控
  7. hibernate课程 初探一对多映射2-3 创建hibernateUtil工具类
  8. 基于百度ai,图灵机器人,Flask 实现的网站语音智能问答
  9. &lt;Android 基础(十三)&gt; shape
  10. ComponentOne、Spread、ActiveReports 5折起 加入惊喜惠