Neko's loop(RMA+循环群)

2024-02-18 05:38
文章标签 loop rma 循环群 neko

本文主要是介绍Neko's loop(RMA+循环群),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem Description

Neko has a loop of size n.
The loop has a happy value ai on the i−th(0≤i≤n−1) grid. 
Neko likes to jump on the loop.She can start at anywhere. If she stands at i−th grid, she will get ai happy value, and she can spend one unit energy to go to ((i+k)modn)−th grid. If she has already visited this grid, she can get happy value again. Neko can choose jump to next grid if she has energy or end at anywhere. 
Neko has m unit energies and she wants to achieve at least s happy value.
How much happy value does she need at least before she jumps so that she can get at least s happy value? Please note that the happy value which neko has is a non-negative number initially, but it can become negative number when jumping.

 

 

Input

The first line contains only one integer T(T≤50), which indicates the number of test cases. 
For each test case, the first line contains four integers n,s,m,k(1≤n≤104,1≤s≤1018,1≤m≤109,1≤k≤n).
The next line contains n integers, the i−th integer is ai−1(−109≤ai−1≤109)

 

 

Output

For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.

 

 

Sample Input

 

2 3 10 5 2 3 2 1 5 20 6 3 2 3 2 1 5

 

 

Sample Output

 

Case #1: 0

Case #2: 2

思路:

在所有的数里面,根据他的规则就将数组分成了若干个循环圈。我们只要在每个圈里找出任意起点能获得的最大开心值,那么所有的开心值取个max,最后和s比较就行啦。既然是圈那么每跑一圈的收获都是确定的,那么我们就可以只计算余数就好了。

可是有这么一种情况,就是说我们要取得数比余数多但又小于一个周期。为了解决这么一个问题,我们可以把其中一个周期放到余数一起计算,这样就会得到最大值啦。然后RMQ来解决最大区间子段和。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
const int maxn = 1e5+666;
ll a[maxn],b[maxn],dp[maxn][20];
int mk[maxn];void ST(ll A[],ll n) {for (int i = 1; i <= n; i++)dp[i][0] = A[i];for (int j = 1; (1 << j) <= n; j++) {for (int i = 1; i + (1 << j) - 1 <= n; i++) {dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);}}
}
ll RMQ(int l, int r) {int k = (int)(log(double(r-l+1))/log((double)2));return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}int main()
{int t_t,cas = 1;scanf("%d",&t_t);while(t_t--){ll n,m,s,k;scanf("%lld%lld%lld%lld",&n,&s,&m,&k);;for(int i = 0; i < n; i++){scanf("%lld",&a[i]);}memset(mk,0,sizeof(mk));ll ans = -inf;for(int i = 0; i < n; i++){if(mk[i]) continue;int x = i;int j = 1;while(!mk[x]){mk[x] = 1;b[j++] = a[x];x = (x+k)%n;}for(int l = 1; l < j; l++){b[l+j-1] = b[l];b[l+2*j-2] = b[l];}b[0] = 0;for(int l = 1; l <= (j-1)*3; l++){b[l] += b[l-1];}ll res = 0;ll M = m;ll fasd = 0;if(m > j-1){M = m-j+1;fasd = j-1;}else M = m;ll mm = m%(j-1)+fasd;ST(b,(j-1)*3LL);ll mx = -inf;for(int l = 1; l < j; l++){ll mmx = 0;ll tmp = b[j-1];if(tmp > 0){mmx += tmp*(M/(j-1));}ll ghf = RMQ(l,l+mm-1)-b[l-1];mmx += ghf;mx = max(mx,mmx);}ans = max(ans,mx);}if(ans < 0) ans = s;else {if(s-ans >= 0) ans = s-ans;else ans = 0;}printf("Case #%d: %lld\n",cas++,ans);}return 0;
}/*
10
6 10 5 2
3 -3 2 3 -4 1*/

 

 

这篇关于Neko's loop(RMA+循环群)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/720206

相关文章

myEclipse失去焦点时报错Unhandled event loop exception的解决方案

一句话:百度杀毒惹的祸。。。。果断卸载后问题解决。

多表连接的三种方式hash join,merge join,nested loop

多表之间的连接有三种方式:Nested Loops,Hash Join和 Sort Merge Join. 下面来介绍三种不同连接的不同:     一. NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择。在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表中检索找到与它匹配的行,因此整个查询返回的结果集不能太大(大于1 万不适合),要把返回

vivado error:Combinatorial Loop Alert:1 LUT cells form a combinatorial loop

VIVADO ERROR :Combinatorial Loop Alert:1 LUT cells form a combinatorial loop vivao生成bit流时发生报错,如下图所示定位原因解决 vivao生成bit流时发生报错,如下图所示 定位原因 在三段式状态机中,组合逻辑代码if else 语句未写全只写了if…elsif…,没有写else,导致错误

js- 宏微任务和事件loop

宏微任务和事件loop 目录 文章目录 前言推荐阅读宏微任务的定义宏微任务的区别常见面试代码 宏任务微任务`Event-Loop`在浏览器中的表现`Node`中的表现setImmediate与setTimeout的区别`process.nextTick``async/await`函数小节 前言 面试常问三问题宏微任务面对异步事件宏微事件、Event-Loop 推荐阅读 j

Scala并发编程react、loop代码实战详解

示例代码及注释: //scala并发编程中的react和loop,共同特点://通过线程存用的方式让性能有所提升。//Actor本身的运行,被actor子系统管理的时候,会有一个或者多个远程的线程让当前的actor使用//一般情况下每个Actor都有自己的线程。只有有自己的线程时,我们的Actor中的actor方法才会执行。//但是,这样线程的开销会非常大,所以为了共用线

oracle存储过程Loop循环一张表插入到另外一张表

oracle存储过程Loop循环一张表插入到另外一张表   1、创建一个存储过程   Sql代码   create or replace procedure inserttest as   cursor cs is SELECT sales_id FROM t02salesinfo_backup;sales_id varchar(128);   begin   for c in c

汇编语言04——[BX]和loop指令

整理自fishcc论坛课件 首先展示一个新的程序: assume cs:codesg codesg segment start: mov ax,2000H mov ds,ax mov al,[0] mov bl,[1] mov cl,[2] mov dl,[3] mov ax,4C00H int 21H codesg ends end start 使用masm进行

sh handle_data.sh: 2: handle_data.sh: Syntax error: Bad for loop variable

今天写了个简单shell处理数据,如下: #!/bin/shfor((i=1;i<220;i++));do/usr/bin/php /var/artisan handle_data 1;done; 结果报错 sh handle_data.sh: 2: handle_data.sh: Syntax error: Bad for loop variable 查询后发现是Ubun

node.js中Event loop机制

(1)所有同步任务都在主线程上执行,形成一个执行栈 (2)主线程之外,还存在一个"任务队列",只要异步任务有了运行结果,就在"任务队列"之中放置一个事件。 (3)一旦"执行栈"中的所有同步任务执行完毕,系统就会读取"任务队列",看看里面有哪些事件。那些对应的异步任务,于是结束等待状态,进入执行栈,开始执行。 (4)主线程不断重复上面的第三步。 "任务队列"是一个事件的队列(

Range-Based For Loop(范围基于 for 循环)的使用方法

在 C++11 中,引入了一种新的循环结构——范围基于 for 循环(Range-Based For Loop)。这种语法旨在简化遍历容器(如 vector、list、array 等)中的所有元素的过程,使代码更加简洁和易读。范围基于 for 循环允许开发者以更直观的方式遍历容器中的元素。与传统的 for 循环相比,它不再需要显式地使用索引或迭代器,使代码更加简洁和可读。 目录 范围基于