当前位置:网站首页>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; }
边栏推荐
- Analysis of volatile principle
- 软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
- PHP直播源码实现简单弹幕效果的相关代码
- YGG 公会发展计划第 1 季总结
- typescript30 - any type
- 【LeetCode Daily Question】——704. Binary Search
- 手写一个博客平台~第一天
- 使用百度EasyDL实现厂区工人抽烟行为识别
- Data transfer at the data link layer
- Win Go开发包安装配置、GoLand配置
猜你喜欢
AOF rewrite
pcie inbound和outbound关系
HSDC和独立生成树相关
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
LeetCode brush diary: LCP 03. Machine's adventure
手写博客平台~第二天
Golang分布式应用之定时任务
Speed up your programs with bitwise operations
6-25 Vulnerability Exploitation - irc Backdoor Exploitation
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
随机推荐
typescript35-class的构造函数
手写一个博客平台~第三天
The characteristics and principle of typescript29 - enumeration type
typescript32-ts中的typeof
typescript29-枚举类型的特点和原理
Win Go开发包安装配置、GoLand配置
AntPathMatcher uses
检查IP或端口是否被封
华为5年女测试工程师离职:多么痛的领悟...
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
Pcie the inbound and outbound
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
typescript38-class的构造函数实例方法继承(implement)
Effects of Scraping and Aggregation
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
Analysis of volatile principle
6-25漏洞利用-irc后门利用
成都openGauss用户组招募啦!
【轮式里程计】
¶Backtop 回到顶部 不生效