Removing Stones(2019年牛客多校第三场G+启发式分治)
题目链接
题意
初始时有\(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;
}
Removing Stones(2019年牛客多校第三场G+启发式分治)的更多相关文章
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- [2019牛客多校第三场][G. Removing Stones]
题目链接:https://ac.nowcoder.com/acm/contest/883/G 题目大意:有\(n\)堆石头,每堆有\(a_i\)个,每次可以选其中两堆非零的石堆,各取走一个石子,当所有 ...
- 启发式分治:2019牛客多校第三场 G题 Removing Stones
问题可以转换为求有多少个区间数字的总和除2向下取整大于等于最大值.或者解释为有多少个区间数字的总和大于等于最大值的两倍(但是若区间数字总和为奇数,需要算作减1) 启发式分治: 首先按最大值位置分治,遍 ...
- 2019年牛客多校第三场 F题Planting Trees(单调队列)
题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...
- 牛客多校第三场 F Planting Trees
牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...
- 牛客多校第三场 A—pacm team (4维背包加路径压缩)
链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 Eddy was a contestant participating , Eddy fail ...
- 牛客多校第四场 G Maximum Mode
链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...
- Distance(2019年牛客多校第八场D题+CDQ+树状数组)
题目链接 传送门 思路 这个题在\(BZOJ\)上有个二维平面的版本(\(BZOJ2716\)天使玩偶),不过是权限题因此就不附带链接了,我也只是在算法进阶指南上看到过,那个题的写法是\(CDQ\), ...
- 2019年牛客多校第四场 B题xor(线段树+线性基交)
题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...
随机推荐
- Linux构建DNS主从服务器
所有服务器:iptables -Fsystemctl stop firewalldsetenforce 0 配置yum 主服务器:[root@localhost ~]# yum -y install ...
- 线段树模板(无lazy优化)
区间修改与区间查询问题 模板: int ans; struct node{ int l,r,v; node(){v=;} }tree[LEN*]; int arr[LEN]; //建树 void bu ...
- [BJOI2019]光线(DP)
降智了…… 当你走头无路的时候就应该知道瞎搞一个DP: $p[i]$ 表示光射入第 $1$ 块玻璃时,从第 $i$ 块玻璃出去的光量. $q[i]$ 表示光射入第 $i$ 块玻璃时,从第 $i$ 块玻 ...
- 动手学深度学习5-softmax回归
softmax回归 softmax 回归模型 单样本分类的矢量计算表达式 小批量样本分类的矢量计算表达式 交叉熵损失函数 模型预测以及评价 小结 softmax回归 前几节介绍的是线性回归模型适用于输 ...
- 部门工资前三高的所有员工 - LeetCode
Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId . +----+-------+--------+---- ...
- Redis学习之对象系统源码分析
背景知识: Redis并没有直接使用sds,双端链表,字典,压缩列表,跳表等这些数据结构来直接实现键值对数据库,而是基于这些对象创建了一个对象系统,这个对象系统包含5个对象:字符串对象,列表对象,哈希 ...
- Git的使用--如何安装和使用 github,让小白不在那么白 (一)(超详解)
简介 刚开始写了关于如何将本地代码上传到github上,但是有些小伙伴们不清楚如何安装Git,这一篇就给小伙伴们普及一下Git的安装和使用.适合刚开始用git的小白,大神或者大佬请绕道. 实际项目开发 ...
- Linux内核调优部分参数说明
#接收套接字缓冲区大小的默认值(以字节为单位). net.core.rmem_default = 262144 #接收套接字缓冲区大小的最大值(以字节为单位). net.core.rmem_max = ...
- 【leetcode-148】排序链表
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3输出: 1->2->3->4示例 2: 输入: ...
- ASP.NET Core中使用MialKit实现邮件发送
# 导包 首先我们需要导入 MailKit NuGet包,NuGet安装包命令在下方拓展介绍中. # 引用命名空间 using MailKit.Net.Smtp; using MimeKit; # 邮 ...