当前位置:网站首页>CodeTon Round 2 D. Magical Array 规律
CodeTon Round 2 D. Magical Array 规律
2022-08-02 01:50:00 【HeartFireY】
题目分析
给定 n n n个初始值相同的数组,共有两种操作:
- 操作 1 1 1:选择一对 ( i , j ) (i, j) (i,j), a [ i − 1 ] a[i - 1] a[i−1]加 1 1 1, a [ i ] a[i] a[i]减去 1 1 1, a [ j ] a[j] a[j]减去 1 1 1, a [ j + 1 ] a[j + 1] a[j+1]加 1 1 1。
- 操作 2 2 2:选择一对 ( i , j ) (i, j) (i,j), a [ i − 1 ] a[i - 1] a[i−1]加 1 1 1, a [ i ] a[i] a[i]减去 1 1 1, a [ j ] a[j] a[j]减去 1 1 1, a [ j + 2 ] a[j + 2] a[j+2]加 1 1 1。
只有一个数组被执行了若干次 2 2 2操作,问哪个数组被执行了操作 2 2 2,操作了多少次
令 s [ ] s[] s[]表示数组 a [ ] a[] a[]的前缀和,我们对两个操作在前缀和上进行分析:
操作 1 1 1:剥离未收到影响的区间: [ 1 , i − 2 ] , [ i + 1 , j − 1 ] , [ j + 2 , n ] [1, i - 2],[i+ 1, j - 1], [j + 2, n] [1,i−2],[i+1,j−1],[j+2,n],我们将数组和表示为前缀分段和加和式:
s [ i − 2 ] + ( s [ j − 1 ] − s [ i ] ) + ( s [ n ] − s [ j + 1 ] ) + a [ i − 1 ] + a [ i ] + a [ j ] + a [ j + 1 ] s[i - 2] + (s[j - 1] - s[i]) + (s[n] - s[j + 1]) + a[i - 1] + a[i] + a[j] + a[j + 1] s[i−2]+(s[j−1]−s[i])+(s[n]−s[j+1])+a[i−1]+a[i]+a[j]+a[j+1]
经过 1 1 1次操作后:因为受影响元素连续,因此前缀和元素受到 − 1 -1 −1影响后立即受到 + 1 +1 +1影响,不变
s [ i − 2 ] + ( s [ j − 1 ] − s [ i ] ) + ( s [ n ] − s [ j + 1 ] ) + ( a [ i − 1 ] + 1 ) + ( a [ i ] − 1 ) + ( a [ j ] − 1 ) + ( a [ j + 1 ] + 1 ) s[i - 2] + (s[j - 1] - s[i]) + (s[n] - s[j + 1]) + (a[i - 1] + 1) + (a[i] - 1) + (a[j] - 1) + (a[j + 1] + 1) s[i−2]+(s[j−1]−s[i])+(s[n]−s[j+1])+(a[i−1]+1)+(a[i]−1)+(a[j]−1)+(a[j+1]+1)
发现前缀和数组总和不受影响操作 2 2 2:同上,先写出加和式:
s [ i − 2 ] + ( s [ j − 1 ] − s [ i ] ) + ( s [ n ] − s [ j + 2 ] ) + ( s [ j + 1 ] − s [ j ] ) + a [ i − 1 ] + a [ i ] + a [ j ] + a [ j + 2 ] s[i - 2] + (s[j - 1] - s[i]) + (s[n] - s[j + 2]) + (s[j + 1] - s[j]) + a[i - 1] + a[i] + a[j] + a[j + 2] s[i−2]+(s[j−1]−s[i])+(s[n]−s[j+2])+(s[j+1]−s[j])+a[i−1]+a[i]+a[j]+a[j+2]
经过 1 1 1次操作后,我们先分析收到影响的元素: a [ i − 1 ] , a [ i ] , a [ j ] , a [ j + 2 ] a[i - 1], a[i], a[j], a[j + 2] a[i−1],a[i],a[j],a[j+2],但发现由于受影响的元素不连续,因此 s [ j + 1 ] , s [ j + 2 ] s[j + 1], s[j + 2] s[j+1],s[j+2]会受影响,但收到的影响在 a [ j + 2 ] a[j + 2] a[j+2]处抵消,也就是 s [ j + 2 ] s[j + 2] s[j+2]上的 − 1 -1 −1影响被消除。那么:
s [ i − 2 ] + ( s [ j − 1 ] − s [ i ] ) + ( s [ n ] − s [ j + 2 ] ) + ( ( s [ j + 1 ] − 1 ) − s [ j ] ) + ( a [ i − 1 ] + 1 ) + ( a [ i ] − 1 ) + ( a [ j ] − 1 ) + ( a [ j + 1 ] + 1 ) s[i - 2] + (s[j - 1] - s[i]) + (s[n] - s[j + 2]) + ((s[j + 1] - 1) - s[j]) + (a[i - 1] + 1) + (a[i] - 1) + (a[j] - 1) + (a[j + 1] + 1) s[i−2]+(s[j−1]−s[i])+(s[n]−s[j+2])+((s[j+1]−1)−s[j])+(a[i−1]+1)+(a[i]−1)+(a[j]−1)+(a[j+1]+1)
发现前缀和数组总和被 − 1 -1 −1。操作 2 2 2对总和的影响,我们可以从式子的角度分析:
∑ i = 1 n ∑ j = 1 i a [ j ] \sum^{n}_{i = 1}\sum^{i}_{j = 1} a[j] i=1∑nj=1∑ia[j]
对 a [ x ] a[x] a[x]元素 + 1 +1 +1, a [ x + 1 ] − 1 a[x + 1] - 1 a[x+1]−1
∑ i = 1 x − 1 ∑ j = 1 i a [ j ] + ∑ i = x x ∑ j = 1 i a [ j ] + ∑ i = x n 1 + ∑ i = x + 1 n ∑ j = 1 i a [ j ] − ∑ i = x + 1 n 1 = ( ∑ i = 1 n ∑ j = 1 i a [ j ] ) + 1 \sum_{i = 1}^{x - 1}\sum_{j = 1}^{i}a[j] + \sum_{i = x}^{x}\sum_{j = 1}^{i}a[j] + \sum_{i = x}^{n}1 + \sum_{i = x + 1}^{n}\sum_{j = 1}^{i}a[j] - \sum_{i = x + 1}^{n}1 = (\sum^{n}_{i = 1}\sum^{i}_{j = 1} a[j]) + 1 i=1∑x−1j=1∑ia[j]+i=x∑xj=1∑ia[j]+i=x∑n1+i=x+1∑nj=1∑ia[j]−i=x+1∑n1=(i=1∑nj=1∑ia[j])+1
再分析 a [ y ] − 1 a[y]-1 a[y]−1, a [ x + 2 ] a[x + 2] a[x+2]元素 + 1 +1 +1:
∑ i = 1 x − 1 ∑ j = 1 i a [ j ] + ∑ i = x x ∑ j = 1 i a [ j ] − ∑ i = x n 1 + ∑ i = x + 1 n ∑ j = 1 i a [ j ] + ∑ i = x + 2 n 1 = ( ∑ i = 1 n ∑ j = 1 i a [ j ] ) − 2 \sum_{i = 1}^{x - 1}\sum_{j = 1}^{i}a[j] + \sum_{i = x}^{x}\sum_{j = 1}^{i}a[j] - \sum_{i = x}^{n}1 + \sum_{i = x + 1}^{n}\sum_{j = 1}^{i}a[j] + \sum_{i = x + 2}^{n}1 = (\sum^{n}_{i = 1}\sum^{i}_{j = 1} a[j]) - 2 i=1∑x−1j=1∑ia[j]+i=x∑xj=1∑ia[j]−i=x∑n1+i=x+1∑nj=1∑ia[j]+i=x+2∑n1=(i=1∑nj=1∑ia[j])−2
发现影响为: − 1 -1 −1。那么根据以上结论,可以找出被影响的数组及操作次数。同理,操作一影响为 0 0 0。
Code
#include <bits/stdc++.h> #pragma gcc optimize("O2") #pragma g++ optimize("O2") #define int long long #define endl '\n' using namespace std; const int N = 1e6 + 10; int flag[N]; inline void solve(){ int n, m; cin >> n >> m; memset(flag, 0, (n + 5) * sizeof(int)); int **a = (int **)malloc((n + 2) * sizeof(int**)); int i, j; for (i = 0; i <= n; i++) { a[i] = (int*)malloc((m + 2) * sizeof(int)); } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++) cin >> a[i][j]; } for(int i = 1; i < m; i++){ int minn = *min_element(a[j] + 1, a[j] + 1 + n); for(int j = 1; j <= n; j++){ flag[j] += (a[j][i] -= minn); a[j][i + 1] += a[j][i]; } } int pos = 1; for(int i = 2; i <= n; i++) if(flag[pos] > flag[i]) pos = i; if(pos == 1) cout << pos << ' ' << flag[2] - flag[1] << endl; else cout << pos << ' ' << flag[1] - flag[pos] << endl; } signed main(){ ios_base::sync_with_stdio(false), cin.tie(0); int t = 1; cin >> t; while(t--) solve(); return 0; }
边栏推荐
- Basic use of typescript34-class
- 信息化和数字化的本质区别是什么?
- Understand the big model in seconds | 3 steps to get AI to write a summary
- 3 Month Tester Readme: 4 Important Skills That Impacted My Career
- Kubernetes — Flannel
- 大话西游无法登陆解决
- Day116. Shangyitong: Details of appointment registration ※
- After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
- 使用百度EasyDL实现厂区工人抽烟行为识别
- typescript38-class的构造函数实例方法继承(implement)
猜你喜欢
检查IP或端口是否被封
华为5年女测试工程师离职:多么痛的领悟...
MySQL optimization strategy
『网易实习』周记(一)
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
雇用WordPress开发人员:4个实用的方法
外包干了三年,废了...
Use baidu EasyDL implement factory workers smoking behavior recognition
Redis 订阅与 Redis Stream
字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
随机推荐
Can't connect to MySQL server on 'localhost3306' (10061) Simple and clear solution
哈希表
电商库存系统的防超卖和高并发扣减方案
检查IP或端口是否被封
Navicat data shows incomplete resolution
大话西游无法登陆解决
3. Bean scope and life cycle
volatile原理解析
Fly propeller power space future PIE - Engine Engine build earth science
Win Go开发包安装配置、GoLand配置
Use baidu EasyDL implement factory workers smoking behavior recognition
Constructor instance method inheritance of typescript37-class (extends)
Reflex WMS中阶系列7:已经完成拣货尚未Load的HD如果要取消拣货,该如何处理?
Effects of Scraping and Aggregation
【Brush the title】Family robbery
字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
飞桨开源社区季度报告来啦,你想知道的都在这里
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
Named parameter implementation of JDBC PreparedStatement
typescript38-class的构造函数实例方法继承(implement)