题意:给你一个长度为偶数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. LeetCode141-环形链表检测

    题目 给定一个链表,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置 ...

  2. .NET Core学习笔记(9)——Entity Framework Core之Code First

    上篇我们介绍了怎么通过已有的SQLServer表来创建实体类,本篇我们改用Code First的方式,由C#代码书写的实体类来生成SQLServer表.并且通过简单的Console APP往SQLSe ...

  3. Lambda表达式你会用吗?

    函数式编程 在正式学习Lambda之前,我们先来了解一下什么是函数式编程 我们先看看什么是函数.函数是一种最基本的任务,一个大型程序就是一个顶层函数调用若干底层函数,这些被调用的函数又可以调用其他函数 ...

  4. 安装newman error:package exports for 'c:\nmp\node_modules\newman\node_module 解决办法

    一.场景描述: 通过npm安装newman时,一直失败. 尝试了很多安装命令: npm install -g newman npm install -g newman --registry=http: ...

  5. VBA调用数独求解器

    我开发了一个用于求解数独的dll文件,只需要双击一下注册表文件,就可以在VBA中调用这个功能了.具体步骤如下: 下载:https://share.weiyun.com/5dpcNqx 找到ExcelS ...

  6. centos7 开放指定端口

    centos7 开放指定端口 #开放8080端口 firewall-cmd --zone=public --add-port=8080/tcp --permanent #重载防火墙 firewall- ...

  7. Android根据pdf模板生成pdf文件

    我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做. 需要包含的jar包:iText-5.0.6.jar    iTextAsian.jar ,怎样jar包导入工程 ...

  8. 全网最详细的PyCharm+Anaconda的安装。

    目录 PyCharm的安装 一.下载安装包 1.安装网站 2.在导航栏输入网址回车 3.点击 DOWNLOAD. 4.它有专业版和社区版,我们下载社区版就可以使用了.(专业版要收费) 二.安装过程 5 ...

  9. 超精讲-逐例分析 CSAPP:实验2-Bomb!(下)

    好了话不多说我们书接上文继续来做第二个实验下面是前半部分实验的连接 5. 第五关 首先感觉应该是个递归问题 /* Round and 'round in memory we go, where we ...

  10. get uuid

    https://wx2.qq.com/?&lang=zh_CN /** * 启动二维码登录 */ function doQrcodeLogin() { loginFactory.getUUID ...