hdu-3183A Magic Lamp(贪心)

题目的意思是:
给你一个大数,然后删减其中的K个数,并且剩下的数还是按原来在的先后次序排列,求所得的那个数最小的那个数。
思路:贪心(要取得数最小,你从左往右选数的时候,选的第一数,就是选后组成数的位权最高的,要一个数最小,位权最高的所对应那位要最小,然后依次是下一位权最小)。原来有N个数,删除K个数得最小数可以转化为从左往右选N-K个数所组成的数最小。那么第一个数必须在区间[0,K](数组下标)(存大数的数组首位是从0开始的)。
证:选的第一位数如果超过K,假如取的是K+S(K+S<=N-1)那么从(K+S+1)到N-1还剩下(N-K-S-1)个数,因为已经选了一个数剩余(N-K-1)个数没选,那么这些数必定要在区间
[K-S+1,N]上选。又(N-K-1)大于区间长度N-K-S-1(S>=1).
所以第一个数只能在[0,K]上取,且要取最小值,每位取当前区间最小才能保证最后所得数最小。
那么当第一个数取完后,取的是pos位置的,那么在[0,pos-1]是未取得数,也就是去掉的数,因为后面省下的(N-K-1)个数不能在这些位置去,因为这些数在pos之前,所以如果去的话
在所得的数中位置在第一个数之前,所以与pos为第一个数矛盾。
这样你选第二个数时此时K应该更新为K-(pos+1-n)(n为当前所选好的数的个数(当前n=1)),pos更新为pos+1;那么第二个数在区间[pos,pos+K]其实pos+K就为K+n;
那么这样直到选好N-K个数就行了。
/*RMQ优化 复杂度为n*log(n)*/
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<math.h>
7 void RMQup(int k,int l);
8 int que(int i,int j);
9 int Min(int x,int y);
10 char a[2000];
11 int b[2000];
12 const int N=100;
13 int RMQ[1005][1005];
14 using namespace std;
15 int main(void)
16 {
17 int n,i,j,k,p,q,l,m;
18 while(scanf("%s",a)!=EOF)
19 {
20 scanf("%d",&k);
21 l=strlen(a);
22 p=l-k;
23 for(i=0; i<l; i++)
24 RMQ[0][i]=i;
25 int r=log2(l);
26 r++;
27 RMQup(r,l);
28 int pp,qq;
29 int dd;
30 pp=0;
31 m=k;
32 int coutt =0;
33 while(coutt<p)//选好p个数字就结束
34 {
35 int z=que(pp,m);
36 b[coutt++]=a[z]-'0';
37 pp=z+1;
38 m++;
39 }
40 for(i=0; i<p; i++)
41 {
42 if(b[i]!=0)
43 {
44 break;
45 }
46 }
47 for(j=i; j<p; j++)
48 {
49 printf("%d",b[j]);
50 }
51 if(i==p)
52 {
53 printf("0");
54 }
55 printf("\n");
56 }
57
58 return 0;
59
60
61 }
62
63
64 void RMQup(int k,int l)//dp求RMQ(这种RMQ链接讲解http://www.cnblogs.com/zzuli2sjy/p/4971449.html)
65 {
66 int i,j;
67 for(i=1; i<=k; i++)
68 for(j=0; j<l; j++)
69 if(j+(1<<i-1)<l)
70 RMQ[i][j]=Min(RMQ[i-1][j],RMQ[i-1][j+(1<<i-1)]);
71
72 }
73
74 int que(int i,int j)
75 {
76 int f=log2(j-i+1);
77 return Min(RMQ[f][i],RMQ[f][j-(1<<f)+1]);
78
79 }
80 int Min(int x,int y)//求在某段的最小值的数组下标
81 {
82 if(a[x]==a[y])
83 {
84 return x<y?x:y;//当两个数相同时返回数组下标小的(比如223 要去1个数字,答案是22,如果返回数组下标大的结果就是23了)
85 }
86 else
87 {
88 return a[x]<a[y]?x:y;
89 }
90 }
1 /*直接循环找段最小*/
2 #include<stdio.h>
3 #include<algorithm>
4 #include<iostream>
5 #include<stdlib.h>
6 #include<string.h>
7 using namespace std;
8 char a[1005];
9 char b[1005];
10 int main(void)
11 {
12 int n,i,j,k,p,q,l;
13 while(scanf("%s",a)!=EOF)
14 {
15 scanf("%d",&k);
16 l=strlen(a);
17 if(k==l)
18 {
19 printf("0\n");
20 }
21 else
22 {
23 int y=l-k;
24 int x=0;
25 int m=y;
26 int coutt=0;
27 int dd;
28 dd=0;
29 while(coutt<y)
30 {
31 b[coutt]=a[x];
32 for(i=x; i<=x+k; i++)//循环找最小
33 {
34 if(a[i]<b[coutt])
35 {
36 b[coutt]=a[i];
37 dd=i;
38 }
39
40 }
41 dd=dd+1;
42 k-=(dd-x-1);
43 coutt++;
44 x=dd;
45 }
46 for(i=0; i<coutt; i++)
47 {
48 if(b[i]!='0')
49 {
50 break;
51 }
52 }
53 if(i==coutt)
54 {
55 printf("0");
56 }
57 for(j=i; j<coutt; j++)
58 {
59 printf("%c",b[j]);
60 }
61 printf("\n");
62 }
63 }
64 return 0;
65 }
hdu-3183A Magic Lamp(贪心)的更多相关文章
- hdu A Magic Lamp
http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3183 A Magic Lamp 贪心
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- HDU3183 A Magic Lamp —— 贪心(单调队列优化)/ RMQ / 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的 ...
- A Magic Lamp HDU - 3183 (逆向贪心/RMQ)
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
- hdu 3183 A Magic Lamp(RMQ)
题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...
- A Magic Lamp(贪心+链表)
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3183 A Magic Lamp RMQ ST 坐标最小值
hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...
- A Magic Lamp -- hdu -- 3183
http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3183 A Magic Lamp(RMQ)
A Magic Lamp Time Limi ...
随机推荐
- 【模板】Splay(伸展树)普通平衡树(数据加强版)/洛谷P6136
题目链接 https://www.luogu.com.cn/problem/P6136 题目大意 需要写一种数据结构,来维护一些非负整数( \(int\) 范围内)的升序序列,其中需要提供以下操作: ...
- Hadoop入门 集群常用知识与常用脚本总结
目录 集群常用知识与常用脚本总结 集群启动/停止方式 1 各个模块分开启动/停止(常用) 2 各个服务组件逐一启动/停止 编写Hadoop集群常用脚本 1 Hadoop集群启停脚本myhadoop.s ...
- MapReduce的类型与格式
MapReduce的类型 默认的MR作业 默认的mapper是Mapper类,它将输入的键和值原封不动地写到输出中 默认的partitioner是HashPartitioner,它对每条记录的键进行哈 ...
- 在 Apple Silicon Mac 上 DFU 模式恢复 macOS 固件
DFU 模式全新安装 macOS Big Sur 或 macOS Monterey 请访问原文链接:https://sysin.org/blog/apple-silicon-mac-dfu/,查看最新 ...
- 一起手写吧!promise.all
Promise.all 接收一个 promise 对象的数组作为参数,当这个数组里的所有 promise 对象全部变为resolve或 有 reject 状态出现的时候,它才会去调用 .then 方法 ...
- Https原理及证书管理
Https原理及证书管理 SSL(Secure Sockets Layer,安全套接层)/TLS(Transport Layer Security,传输层安全)保证了客户端web服务器的连接安全.客户 ...
- Android 高级UI组件(二)
1.ExpandableListView 显示垂直滚动两级列表的条目,只允许两个层次 整体思路: 要给ExpandableListView设置适配器,那么必须先设置数据源. 数据源,就是此处的适配器类 ...
- 利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired
大家平时使用spring依赖注入,都是怎么写的? @Servicepublic class OrderService {@Autowiredprivate UserService userServic ...
- 【C/C++】编码(腾讯)
假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, - -, b, ba, ...
- 使用 OPC Browser 加载 OPC Server 监测点
1,首先第一步,要连接OPC ,创建好 OPC对象. /// <summary> /// 连接OPC /// </summary> private string OPCIP=1 ...