题面

B - Moderate Differences


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There are N squares in a row. The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.

Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:

  • For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).

As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares. Determine whether it is possible to fill the squares under the condition.

Constraints

  • \( 3≤N≤500000 \)
  • \( 0≤A≤10^{9} \)
  • \( 0≤B≤10^{9} \)
  • \( 0≤CD≤10^{9} \)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B C D

Output

Print YES if it is possible to fill the squares under the condition; print NO otherwise.


Sample Input 1

Copy
5 1 5 2 4

Sample Output 1

Copy
YES

For example, fill the squares with the following integers: 1−1375, from left to right.


Sample Input 2

Copy
4 7 6 4 5

Sample Output 2

Copy
NO

Sample Input 3

Copy
48792 105960835 681218449 90629745 90632170

Sample Output 3

Copy
NO

Sample Input 4

Copy
491995 412925347 825318103 59999126 59999339

Sample Output 4

Copy
YES

题面大意(取自__stdcall)

有N个格子排成一排,最左边的里面写了数字A,最右边的写了数字B,中间的 格子都是空的。 你需要在中间的每个格子里填上一个数字,使得这个序列中,任意相邻两个数 的差的绝对值在[C,D]之间。 问是否存在这样一种可行的填数方案,输出YES或者NO。

题解

本题数据范围较大,显然直接暴力会出现TLE的惨案

因为输入数据量不多,显然可以考虑利用合理的构造解决这个问题

那么如何构造呢?

我首先考虑了构造一组加减,使得A的数字可以被继承到B旁边,在继承的过程中将A和B的值之间差的绝对值缩小到\( \left [ C ,D \right ]  \),但是这个思路虽然想起来非常自然却不是很好实现

我们需要更加优美的解决方案

首先考虑题目的条件,题目条件显然是在指要填数,使得每个数都满足 \( C \ \le \  \left | x_{i+1} \ - \ x_{i} \right | \ \le \ D,i \ \in \left [ 2,n \right ] \)

那么观察这个简单的式子 \( x_{i+1}-x_{i} \)

我们易得\( \sum (x_{i+1}-x_{i}) \ = \ x_{i}-x_{1} \),即\( B-A \)

观察到这一重要性质之后,不难发现我们的解一定与\( B-A \)有关

于是我们考虑一个简化的问题

有N个格子排成一排,最左边的里面写了数字A,最右边的写了数字B,中间的 格子都是空的。 你需要在中间的每个格子里填上一个数字,使得这个序列中,任意相邻两个数 的差的绝对值在[0,D]之间。 问是否存在这样一种可行的填数方案,输出YES或者NO。

这个问题失去了C的下界,是不是很好解决?

我们可以很容易的计算出\( B-A \),所以只要在A到B的空间中,每个增加D或不增加,存在任意一个方案可以使最终\( x_{n}-x_{n-1} \)的值小于D即可

由此,只要让原问题变得无下限失去下限,问题就很好解决了呢

所以我们可以再考虑一种简单情况,如果所有的决策如果要改变大小C的值都只能加C,那么如何让这个问题失去下限?

很简单,把每个都减去C就好了

但问题是我们现在不知道到底这个决策中有多少个是+C,有多少个是-减c啊QWQ

这个时候就要枚举了

我们进行一些浅显的数学运算

假设现在存在\( K \)个满足条件\( C \le x_{i+1}-x_{i} \le D \)的式子,则因为总共要满足\( n-1 \)个连续的条件,所以一定存在\( n-K-1 \)个满足 \( -D \le x_{i+1}-x_{i} \le -C \)的式子

由于\( \sum (x_{i+1}-x_{i}) \ = \ x_{i}-x_{1} \  = \ B-A \),我们把这些式子加和,可以得到

\( K*C-(n-K-1)*d \le B-A \le K*D-(n-k-1)*C \)的结论

我们只需要枚举K计算有无方案即可

扔代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
long long n,a,b,c,d;
int main(){
while(scanf("%lld",&n)==){
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
for(int i=;i<=n-;i++){
if((b-a)>=(i*c-(n-i-)*d)&&(b-a)<=(i*d-(n-i-)*c)){
printf("YES");
return ;
}
}
}
printf("NO");
return ;
}

题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)的更多相关文章

  1. AtCoder Grand Contest 017 题解

    A - Biscuits 题目: 给出 \(n\) 个物品,每个物品有一个权值. 问有多少种选取方式使得物品权值之和 \(\bmod\space 2\) 为 \(p\). \(n \leq 50\) ...

  2. AtCoder Grand Contest 017 (VP)

    contest link Official Editorial 比赛体验--之前做题的时候感觉 AtCoder 挺快的,现在打了VP之后发现还是会挂的--而且不是加载缓慢或者载不出来,直接给你一个无法 ...

  3. AtCoder Grand Contest 017

    noi前橙名计划失败.全程搞C而gg…… A - Biscuits 题意:背包,求价值为奇/偶的方案数. #include<cstdio> #include<queue> #i ...

  4. AtCoder Grand Contest 017 B

    B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stateme ...

  5. AtCoder Grand Contest 017题解

    传送门 \(A\) 直接转移就是了 typedef long long ll; const int N=55; ll f[N][2];int a[N],n,p; int main(){ scanf(& ...

  6. AtCoder Grand Contest 017 F - Zigzag

    题目传送门:https://agc017.contest.atcoder.jp/tasks/agc017_f 题目大意: 找出\(m\)个长度为\(n\)的二进制数,定义两个二进制数的大小关系如下:若 ...

  7. AtCoder Grand Contest 017 迟到记

    晚上去操场上浪. 回来以后看到好几个人开着 \(AtCoder\) 在打代码. ... ... 今天有 \(AtCoder\) 比赛 ? 管它呢, \(Kito\) 在切西瓜,先吃西瓜... 然后看 ...

  8. AtCoder Grand Contest 017 A

    Problem Statement There are N bags of biscuits. The i-th bag contains Ai biscuits. Takaki will selec ...

  9. AtCoder Grand Contest 027 (AGC017) D - Modulo Matrix 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC027C.html 题解 首先我们假装 max mod min = 1 然后对着这个构造. 将各自黑白染色, ...

随机推荐

  1. Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现

    https://blog.csdn.net/zouxy09/article/details/9993371 自己平时看了一些论文,但老感觉看完过后就会慢慢的淡忘,某一天重新拾起来的时候又好像没有看过一 ...

  2. Java基础语法(一 )

    一.关键字 关键字概述 被Java语言赋予特定含义的单词 关键字特点 组成关键字的字母全部小写 关键字注意事项 goto和const作为保留字存在,目前并不使用 关键字单词 用于定义数据类型的关键字 ...

  3. 浅谈大数据与hadoop家族

    按照时间的早晚从大数据出现之前的时代讲到现在.暂时按一个城市来比喻吧,反正Landscape的意思也大概是”风景“的意思. 早在大数据概念出现以前就存在了各种各样的关于数学.统计学.算法.编程语言的研 ...

  4. Vim 文本编辑器的基本使用

    Vim文本编辑器是Linux/Unix系统下最常用的工具之一,通过该工具可以很方便的建立.修改.编辑文档或者程序文件,其作用类似与windows系统下的记事本或者notepad++,因此熟练掌握该工具 ...

  5. 吴恩达深度学习笔记(deeplearning.ai)之卷积神经网络(CNN)(上)

    作者:szx_spark 1. Padding 在卷积操作中,过滤器(又称核)的大小通常为奇数,如3x3,5x5.这样的好处有两点: 在特征图(二维卷积)中就会存在一个中心像素点.有一个中心像素点会十 ...

  6. localstorage跨域解决方案

    localstorage也存在 跨域的问题, [解决思路如下] 在A域和B域下引入C域,所有的读写都由C域来完成,本地数据存在C域下; 因此 A哉和B域的页面必定要引入C域的页面; 当然C域最好是主域 ...

  7. Step6:SQL Server 数据变更时间戳(timestamp)在复制中的运用

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 方案(Solution) 方案一(Solution One) 方案二(Solution Two ...

  8. 怎样从外网访问内网SQLServer数据库?

    本地安装了一个SQLServer数据库,只能在局域网内访问到,怎样从外网也能访问到本地的SQLServer数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动SQLServer数据 ...

  9. 打印一个浮点值%f和%g

    详见代码 后续或有更新 #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { fl ...

  10. pyenv安装

    petalinux-config出错 以为是pyenv的问题,后发现不是,把pyenv的安装卸载总结如下: 折腾了半天发觉是安装了pyenv导致的python版本混乱,卸载后问题解决了.(卸载过程见h ...