当前位置:网站首页>CodeTon Round 2 D. Magical Array
CodeTon Round 2 D. Magical Array
2022-08-02 02:01:00 【HeartFireY】
题目分析
给定 n n narrays with the same initial value,共有两种操作:
- 操作 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.
Only one array is executed several times 2 2 2操作,Ask which array the operation was performed on 2 2 2,操作了多少次
令 s [ ] s[] s[]表示数组 a [ ] a[] a[]的前缀和,We analyze the two operations on the prefix sum:
操作 1 1 1:Strip unaffected intervals: [ 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],We denote array sums as prefix-segmented sums:
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次操作后:Because the affected elements are continuous,So prefixes and elements are subject to − 1 -1 −1Immediately after impact + 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)
Found that prefixes and array sums are not affected操作 2 2 2:同上,Write the summation first:
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次操作后,We start by analyzing the elements that are affected: 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],But found discontinuities due to affected elements,因此 s [ j + 1 ] , s [ j + 2 ] s[j + 1], s[j + 2] s[j+1],s[j+2]会受影响,But the impact is there a [ j + 2 ] a[j + 2] a[j+2]offset,也就是 s [ j + 2 ] s[j + 2] s[j+2]上的 − 1 -1 −1Effects are eliminated.那么:
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)
Found prefix and array sum to be − 1 -1 −1.操作 2 2 2effect on the total,We can analyze it from the point of view of formula:
∑ 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
The impact was found to be: − 1 -1 −1.Then according to the above conclusion,The affected arrays and the number of operations can be found.同理,Action-Affect is 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; }
边栏推荐
猜你喜欢
【轮式里程计】
字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
Day116. Shangyitong: Details of appointment registration ※
typescript35-class的构造函数
Use baidu EasyDL implement factory workers smoking behavior recognition
成都openGauss用户组招募啦!
typescript34-class的基本使用
Chengdu openGauss user group recruit!
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
随机推荐
¶Backtop 回到顶部 不生效
『网易实习』周记(二)
Hash collisions and consistent hashing
typescript35-class的构造函数
AntPathMatcher使用
LeetCode刷题日记:53、最大子数组和
"NetEase Internship" Weekly Diary (3)
Shell入门终章
哈希冲突和一致性哈希
Kubernetes之本地存储
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
编码经验之谈
LeetCode刷题日记: 33、搜索旋转排序数组
LeetCode brushing diary: 53, the largest sub-array and
CodeTon Round 2 D. Magical Array 规律
Use baidu EasyDL implement factory workers smoking behavior recognition
华为5年女测试工程师离职:多么痛的领悟...
密码学的基础:X.690和对应的BER CER DER编码
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
LeetCode刷题日记:153、寻找旋转排序数组中的最小值