博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[dp][前缀和][并查集] 洛谷 P3575 DOO-Around the world
阅读量:5263 次
发布时间:2019-06-14

本文共 3344 字,大约阅读时间需要 11 分钟。

题目描述

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\ 0002n1 000 000, 1\le s\le 1001s100),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++ln109), 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_n1dil1+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 42 2 1 3 3 13 2 4 11
输出样例#1:
4NIE32

 

题解

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

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

     

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

代码

1 #include 
2 #include
3 #include
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
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

你可能感兴趣的文章
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>
卷积中的参数
查看>>
51nod1076 (边双连通)
查看>>
ViewPager的onPageChangeListener里面的一些方法参数:
查看>>
Jenkins关闭、重启,Jenkins服务的启动、停止方法。
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>
2019春 软件工程实践 助教总结
查看>>
Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
查看>>
多线程实现资源共享的问题学习与总结
查看>>
java实现哈弗曼树
查看>>
程序的静态链接,动态链接和装载 (补充)
查看>>
关于本博客说明
查看>>
线程androidAndroid ConditionVariable的用法
查看>>
转载:ASP.NET Core 在 JSON 文件中配置依赖注入
查看>>
代码变量、函数命名神奇网站
查看>>
redis cli命令
查看>>
Problem B: 占点游戏
查看>>
python常用模块之sys, os, random
查看>>