题目链接

传送门

题意

初始时有\(n\)堆石子,每堆石子的石子个数为\(a_i\),然后进行游戏。

游戏规则为你可以选择任意两堆石子,然后从这两堆中移除一个石子,最后石子个数变为\(0\)则获胜否则失败。由于总石子个数可能为奇数,此时不可能获胜,因此加了个规则为如果石子个数为奇数,那么可以事先移除一个石子。

问你有多少个区间能让玩游戏的人获胜。

思路

经过模型转换后题意变为有多少个区间,区间内石子个数之和大于等于石子最大数的两倍。

启发式分治,大体处理方法和这题一样。

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 300000 + 2;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; LL ans;
int _, n;
LL sum[maxn];
int a[maxn], dp[maxn][20], pos[maxn][20], lg[maxn]; void init() {
lg[0] = -1;
for(int i = 1; i <= n; ++i) lg[i] = lg[i>>1] + 1;
for(int j = 1; j <= lg[n]; ++j) {
for(int i = 1; i + (1<<j) - 1 <= n; ++i) {
if(dp[i][j-1] >= dp[i+(1<<(j-1))][j-1]) {
dp[i][j] = dp[i][j-1];
pos[i][j] = pos[i][j-1];
} else {
dp[i][j] = dp[i+(1<<(j-1))][j-1];
pos[i][j] = pos[i+(1<<(j-1))][j-1];
}
}
}
} int query(int l, int r) {
int k = lg[r-l+1];
if(dp[l][k] >= dp[r-(1<<k)+1][k]) return pos[l][k];
else return pos[r-(1<<k)+1][k];
} void solve(int l, int r) {
if(l >= r) return;
if(l + 1 == r) {
ans += (a[l] == a[r]);
return;
}
int pos = query(l, r);
if(r - pos > pos - l) {
for(int i = l; i <= pos; ++i) {
int ub = r, lb = pos + 1, mid, pp = -1;
if(i != pos) lb = pos;
while(ub >= lb) {
mid = (ub + lb) >> 1;
if(sum[mid] - sum[i-1] >= 2 * a[pos]) {
pp = mid;
ub = mid - 1;
} else {
lb = mid + 1;
}
}
if(pp == -1) continue;
ans += (r - pp + 1);
}
} else {
for(int i = pos; i <= r; ++i) {
int ub = pos - 1, lb = l, mid, pp = -1;
if(i != pos) ub = pos;
while(ub >= lb) {
mid = (ub + lb) >> 1;
if(sum[i] - sum[mid-1] >= 2 * a[pos]) {
pp = mid;
lb = mid + 1;
} else {
ub = mid - 1;
}
}
if(pp == -1) continue;
ans += (pp - l + 1);
}
}
solve(l, pos - 1), solve(pos + 1, r);
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &_);
while(_--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
sum[i] = sum[i-1] + a[i];
dp[i][0] = a[i];
pos[i][0] = i;
}
init();
ans = 0;
solve(1, n);
printf("%lld\n", ans);
}
return 0;
}

最新文章

  1. JavaScript求和
  2. C# 常用结构
  3. [Tool] WireShark基本使用
  4. Out of Hay
  5. 使用label在winfrom中添加分割线
  6. vcffilter 工具bug以及解决办法
  7. PLSQL_性能优化系列17_Oracle Merge Into和Update更新效率
  8. 关于mysql下hibernate实体类字段与数据库关键字冲突的问题
  9. 运用集合来做一个DVD管理器(全代码)
  10. JSONP跨域的原理
  11. Java和JavaScript的时间互传
  12. LAMBDA表达式常用 (全)
  13. 图片验证码的JAVA工具类
  14. sqoop实现关系型数据库与hadoop之间的数据传递-import篇
  15. 极客验证官方demo构建使用及代码分析
  16. Codeforces Round #551 (Div. 2) A-E
  17. go语言之进阶篇显式调用panic函数
  18. iOS微信支付demo运行报错解决如下
  19. C#访问SQLServer数据库访问帮助类
  20. 实习培训——Servlet(5)

热门文章

  1. Maven使用第三方Jar文件
  2. 《30天自制操作系统》笔记5 --- (Day3)
  3. Scala词法文法解析器 (二)分析C++类的声明
  4. addpath(),genpath()
  5. vertica单节点故障恢复 Startup Failed, ASR Required
  6. Ubuntu16 安装 wireshark
  7. pytorch tutorial 2
  8. Windows 10 更新补丁后Visual Studio 2017 运行项目出现错误
  9. UNION ALL \UNION
  10. Markdown温故知新(0):导航目录