SGU - 296 - Sasha vs. Kate
上题目:
296. Sasha vs. Kate
Memory limit: 65536
kilobytes
output: standard
During the regular Mars's World Finals Subregional Programming Contest a boy Sasha lost N
"Mars" bars of chocolate to a girl Kate. But for two years already
Sasha does not hurry to pay his debt. And now Sasha and Kate decided
that Sasha will give Kate P chocolate bars, where number P can be obtained from the number N by removing exactly K
decimal digits. Sasha generously let Kate to choose digits to be
removed. Your task is to find out how many bars Sasha will give Kate. Of
course Kate will choose K digits from the number N in such a way that the resulting number P would be maximal.
The first line of the input file contains two integer numbers N and K (1≤ N≤ 101000; 0≤ K≤ 999). Number K is strictly less than the number of digits in N. N will not have any leading zeros.
Output the unknown P.
sample input |
sample output |
1992 2 |
99 |
sample input |
sample output |
1000 2 |
10 |
题意是给你一个最多有1001的数字,然后让你剔除其中的某K个数字,求可以得到的最大的数字。
这一题用贪心思想。
这一题一开始读错题目,以为是挑K个数字,还以为是连续的,然后就WA了两次。后来终于看清题目了。然后就开始了各种yy,想过的思路有不少,最后列举了几个数字,找到了一点规律。
规律是这样的:当第i个数字比后一个数字小的话,把它删掉可以比删掉其他数字的结果得到更大的结果,然后如果后面的数字等于或者小于当前的数字的话,说明当前的数字暂时可以不用删掉,然后对i+1操作。
当然,这个规律还不完善,如果数字为32121,要求删掉2个数字的话,那当然是删掉两个1,可是刚才的思路只删除了一个1,同时如果在外面加一个循环也不能解决问题,如果序列式是32112的话就可以很好地得到正确答案,那应该怎么做呢,暂时的做法是在所有数字的结尾加一个INF,这是根据我的代码的结构(我是用栈来实现的)写出来的解决方案= =,好像有点XX(= =),AC的代码有bug= =现在改了一下。听其他同学的解法好像用了一个for和一个while就搞定了。= =
上代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#define MAX 300000+10
using namespace std; int s[MAX];
stack<int> S; int main()
{
//freopen("data.txt","r",stdin);
int i,j,n,k,e,h;
char c;
k=;
memset(s,-,sizeof(s));
while((c=getchar())!=' ')
{
s[k]=c-'';
k++;
}
scanf("%d",&n);
h=n;
i=;
//S.push(s[i]);
//i++;
while(h)
{
while(!S.empty() && s[i]>S.top())
{
S.pop();
h--;
if(h==) break;
}
if(h==) break;
S.push(s[i]);
i++;
//if(s[i]==-1) break;
}
h=S.size();
j=;
for(i--;j<h;j++,i--)
{
s[i]=S.top();
S.pop();
}
//e=0;
//for(j=0;j<h;j--) e=e*10+p[j];
if(i<k)
for(i++;i<k;i++)
{
printf("%d",s[i]);
//e=e*10+s[i];
}
else printf("");
printf("\n");
return ;
}
296
SGU - 296 - Sasha vs. Kate的更多相关文章
- SGU 296.Sasha vs. Kate(贪心)
题意: 给出长度为n(<=1000)的一个数.输出删掉k个数字后的最大值. Solution: 简单贪心. s[i]代表数字s的第i位. 从前往后第一个满足s[i]>s[i-1]的位置,最 ...
- 今日SGU 5.22
SGU 296 题意:给你一个最多1000位的数,让你删除k位使得剩下的数最大 收获:贪心 #include<bits/stdc++.h> #define de(x) cout<&l ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- Ubuntu 汉化及kate汉化和使用自带终端的解决方式
汉化方法:1,打开屏幕上方的“system”(系统),里面有个“Administration”(系统管理),选择“Snaptic Package Manager”(软件包管理器),打开,输入密码后进入 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
随机推荐
- JS中split使用方法和数组中元素的删除
JS中split使用方法和数组中元素的删除 JS中split使用方法 <script language="javascript"> function spli(){ d ...
- fixed和absolute
fixed是相对于浏览器窗口固定 absolute是相对于整体网页固定.(整体网页包括所有的内容,包含右侧滑动条滑动所能看到的内容)
- SqlServer 自动备份策略设置
企业管理器中的Tools,Database Maintenance Planner,可以设置数据库的定期自动备份计划.并通过启动Sql server Agent来自动运行备份计划.具体步骤如下: 1. ...
- 基于Spark ML的Titanic Challenge (Top 6%)
下面代码按照之前参加Kaggle的python代码改写,只完成了模型的训练过程,还需要对test集的数据进行转换和对test集进行预测. scala 2.11.12 spark 2.2.2 packa ...
- 一个豆瓣 API 的反向代理配置,旨在解决豆瓣屏蔽小程序请求问题(豆瓣接口 403 问题)
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- Newtonsoft.Json 序列化日期问题解决
上代码 其中的使用方法和UserInfo实体对象就不贴代码了. /// <summary> /// 把对象转成json字符串 /// </summary> /// <pa ...
- LeetCode Weekly Contest 28
1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocode ...
- 题解报告:hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧 ...
- windbg将调试信息保存到文本文件
在跟踪一些出现频率较低的问题时,有时候需要长时间调试,但是在在输出信息太多时可能前面的日志会被清空,为避免这种情况,可以将输出日志记录到文本文件以备查看. 1. 可以在启动时直接用带 -logo的命令 ...
- C#获取窗口大小和位置坐标 GetWindowRect用法
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWi ...