![](https://i-blog.csdnimg.cn/blog_migrate/6161e64b5b6b92880907a56f9cba3eff.png)
![](https://i-blog.csdnimg.cn/blog_migrate/131023d9bd8dbba20b38b0dfa16825d8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/065ebd78a75b13a8b447b6a519c478ad.jpeg)
x[i]代表i向i+1移动的牌数,负数时代表i+1向i移动
由于每次移动可以移动若干张牌,问移动几次,所以我们只需要看x[1]~x[n-1]中非零的个数。
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 100;
int a[N], sum[N], c[N];
int n;
int get()
{
for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
int avg = sum[n] / n;
int res = 0;
for(int i = 1; i <= n - 1; i++) {
c[i] = sum[i] - i * avg;
if(c[i] != 0) res ++;
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
cout << get() << endl;
return 0;
}
环形均分纸牌问题:
![](https://i-blog.csdnimg.cn/blog_migrate/a74784b2c215a102afa9dfd7818dda0d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f4267fc61af4371d25b2d72294ab92e5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ec2acc0f3ab89e3d1803a83cbfef1211.png)
这题和上题不大一样,每次移动只能移动一个,问移动次数。并且行和列都要均分,但是行内交换不会影响到列,列内交换也不会影响到行,所以我们分别进行两次环形均分纸牌操作就行了。
![](https://i-blog.csdnimg.cn/blog_migrate/1964bcb7ad14eff8ef8131bbe99412ad.jpeg)
最后就转换成了一个经典问题:给你一组点的位置,求某个位置到所有点的距离最小是多少,最小值就是(所有点到其中位数的距离之和)
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N = 1e5 + 100;
int row[N], col[N], sum[N], c[N];
int n, m, t;
int get(int x, int a[])
{
for(int i = 1; i <= x; i++) sum[i] = sum[i - 1] + a[i];
if(sum[x] % x) return -1;
int avg = sum[x] / x;
c[1] = 0;
for(int i = 2; i <= x; i++) c[i] = sum[i - 1] - (i - 1) * avg;
sort(c + 1, c + 1 + x);
int res = 0;
for(int i = 1; i <= x; i++) res += abs(c[i] - c[(x + 1) / 2]);
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> t;
while(t--)
{
int x, y;
cin >> x >> y;
row[x]++; col[y]++;
}
int r = get(n, row);
int c = get(m, col);
if(~r && ~c) cout << "both" << " " << r + c << endl;
else if(~r) cout << "row" << " " << r << endl;
else if(~c) cout << "column" << " " << c << endl;
else cout << "impossible" << endl;
return 0;
}