题意:给你一个长度为偶数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. LeetCode53 最大子序列问题

    题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和.     示例:     输入: [-2,1,-3,4,-1,2,1,-5,4],   ...

  2. 【ORA】ora-39700解决

  3. 【Oracle】查看当前连接数和最大连接数

    查看当前数据库连接数 select count(*) from v$session where username is not null; select count(*) from v$process ...

  4. 攻防世界 - Web(二)

    supersqli: (!!!) 1.判断有误注入,1'报错, 1' 报错,1'# 正常且为True,1' and 1=1# 正常且为True,1' and 1=2# 正常且为False,所以它里边存 ...

  5. Android 8.0/9.0 wifi 自动连接评分机制

    前言 Android N wifi auto connect流程分析 Android N selectQualifiedNetwork分析 Wifi自动连接时的评分机制 今天了解了一下Wifi自动连接 ...

  6. 找不到:DarchetypeCatalog=local

    设置IDEA Maven->Runner 界面的VM Options参数值为-DarchetypeCatalog=local 刷新项目Maven配置,在项目右边界面,重新引入Maven

  7. Obligations for calling close() on the iterable returned by a WSGI application

    Graham Dumpleton: Obligations for calling close() on the iterable returned by a WSGI application. ht ...

  8. jvm 三种编译

    https://blog.csdn.net/fuxiaoxiaoyue/article/details/93497558 https://blog.csdn.net/tjiyu/article/det ...

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

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

  10. 安装sqlserver 的时候 报错:无法通过Windows功能控制面板自动安装或卸载Windows Server 角色和功能。

    无法安装以下功能:.NET Framework 3.5(包括.NET2.0和3.0) 无法通过Windows功能控制面板自动安装或卸载Windows Server 角色和功能. 若要安装Windows ...