题意:给你一个长度为偶数n的数组,每次可以将一个元素修改为不大于k的值,要求每个a[i]+a[n-i+1]都相等,求最少操作多少次

题解:假设每一对的和都为sum,小的记为mn,大的记为mx;

        枚举[2,2*k]的所有数x:

           我们对每一对相应的数考虑,有三种情况:改一个数,改两个数,不改

       1.改一个数:当x∈[mn+1,mx+k];          ==》b[mn+1]+,1,b[mx+k+1]-1;

    2.该两个数:当x∈[2,mn] || [mx+k+1,2*k];   ==》b[2]+2,b[mn+1]-2;    b[mx+k+1]+2,b[2*k](最后一个,没必要变)

    3.不改:x==sum

       所以我们将每对数记录一个差分数组b[i], 维护区间然后还原即可.

代码:

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <stack>
7 #include <queue>
8 #include <vector>
9 #include <map>
10 #include <set>
11 #include <unordered_set>
12 #include <unordered_map>
13 #define ll long long
14 #define fi first
15 #define se second
16 #define pb push_back
17 #define me memset
18 const int N = 1e6 + 10;
19 const int mod = 1e9 + 7;
20 using namespace std;
21 typedef pair<int,int> PII;
22 typedef pair<long,long> PLL;
23
24 int t;
25 int n,k,a[N];
26 int sum;
27 int mx,mn;
28 int main() {
29 ios::sync_with_stdio(false);
30 cin>>t;
31 while(t--){
32 cin>>n>>k;
33 int b[2*k+10];
34 me(b,0,sizeof(b));
35 for(int i=1;i<=n;++i) cin>>a[i];
36
37 for(int i=1;i<=n/2;++i){
38 sum=a[i]+a[n-i+1];
39 mn=min(a[i],a[n-i+1]);
40 mx=max(a[i],a[n-i+1]);
41
42 b[2]+=2;
43 b[mn+1]--;
44 b[mx+k+1]++;
45 b[sum]--;
46 b[sum+1]++;
47 }
48 int res=b[2];
49 for(int i=3;i<=2*k;++i){
50 b[i]+=b[i-1];
51 res=min(res,b[i]);
52 }
53 printf("%d\n",res);
54 }
55
56 return 0;
57 }

Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)的更多相关文章

  1. Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串

    题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...

  2. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  3. [codeforces round#475 div2 ][C Alternating Sum ]

    http://codeforces.com/contest/964/problem/C 题目大意:给出一个等比序列求和并且mod 1e9+9. 题目分析:等比数列的前n项和公式通过等公比错位相减法可以 ...

  4. Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)

    D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...

  5. Codeforces Round #599 (Div. 2) E. Sum Balance

    这题写起来真的有点麻烦,按照官方题解的写法 先建图,然后求强连通分量,然后判断掉不符合条件的换 最后做dp转移即可 虽然看起来复杂度很高,但是n只有15,所以问题不大 #include <ios ...

  6. Codeforces Round #599 (Div. 1) C. Sum Balance 图论 dp

    C. Sum Balance Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to ...

  7. Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心

    D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...

  8. Codeforces Round #530 (Div. 1) 1098A Sum in the tree

    A. Sum in the tree Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root ha ...

  9. Codeforces Round #232 (Div. 2) On Sum of Fractions

    Let's assume that v(n) is the largest prime number, that does not exceed n; u(n) is the smallest pri ...

随机推荐

  1. 在CentOS上安装Singularity高性能容器

    什么是singularity容器 Singularity是劳伦斯伯克利国家实验室专门为大规模.跨节点HPC和DL工作负载而开发的容器化技术.具备轻量级.快速部署.方便迁移等诸多优势,且支持从Docke ...

  2. ptgmk

    num >= 1125899906842624 ? "P" \ : num >= 1099511627776 ? "T" \ : num >= ...

  3. 【Oracle】dump函数用法

    Oracle dump函数的用法 一.函数标准格式: DUMP(expr[,return_fmt[,start_position][,length]]) 基本参数时4个,最少可以填的参数是0个.当完全 ...

  4. kubernets之Replication Controller

    一  Replication Controller的介绍      pod可能会由于各种原因消失和多出来,例如node节点去除集群或者人为的手工创建,所以为了方便和管理pod的数量,k8s里面 的另外 ...

  5. 【Android】报错 Please ensure Hyper-V is disabled in Windows Features, or refer to the Intel HAXM 的解决方案

    参考文章 实测华为锐龙本(adm yes)安装Android avd虚拟机教程 环境 Android Studio 3.6; Windows 1909; AMD Ryzen 4800U with Ra ...

  6. 前端知识(一)05 axios-谷粒学院

    目录 一.axios的作用 二.axios实例 1.复制js资源 2.创建 axios.html 3.引入js 4.启动课程中心微服务 5.编写js 6.html渲染数据 7.跨域 8.使用生命周期函 ...

  7. GStreamer环境搭建篇

    GStreamer是一套强大的多媒体中间件系统,跟FFmpeg功能类似. 各个Linux发行版(Ubuntu,fedora),大都集成了GStreamer相关工具,而作为软件层次结构最上层的播放器,几 ...

  8. Vim配置及其他注意事项

    常用的一些配置 set t_ti= set t_te= "这两个比较特殊,后边说 set termencoding=utf-8 set encoding=utf-8 set fileenco ...

  9. Asp.netCore 3.1控制器属性注入and异步事务Aop by AutoFac

    Aspect Oriented Programming(AOP)是较为热门的一个话题.AOP,国内我们都习惯称之为:面向切面编程 下面直接code 干货展示:(一般人我还不告诉,嘻嘻) 1:导入相关的 ...

  10. 网页小实验——用canvas生成精灵动画图片

    实验目标:借助canvas把一张国际象棋棋子图片转换为一组适用于WebGL渲染的精灵动画图片,不借助其他图片处理工具,不引用其他库只使用原生js实现. 初始图片如下: 一.图片分割 将初始图片分割为六 ...