题目描述

After trying hard for many years, Byteasar has finally received a pilot license.

To celebrate the fact, he intends to buy himself an airplane and fly around the planet 3-SATurn (as you may have guessed, this is the planet on which Byteotia is located).

Specifically, Byteasar plans to fly along the equator.

Unfortunately, the equator is rather long, necessitating refuels.

The flight range (on full tank) of each aircraft is known.

There is a number of airports along the equator, and a plane can be refueled when it lands on one.

Since buying an airplane is a big decision, Byteasar asks your help.

He is about to present you with a list of different plane models he is considering.

Naturally, these differ in their flight range.

For each plane model, he would like to know the minimum number of landings (including the final one) he would have to make in order to complete the journey.

Note that for each airplane model, the journey may start at a different airport.

通过几年的努力,Byteasar最终拿到了飞行员驾驶证。为了庆祝这一事实,他打算买一架飞机并且绕Byteotia星球赤道飞行一圈。但不幸的是赤道非常长所以需要中途加几次油。现在已知赤道上面所有飞机场,所有飞机从飞机场起飞降落也可以加油。因为买飞机是个十分重大的决定,Byteasar决定寻求你的帮助。他将会让你模拟不同的飞行路线。自然这些飞机一次能走的航程是不同的。对于每次模拟,他想要知道最少需要降落多少次(包括最后一次)。需要注意的是起点可以任意选取。

输入输出格式

输入格式:

The first line of the standard input contains two integers nn and ss (2\le n\le 1\ 000\ 0002≤n≤1 000 000, 1\le s\le 1001≤s≤100),separated by a single space, denoting the number of airports along the equator and the number of airplane models Byteasar is considering.

The second line contains nn positive integers l_1,l_2,\cdots,l_nl1​,l2​,⋯,ln​ (l_1+l_2+\cdots+l_n\le 10^9l1​+l2​+⋯+ln​≤109), separated by single spaces, specifying the distances between successive airports along the equator.

The number l_ili​ is the distance between the ii-th and (i+1)(i+1)-st (or nn-th and first if i=ni=n) in kilometers.

The third line contains ss integers d_1,d_2,\cdots,d_sd1​,d2​,⋯,ds​ (1\le d_i\le l_1+l_2+\cdots+l_n1≤di​≤l1​+l2​+⋯+ln​), separated by single spaces. The number d_idi​ is the ii-th airplane model's flight range in kilometers, i.e., the maximum distance it can fly before landing and refueling.

输出格式:

Your program should print ss lines to the standard output: the ii-th of these should contain a single integer, namely, the minimum lumber of flight segments (and thus also landings) necessary to fly the ii-th airplane around the planet 3-SATurn along the equator, starting at an airport of choice, or the word NIE (Polish for no) if it is impossible to complete the journey with this airplane.

输入输出样例

输入样例#1:

6 4
2 2 1 3 3 1
3 2 4 11

输出样例#1:

4
NIE
3
2

题解

  • 首先,我们可以把环破开,将链增长一倍,求出前缀和
  • 对于每个询问,设油量为d

  • 先预处理出每个点走一次最多走到哪,这个用尺取法可以O(n)

  • 然后得到一颗树,算一下每个点的深度,枚举起点,在树上一直向上爬,直到距离超过n,爬的过程同时用并查集合并,可以用dp来记录答案

代码

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #define N 2000010
 5 using namespace std;
 6 int n,m,mx,x,fa[N],sum[N],f[N];
 7 int main()
 8 {
 9     scanf("%d%d",&n,&m);
10     for (int i=1,x;i<=n;i++) scanf("%d",&x),fa[i]=i,mx=max(mx,x),sum[i]=sum[i-1]+x;
11     for (int i=n+1;i<=n+n;i++) sum[i]=sum[i-1]+sum[i-n]-sum[i-n-1];
12     while (m--)
13     {
14         scanf("%d",&x);
15         if (x<mx) { printf("NIE\n"); continue; }
16         for (int i=n+1,j=1;;i++)
17         {
18             while (sum[i]-sum[j]>x) j++;
19             f[i]=f[j]+1,fa[i]=fa[j];
20             if (i-fa[i]>=n) { printf("%d\n",f[i]); break; }
21         }
22     }
23 }

转载于:https://www.cnblogs.com/Comfortable/p/10340456.html

[dp][前缀和][并查集] 洛谷 P3575 DOO-Around the world相关推荐

  1. 并查集模板——并查集(洛谷 P3367)

    题目选自洛谷P3367 是并查集的最入门的题目,也是并查集的模板题~~ 如果你还不知道并查集是什么? 请看我的这篇文章 点击链接 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入格式 ...

  2. 用C语言构造康托集,洛谷——P1014 Cantor表

    P1014 Cantor表 题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 - 2/1 ...

  3. 洛谷--橙色百道DP总结

    最近刷完了洛谷橙色DP大约一百道,算是发现了一些套路,就部分题目做一些总结. 大概分为三类 第一类,九大背包及其衍生 第二类,经典DP模型,如LCS,LIS等 第三类,实际问题背景的普通,环形,树上D ...

  4. 洛谷 深基 第4部分 基础数学与数论(19-21课)

    洛谷 深基 第4部分 基础数学与数论  第19章 位运算与进制转换 P1143 进制转换 https://www.luogu.com.cn/problem/P1143 洛谷P1143 进制转换的Pyt ...

  5. 最短路分层图专题 洛谷P2939,4822,4568

    2020.6.3 今天主要练习了下分层图.看洛谷题解每次都能有新收获.今天本来想练dp,后来感觉可能会太自闭了,不如先来一发最短路,毕竟看家本领不能忘.然后点进了北京某年wc的一道题,让求1-n的最短 ...

  6. 洛谷 U266184 宠物小精灵之收服

    题目链接:宠物小精灵之收服 - 洛谷 标签:dp.背包问题 思路: 精灵球个数 与 对皮卡丘的伤害 都可以看成背包问题中的体积,所以将dp数组升级为二维 dp[i][j]表示 i个精灵球 j滴血皮卡丘 ...

  7. 【无码专区9】序列统计(带权并查集 + 前缀和建边 + dp)

    因为只有std,没有自我实现,所以是无码专区 主要是为了训练思维能力 solution才是dls正解,但是因为只有潦草几句,所以大部分会有我自己基于正解上面的算法实现过程,可能选择的算法跟std中dl ...

  8. 并查集——营救(洛谷 P1396)

    题目选自洛谷P1396 看到最大的最小很多人想到二分,但是可以有更好的解法. 我们不妨用并查集维护这个图,将边从小到大排序,每次取出边权最小的边,若该边的起点与终点未在一个集合内,就将其合并.当源点与 ...

  9. 并查集——程序自动分析(洛谷 P1955)

    题目选自洛谷P1955 比较复杂一点的并查集题目,还用到了离散化的思想 首先明确一点:相等具有传递性,不相等不具有传递性(Eg:若x1等于x2,x2等于x3时,显然x1=x3.但当x1不等于x2,x2 ...

最新文章

  1. 架设httpd服务器
  2. 【技术综述】有三说GANs(上)
  3. 李彦宏妻子马东敏向中国科大捐赠1亿 成立蔷薇基金
  4. 用 matlab 爬取期刊影响因子
  5. html设置table border,用css来设置table的border
  6. Python3实现红黑树[下篇]
  7. error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js
  8. 有限状态机的C++实现(2)-bayonet开源网络服务器框架
  9. 【ClickHouse】ClickHouse 同步 MySQL 数据库
  10. [翻译]XNA在线俱乐部网站即将开站!
  11. ADS仿真 之 交流仿真和S参数仿真示例
  12. Oracle数据库数据恢复方法
  13. 快慢指针(Fast and Slow Pointers)
  14. 2022年施工员-市政方向-通用基础(施工员)考试题模拟考试平台操作
  15. MATLAB新手简明使用教程(七)——使用matlab建立多项式以及求导,商求导乘积求导等——新手来看,保证看懂。
  16. 单向链表—在单向链表的尾部插入一个元素
  17. 如何刷新本机DNS缓存(Win+Linux+OSX)
  18. 婴儿用品的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  19. AXI总线技术简介——ZYNQ PS和PL的互联技术
  20. pyinstaller : 无法将“pyinstaller”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。所在位置 行:1

热门文章

  1. C++ find函数详解
  2. MatLab画直方图并填充纹理
  3. c 语言密钥存储,在C中读取和写入rsa密钥到pem文件
  4. 局域网有几台电脑频繁断网_一张地图肝了17年,曾经宿舍10点前要断网,但这游戏却能开黑通宵...
  5. 华为面试题错题集总结,你的Java基础过关了吗?
  6. 为了防止这个宅男最爱网站消失,我连夜用Python离线了上万张图片
  7. 第七次霍乱大流行致病弧菌的两个防御系统
  8. 【AMQP】rabbit mq 几种模式的用法
  9. 高德全链路压测平台TestPG的架构与实践
  10. JavaScript入门基础学习总结