CodeForces 346C Number Transformation II
题解:
对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数。
也就是说 [ k * x[i] + 1, (k+1)* x[i] -1 ]这段区间的的数都会走到 k * x[i]上。
所以对于每个位置都先计算出他到右边最远的覆盖位置。
然后在反着求出每个位置能往左走走到的最远的位置。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("___.txt","r",stdin);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e6 + ;
const int M = 3e7;
vector<int> in[N], out[N];
int x[N], to[N], rto[N];
int n;
int main(){
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &x[i]);
int f = ;
int a, b;
scanf("%d%d", &a, &b);
sort(x+, x++n);
n = unique(x+, x++n) - (x+);
for(int i = ; i <= a-b; ++i) rto[i] = i + ;
for(int i = ; i <= n; ++i){
LL start = b / x[i] * x[i];
if(start < b) start += x[i];
while(start < a){
LL r = start + x[i] - ;
if(r > a) r = a;
rto[start-b] = max(rto[start-b], (int)(r-b));
start = r + ;
}
}
// cout << "ok1" << endl;
for(int l = , r = ; l <= a-b; ++l){
int nr = rto[l];
while(r <= nr){
to[r] = l;
++r;
}
}
int ans = , now = a-b;
while(now){
ans++;
now = to[now];
}
cout << ans << endl; return ;
}
CodeForces 346C Number Transformation II的更多相关文章
- Codeforces 346C Number Transformation II 构造
题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...
- Codeforces 346C Number Transformation II 贪心(复杂度计算)
题意及思路:https://www.cnblogs.com/liuzhanshan/p/6560499.html 这个做法的复杂度看似是O(n ^ 2),实际上均摊是O(n)的.我们考虑两种极端数据: ...
- CodeForces346 C. Number Transformation II
C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces 251C Number Transformation
Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...
- cf201.div1 Number Transformation II 【贪心】
1 题目描述: 被给一系列的正整数x1,x2,x3...xn和两个非负整数a和b,通过下面两步操作将a转化为b: 1.对当前的a减1. 2.对当前a减去a % xi (i=1,2...n). 计算a转 ...
- CSUOJ 1299 - Number Transformation II 打表预处理水DP
http://122.207.68.93/OnlineJudge/problem.php?id=1299 第二个样例解释.. 3 6 3->4->6..两步.. 由此可以BFS也可以DP. ...
- Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜
题意及思路:https://blog.csdn.net/bossup/article/details/37076965 代码: #include <bits/stdc++.h> #defi ...
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- 4.Single Number && Single Number (II)
Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...
随机推荐
- ansible批量管理服务 上
1 ansible简介 1.1 ansible批量管理服务概述 (1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方 ...
- Iphone使用过程中遇到的问题
Q1:同一个Apple ID不同设备之间的通话记录保持同步 解决方法: Step1:"设置"--"电话"--"在其他设备上通话"--选择关闭 ...
- Java基础的一些知识点(一):接口interface
1.接口的含义 接口可以理解成统一的协议, 而接口中的属性也属于协议中的内容.但是接口的属性都是公共的,静态的,最终的. 接口的成员特点: 1.成员变量只能是常量,默认修饰符 public stati ...
- Netty学习(九)-Netty编解码技术之Marshalling
前面我们讲过protobuf的使用,主流的编解码框架其实还有很多种: ①JBoss的Marshalling包 ②google的Protobuf ③基于Protobuf的Kyro ④Apache的Thr ...
- sql语句优化:尽量使用索引避免全表扫描
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Mybatis学习笔记之---动态sql中标签的使用
动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...
- web面试
什么是面向对象? 程序中的事物都是用对象结构来描述的,所有的程序都是用面向对象的思想,管理数据和功能的. 面向对象三大特点:封装 继承 多态 什么是封装? 创建一个对象结构,用来保存一个事物的属性和方 ...
- JavaScript数据结构——图的实现
在计算机科学中,图是一种网络结构的抽象模型,它是一组由边连接的顶点组成.一个图G = (V, E)由以下元素组成: V:一组顶点 E:一组边,连接V中的顶点 下图表示了一个图的结构: 在介绍如何用Ja ...
- redis 有没有ACID事务
看redis官网的介绍: redis确实是有事务的,但是和传统的ACID是否相同呢? 原子性(Atomicity) 原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 一致 ...
- 基于注解的SpringAOP源码解析(三)
注意,读完本篇文章需要很长很长时间 在之前的2篇文章:AOP源码分析(一)AOP源码分析(二) 中,我们搭建了SpringAOP源码分析的环境,介绍了@EnableAspectJAutoProxy注解 ...