Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)

题意:给你一个长度为偶数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 (划分区间,差分)的更多相关文章
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- [codeforces round#475 div2 ][C Alternating Sum ]
http://codeforces.com/contest/964/problem/C 题目大意:给出一个等比序列求和并且mod 1e9+9. 题目分析:等比数列的前n项和公式通过等公比错位相减法可以 ...
- 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号结 ...
- Codeforces Round #599 (Div. 2) E. Sum Balance
这题写起来真的有点麻烦,按照官方题解的写法 先建图,然后求强连通分量,然后判断掉不符合条件的换 最后做dp转移即可 虽然看起来复杂度很高,但是n只有15,所以问题不大 #include <ios ...
- 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 ...
- Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心
D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...
- 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 ...
- 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 ...
随机推荐
- Spark Streaming处理Flume数据练习
把Flume Source(netcat类型),从终端上不断给Flume Source发送消息,Flume把消息汇集到Sink(avro类型),由Sink把消息推送给Spark Streaming并处 ...
- Hbase 手动执行MajorCompation
说明: Major Compaction 的作用: 1.将一个Region下的所有StoreFile合并成一个StoreFile文件 2.对于删除.过期.多余版本的数据进行清除 由于MajorComp ...
- Celery--短信与邮件
1 Celery 实现短信--邮件 1.1 容联云-短信 from ronglian_sms_sdk import SmsSDK accountSid = '8a216da8757784cd01759 ...
- leetcode230. 二叉搜索树中第K小的元素
题目链接: https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 题目: 给定一个二叉搜索树,编写一个函数 kthSmalle ...
- Sentry(v20.12.1) K8S 云原生架构探索,JavaScript Enriching Events(丰富事件信息)
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
- 数据分析——Numpy/pandas
NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准数学函数(无需编 ...
- OpenCV 和 Dlib 人脸识别基础
00 环境配置 Anaconda 安装 1 下载 https://repo.anaconda.com/archive/ 考虑到兼容性问题,推荐下载Anaconda3-5.2.0版本. 2 安装 3 测 ...
- file转化为binary对象发送给后台
具体代码如下: function filechange(e) { var file = $('#filed').get(0).files[0]; var fileSize = file.size, f ...
- Py集合,字符串的格式化,函数,便利
可变与不可变 不可变指的是:重新赋值时,内存中的id值会变得 其中有:字符串,数字,元组 name="sb" v=id(name) print(v) name ="ale ...
- ovs-actions
1. 端口说明 OVS支持如下的标准OpenFlow端口名称(括号中是端口号): in_port (65528 or 0xfff8; 0xfffffff8) table (65529 or 0xfff ...