orz 最近被水题卡+FST,各种掉rating

题目大意

一个数s它是n进制的,但是每一位不是用'A','B'....表示的,而是用10,11等等表示的,将它还原成十进制

这种表示方法显然会产生多解,然后求所有的解中最小的那个

这题一想就是贪心,但是一开始算法写渣了,改变思路以后才A的

简单来说就是从右边开始,把尽量多的数压到一位里面,这样会使数更小

压的思路可以这么考虑

每次新加进来一个数,如果加进来以后,已经大于原来的那个数,那么就找截取一段可行的最大数

这样考虑的目的是处理前导0带来的影响

0也有可能是单独的一位,也有可能与其他数压到同一位里

想清楚这个以后就不难写了

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
string str, n;
ll last, w;
bool compare(int x, int y)
{
if(y-x > n.length()) return true;
if(y-x < n.length()) return false;
for(int i = x; i < y; i++)
{
if(str[i] == n[i-x]) continue;
if(str[i] > n[i-x]) return true;
else return false;
}
return true;
}
ll cal(string str, int x, int y)
{
ll ans = ;
for(int i = x; i < y; i++)
ans = ans* + str[i] - '';
return ans;
}
int main()
{
cin>>n;
cin>>str;
ll ans = , k = ;
last = str.length();
for(int i = str.length()-; i >= ; i--)
{
while(compare(i, last))
{
int j;
for(j = i; j < last; j++)
{
if(str[j] == '') continue;
if(compare(j, last)) continue;
j++; break;
}
j--;
ans += cal(str, j, last)*k;
last = j;
k *= cal(n, , n.length());
}
}
if(last != ) ans += cal(str, , last)*k;
cout<<ans<<endl;
}

Codeforces Round #392(div 2) 758D (贪心)的更多相关文章

  1. Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

    D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...

  2. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  3. Codeforces Round #547 (Div. 3) F 贪心 + 离散化

    https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...

  4. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

  5. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  6. Codeforces Round #303 (Div. 2) D 贪心

    D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  7. Codeforces Round #392 (Div. 2)

    D题,给出n,k,k是n进制数,但是大于十进制时,它的表示方法仍为十进制那种,比如16进制下的15,我们可以看成就是15,或者1|5,也就是1×16+5 = 21,让你求出能表达的最小十进制数 从后面 ...

  8. Codeforces Round #545 (Div. 2) D 贪心 + kmp

    https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...

  9. Codeforces Round #547 (Div. 3) G 贪心

    https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...

随机推荐

  1. 知识总结和记录——Bootstrap

    官方地址:https://getbootstrap.com 中文地址:http://www.bootcss.com/ 使用V3版本的Bootstrap,下载的是用于生产环境的Bootstrap. 目录 ...

  2. top小火箭

    // my.js function $(id){return document.getElementById(id)};function show(obj){obj.style.display = & ...

  3. Fetch 头像剪切修改

    前言:通过Input file upload 图片到canvas 中进行剪裁,react 可以引入react-avatar-editor对图片进行剪裁 react-avatar-editor的使用 & ...

  4. Spring Cloud 入门 Consul-Client服务提供

    前面介绍了 Rureka Client服务提供, 只需要改pom.xml部分内容 1.pom.xml <?xml version="1.0" encoding="U ...

  5. java后台poi根据模板导出excel

    public class ExcelUtils { private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = " ...

  6. Cloudera Manager 安装 CDH5

    文档说明 本文是针对Linux CentOS6服务器与CDH5.15的安装手册. 关于CDH和ClouderaManager CDH(Cloudera's Distribution, includin ...

  7. IE6兼容png图片

    <!--[if IE 6]> <script src="/js/DD_belatedPNG.js"></script> <script&g ...

  8. php 操作 mysql 实现批量执行mysql语句 mysql文件

    <?php /** * 批量运行sql文件 * 正则分隔是重点 preg_split("/;[\r\n]+/", filecontent) */ $config = requ ...

  9. 猴子吃桃问题 python

    猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,见只剩下一个桃子了.求第 ...

  10. 30-RoutingMiddleware介绍以及MVC引入

    1-构建路由 public class Startup { // This method gets called by the runtime. Use this method to add serv ...