题意:给你一个长度为偶数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. 【ORA】ORA-01756: quoted string not properly terminated

    出现ORA-01756: quoted string not properly terminated 后,查看SQL是否有中文符号 修改为英文的符号,运行正常

  2. Genymotion虚拟机用键盘输入中文

    genymotion我用的版本是3.0.4,安卓内核版本从4到9都进行了尝试,尤其是教新的版本原生是不带中文输入法的. 前提:安装Genymotion以后,想要随意安装app,需要先安装Genymot ...

  3. [分享] 最流行的 10 个 JavaScript 库

    1. Lodash https://github.com/lodash/lodash 一个工具库,用得还蛮多. 2. Chalk https://github.com/chalk/chalk 给终端加 ...

  4. Linux文件系统之INode

    本文转载自阮一峰博客:理解inode 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存51 ...

  5. 一例 Go 编译器代码优化 bug 定位和修复解析

    https://mp.weixin.qq.com/s/Tyl6dSb7mHBuqqN6WvEuaw

  6. __init__ raises an exception, then __del__ will still be called

    issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org ...

  7. https://twistedmatrix.com/documents/current/core/howto/defer.html

    https://twistedmatrix.com/documents/current/core/howto/defer.html

  8. 【译】.NET 5. 0 中 Windows Form 的新特性

    自从 Windows Form 在 2018 年底开源并移植到 .NET Core 以来,团队和我们的外部贡献者都在忙于修复旧的漏洞和添加新功能.在这篇文章中,我们将讨论 .NET 5.0 中 Win ...

  9. python几个练习(素数、斐波那契数列)

    随机输入求素数: x = int(input("please enter the number:")) if x != 1: for i in range(2, x): if x ...

  10. postfix “ Sender address rejected: not logged in”

      一.问题 先说一下问题,我们公司有两台邮件服务器,分别是不同的域名但是用的是同一个用户认证,最近老有人反应说,有匿名邮件就是通过类似这种网站,进行使用你们公司的SMTP邮件服务器进行发送垃圾邮件 ...