题目链接:https://atcoder.jp/contests/abc128/tasks/abc128_f

题目大意

  给定长度为 N 的序列$s_0, s_1, \dots, s_{N-1}$,现在要选取两个正整数 A 和 B,从$s_0$起跳,按照先往前跳 A 步,再往后跳 B 步的规则正好跳到$s_{N-1}$,每跳到一个地方,其所对应的元素值将会计入你的总分。有如下限制:

  1. 不能跳出序列。
  2. 同一个地方只能被跳到一次。

  请选取适当的 A 和 B,使得得分最大。

分析

  设跳 B 步这个行为进行了 k 次。
  那么$s_{A-B}, s_{2*(A-B)}, \dots, s_{k*(A-B)}$为每次跳 B 步后所能到达的点。
  那么$s_{N-1 - (A-B)}, s_{N-1 - 2*(A-B)}, \dots, s_{N-1 - k*(A-B)}$为每次跳 A 步后所能到达的点。
  可以发现,以上两个序列是一一对应的,唯一变化的只有 A - B 和 k,并且跳 B 步这个行为进行了 k 次可从跳 B 步这个行为进行了 k - 1 次递推而来。
  于是我们可以暴力枚举所有的 A - B 和 k,复杂度大概为$O(N*(1 + \frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{N}))$,几乎是线性的。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef multimap< int, int > MMII;
typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int N, s[maxN];
LL ans; int main(){
INIT();
cin >> N;
Rep(i, N) cin >> s[i]; For(i, , N - ) { // 枚举 A - B
LL ret = ;
For(k, , (N - ) / i) {
LL tmp = N - - i * k;
if(tmp <= i || tmp % i == && tmp / i <= k) break;
ret += s[i * k] + s[tmp];
ans = max(ans, ret);
}
} cout << ans << endl;
return ;
}

最新文章

  1. 【poj2096】 Collecting Bugs
  2. 关于vs调试中kaze时出现的问题
  3. linux驱动的分离分层概念之LED
  4. 网上搜的一个shell中 中文设置的一个样例;
  5. Buzz words
  6. ASMCMD命令
  7. Android 优秀的开源框架整理
  8. Android intent-filter 简单用法
  9. anything vs everything
  10. UVa699 The Falling Leaves
  11. tomcat中jsp编译
  12. hibernate使用sql语句查询实体时,要写上addEntity
  13. MVC 无法将类型“System.Collections.Generic.List&lt;AnonymousType#1&gt;”隐式转换为“System.Collections.Generic.IList&lt;Mvc3Modeltest.Models.Movie&gt;”。存在一个显式转换(是否缺少强制转换?))
  14. cache—主存—辅存三级调度模拟
  15. 二、Cocos2dx概念介绍(游戏开发中不同的坐标系,cocos2dx锚点)
  16. 合并css 合并图片 合并js
  17. 【NOIP2013货车运输】
  18. 在QT中使用C/C++, 在linux下生成 .so, 并调用 .so
  19. HDU2035
  20. python3学习笔记四(列表1)

热门文章

  1. 「CTS2019 | CTSC2019」随机立方体 解题报告
  2. Alibaba Cloud Toolkit,你确定不来尝鲜一下?
  3. 设置select下拉框高度的一种方法
  4. (转)OpenFire源码学习之十八:IOS离线推送
  5. python sort 和sorted排序
  6. jQuery validate验证隐藏表单(hidden)域
  7. Android Telephony分析(六) ---- 接口扩展(实践篇)
  8. docker学习日记一(镜像构建-container commit和image build)
  9. php连接mysql遇到的问题: (HY000/1130) 和 [caching_sha2_password]
  10. python调用tushare获取A股上市公司基础信息