ARC182D Increment Decrement Again
Solution
很好的 Ad-hoc!
首先发现这个取模非常难处理,那么不妨考虑先不取模,做完操作后再取模。那么限制相当于要求
于是我们可以先令
Code:
qwq
#include<bits/stdc++.h> #define ll long long #define int long long using namespace std; const int N = 2e5 + 10; int n, m, a[N], b[N], c[N]; ll solve(ll x){ ll ans = 0; for(int i = 1; i <= n; i++) ans += abs(c[i] - x); return ans; } int fl(int x, int y){ if(x >= 0) return (x / y); else{ if(x % y) return (x / y - 1); else return (x / y); } } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) cin >> b[i]; if(m == 2){ for(int i = 1; i <= n; i++) if(a[i] != b[i]){cout << -1; return 0;} cout << 0; return 0; } c[1] = b[1]; for(int i = 2; i <= n; i++){ int val = (b[i] - b[i - 1] + m) % m; if(a[i] > a[i - 1]) c[i] = c[i - 1] + val; else c[i] = c[i - 1] + val - m; } for(int i = 1; i <= n; i++) c[i] = a[i] - c[i]; sort(c + 1, c + n + 1); ll p = c[(n + 1) / 2]; cout << min(solve(fl(p, m) * m), solve((fl(p, m) + 1) * m)); return 0; }
本文作者:Little_corn
本文链接:https://www.cnblogs.com/little-corn/p/18727540
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步