• 题意:有一堆石子,你每次可以选择相邻(就算两堆石子中间有很多空堆也不算)的两堆石子,使得两堆石子的个数同时\(-1\),你在刚开始的时候有一次交换相邻石子的机会,问你最后能否拿走所有石子.
  • 题解:对于第一堆石子和最后一堆石子,它们只能靠第二堆石子和倒数第二堆石子减去才合法,所以我们由第一堆石子不断向右推和最后一堆石子不断向左推,这个过程可以用前缀和\(pre\)与后缀和\(suf\)表示.如果我们当前选择堆\(<i-1,i>\)的话,那么前缀和\(pre[i-2]\)和后缀和\(suf[i+1]\)是不会受到任何影响的,如果问题出在我们当前选择的这个堆上的话,我们就必须保证\(pre[i-2]\)和\(suf[i+1]\)都是合法的,同时,我们将\(pre[i-2]\)与\(a[i-1]\)合并后和\(suf[i+1]\)与\(a[i]\)合并后的值应该是合法且相等的.于是乎,我们可以枚举所有堆,然后判断不反转和反转是否合法.这里注意,如果\(pre[i]\)不合法,我们将\(pre[i]=-1\),那么\(i\)后面的\(pre\)也都是不合法的,都标记为\(-1\),\(suf\)同理.
  • 代码:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} int _;
int a[N];
int pre[N],suf[N]; bool check(int i){
if(pre[i-2]!=-1 && suf[i+1]!=-1 && a[i-1]>=pre[i-2] && a[i]>=suf[i+1] && a[i-1]-pre[i-2]==a[i]-suf[i+1])
return true;
return false;
} int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>_;
while(_--){
int n;
cin>>n;
rep(i,1,n) cin>>a[i]; pre[0]=0,suf[n+1]=0; rep(i,1,n){
if(pre[i-1]==-1 || a[i]<pre[i-1]){
pre[i]=-1;
}else{
pre[i]=a[i]-pre[i-1];
}
} per(i,n,1){
if(suf[i+1]==-1 || a[i]<suf[i+1]){
suf[i]=-1;
}else{
suf[i]=a[i]-suf[i+1];
}
} bool flag=false; rep(i,2,n){
if(check(i)) {flag=true;break;}
swap(a[i],a[i-1]);
if(check(i)) {flag=true;break;}
swap(a[i],a[i-1]);
if(pre[i-1]==-1) break; //小小的优化
} if(flag) cout<<"YES\n";
else cout<<"NO\n";
} return 0;
}

Codeforces Round #696 (Div. 2) D. Cleaning (思维,前缀和)的更多相关文章

  1. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  2. Codeforces Round #697 (Div. 3) D. Cleaning the Phone (思维,前缀和)

    题意:你的手机有\(n\)个app,每个app的大小为\(a_i\),现在你的手机空间快满了,你需要删掉总共至少\(m\)体积的app,每个app在你心中的珍惜值是\(b_i\),\(b_i\)的取值 ...

  3. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  4. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  5. Codeforces Round #541 (Div. 2) E 字符串 + 思维 + 猜性质

    https://codeforces.com/contest/1131/problem/D 题意 给你n个字符串,字符串长度总和加起来不会超过1e5,定义字符串相乘为\(s*s1=s1+s[0]+s1 ...

  6. Codeforces Round #532 (Div. 2)- B(思维)

    Arkady coordinates rounds on some not really famous competitive programming platform. Each round fea ...

  7. Codeforces Round #469 (Div. 2)C. Zebras(思维+模拟)

    C. Zebras time limit per test memory limit per test 512 megabytes input standard input output standa ...

  8. Codeforces Round #532 (Div. 2)- A(思维)

    This morning, Roman woke up and opened the browser with nn opened tabs numbered from 11 to nn. There ...

  9. Codeforces Round #524 (Div. 2) D(思维,构造)

    #include<bits/stdc++.h>using namespace std;long long dp[107];int main(){    int cnt=1;    dp[1 ...

随机推荐

  1. SqlLoad的简单使用

    sqlload的简单使用: 能实现: 快速导入大量数据 1.先安装oracle 客户端机器.有点大,600M+, 2.安装时选择管理员安装(1.1g) 3.第三步的时候我的出错了.说是环境变量校验不通 ...

  2. Linux面试必备

    1.Linux的体系结构

  3. --safe-user-create

    此参数如果启用,用户将不能用grant语句创建新用户,除非用户有mysql数据库中user表的insert权限, ./mysqld_safe  --safe-user-create & 用-- ...

  4. 【Linux】centos7中 root家目录中perl5文件夹无法删除问题解决

    由于新项目上线,安装了一些perl的一些包 但是发现,在/root下有一个perl5/的文件夹,删除后,重新登录又会出现,很是烦人,而且他还没有内容,就是一个空文件 那么着手搞掉他 环境:centos ...

  5. leetcode 730. 统计不同回文子序列(区间dp,字符串)

    题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...

  6. cut和tr命令的联合使用

    cut的-d选项只能是单个字符,而对于多个连续相同字符分隔的字段,单凭cut命令是达不到想要的效果的,特别是多个连续空格分隔时. 但借助tr -s的压缩重复字符功能,可以将多个连续空格压缩为一个空格, ...

  7. windows下部署Grafana +prometheus平台监控

      1.Prometheus简介 Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖.用户只需要下载对应平台包,解压并且添加基本的配置即可正常启Prometheus S ...

  8. Java基础复习2

    三目运算符 语法:条件判断?表达式1:表达式2; 如果条件判断成立则获取值1否则获取值2 public class demo1{     public static void main(String[ ...

  9. Mybatis Plus 3.4版本之后分页插件的变化

    一.MybatisPlusInterceptor 从Mybatis Plus 3.4.0版本开始,不再使用旧版本的PaginationInterceptor ,而是使用MybatisPlusInter ...

  10. matlab图像处理程序大集合

    1.图像反转 MATLAB程序实现如下:I=imread('xian.bmp');J=double(I);J=-J+(256-1);                 %图像反转线性变换H=uint8( ...