【t053】整数去位
Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
键盘输入一个高精度的正整数N,去掉其中任意M个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的N和M寻找一
种方案使得剩下的数字组成的新数最小。输出组成的新的正整数。
输入数据均不需判错。如果去掉了某几个位后得到的新整数开头为0,保留0。
【输入格式】
第一行为高精度正整数N(N的长度不超过10^6位)
第二行为M(0<=M<=N的长度)
【输出格式】
去掉M位后的最小新数。
Sample Input
82386782
3
Sample Output
23672
Sample Input2
505
1
Sample Output2
05
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t053
【题解】
http://blog.csdn.net/harlow_cheng/article/details/51870979?locationNum=8&fps=1
是这题的升级版;
找到从左往右第一个递减区间的第一个数字把它删掉就好;
因为找到那个数字之后、前面的数字仍然是递增的,所以没必要再往前找;
直接再往后找就可以了;
用链表模拟删除的过程以节省时间;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXN = 1e6+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
struct abc
{
int l,r;
};
char s[MAXN];
int m;
abc a[MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%s",s+1);
scanf("%d",&m);
int len = strlen(s+1);
if (m==len)
{
puts("0");
return 0;
}
rep1(i,1,len)
a[i].l = i-1,a[i].r=i+1;
a[0].r=1;
int i = 1;
while (m)
{
while (a[i].r<=len && s[i]<=s[a[i].r])
i=a[i].r;
if (i==len)
{
a[a[i].l].r = len+1;
i=a[i].l;
m--;
}
else
{
a[a[i].l].r = a[i].r;
a[a[i].r].l = a[i].l;
i=a[i].l;
m--;
}
}
int now = a[0].r;
while (now!=len+1)
{
putchar(s[now]);
now = a[now].r;
}
return 0;
}
【t053】整数去位的更多相关文章
- 【bzoj4942】[Noi2017]整数 压位+线段树
题目描述 P 博士将他的计算任务抽象为对一个整数的操作. 具体来说,有一个整数 $x$ ,一开始为0. 接下来有 $n$ 个操作,每个操作都是以下两种类型中的一种: 1 a b :将 $x$ 加上整数 ...
- BZOJ 4942 NOI2017 整数 (压位+线段树)
题目大意:让你维护一个数x(x位数<=3*1e7),要支持加/减a*2^b,以及查询x的第i位在二进制下是0还是1 作为一道noi的题,非常考验写代码综合能力,敲+调+借鉴神犇的代码 3个多小时 ...
- LOJ 2302 「NOI2017」整数——压位线段树
题目:https://loj.ac/problem/2302 压30位,a最多落在两个位置上,拆成两次操作. 该位置加了 a 之后,如果要进位或者借位,查询一下连续一段 0 / 1 ,修改掉,再在含有 ...
- 【LeetCode 231_整数_位运算】Power of Two
bool isPowerOfTwo(int n) { && !(n & (n - )); }
- UOJ #314. 【NOI2017】整数 | 线段树 压位
题目链接 UOJ 134 题解 可爱的电音之王松松松出的题--好妙啊. 首先想一个朴素的做法! 把当前的整数的二进制当作01序列用线段树维护一下(序列的第i位就是整数中位权为\(2^k\)的那一位). ...
- Java编程的逻辑 (4) - 整数的二进制表示与位运算
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理
原文:SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理 SQL Server 字段类型 decimal(18,6)小数点前是几位? 不可否认,这是 ...
- mysql中整数类型后面的数字,比如int(11),11代表11个字节吗?
原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...
- c 整数运算
一.无符号加法(形式的模运算,无符号加法等价于计算模2w 的和) 示例:非负数 x 和 y 位数: w(8位机) 范围: 0 <= x,y <= 2w -1 结果:0 <= x+y ...
随机推荐
- 理性分析 C++(-O2) 和 JS 的性能差距
laptop: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz.. Test1: 最后一行:时间(ms) #pragma GCC optimize("O2& ...
- (转)Tomcat文件详解
做web项目,最常用的服务器就是Apache的tomcat.虽然一直在用tomcat,但都是仅限在使用的阶段,一直没有深入学习过.想深入学习tomcat,首推的肯定是官网:http://tomcat. ...
- 【Codeforces Round #429 (Div. 2) A】Generous Kefa
[Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...
- GraphX 图数据建模和存储
背景 简单分析一下GraphX是怎么为图数据建模和存储的. 入口 能够看GraphLoader的函数. def edgeListFile( sc: SparkContext, path: String ...
- JPA的配置文件persistence.xml参数详解
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0&q ...
- COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools
http://www.cogs.pro/cogs/problem/problem.php?pid=908 || https://www.luogu.org/problem/show?pid=27 ...
- Altium Designer如何创建类,如何修改线宽
如果线宽还是没有改变,就是因为,有一个默认的class,它的规则优先级要高于PWR
- python分解质因数
将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. # !/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...
- multi_input_paths
- springboot整合freemarker(转)
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...