思维题

E. Matrix and Shifts

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a binary matrix AA of size n×nn×n. Rows are numbered from top to bottom from 11 to nn, columns are numbered from left to right from 11 to nn. The element located at the intersection of row ii and column jj is called AijAij. Consider a set of 44 operations:

  1. Cyclically shift all rows up. The row with index ii will be written in place of the row i−1i−1 (2≤i≤n2≤i≤n), the row with index 11 will be written in place of the row nn.
  2. Cyclically shift all rows down. The row with index ii will be written in place of the row i+1i+1 (1≤i≤n−11≤i≤n−1), the row with index nn will be written in place of the row 11.
  3. Cyclically shift all columns to the left. The column with index jj will be written in place of the column j−1j−1 (2≤j≤n2≤j≤n), the column with index 11 will be written in place of the column nn.
  4. Cyclically shift all columns to the right. The column with index jj will be written in place of the column j+1j+1 (1≤j≤n−11≤j≤n−1), the column with index nn will be written in place of the column 11.

The 3×33×3 matrix is shown on the left before the 33-rd operation is applied to it, on the right — after.

You can perform an arbitrary (possibly zero) number of operations on the matrix; the operations can be performed in any order.

After that, you can perform an arbitrary (possibly zero) number of new xor-operations:

  • Select any element AijAij and assign it with new value Aij⊕1Aij⊕1. In other words, the value of (Aij+1)mod2(Aij+1)mod2 will have to be written into element AijAij.

Each application of this xor-operation costs one burl. Note that the 44 shift operations — are free. These 44 operations can only be performed before xor-operations are performed.

Output the minimum number of burles you would have to pay to make the AA matrix unitary. A unitary matrix is a matrix with ones on the main diagonal and the rest of its elements are zeros (that is, Aij=1Aij=1 if i=ji=j and Aij=0Aij=0 otherwise).

Input

The first line of the input contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases in the test.

The descriptions of the test cases follow. Before each test case, an empty line is written in the input.

The first line of each test case contains a single number nn (1≤n≤20001≤n≤2000)

This is followed by nn lines, each containing exactly nn characters and consisting only of zeros and ones. These lines describe the values in the elements of the matrix.

It is guaranteed that the sum of n2n2 values over all test cases does not exceed 4⋅1064⋅106.

Output

For each test case, output the minimum number of burles you would have to pay to make the AA matrix unitary. In other words, print the minimum number of xor-operations it will take after applying cyclic shifts to the matrix for the AA matrix to become unitary

 四个操作可将原矩阵复制三次变成2n*2n的矩阵

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=4e3+5;
char mp[N][N];
int p[N][N];
void solve()
{
    int n;cin>>n;
    int cnt=0;
    //cout<<66<<endl;
    getchar();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%c",&mp[i][j]);
            //cin>>mp[i][j];
            //cout<<mp[i][j];
            if(mp[i][j]=='1') cnt++;
            mp[i+n][j]=mp[i][j];
            mp[i][j+n]=mp[i][j];
            mp[i+n][j+n]=mp[i][j];
        }
        getchar();
        //cout<<endl;
    }
    for(int i=1;i<=2*n;i++)
    {
        for(int j=1;j<=2*n;j++)
        {
            p[i][j]=p[i-1][j-1]+(mp[i][j]=='1');
        }
    }
    int res=n*n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int num=p[i+n-1][j+n-1]-p[i-1][j-1];
            res=min(res,cnt-num+n-num);
        }
    }
    printf("%d\n",res);
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        solve();
    }
}

N people, numbered 1,2,\ldots ,N1,2,…,N, are going to stand on the number line. Let's denote by x_ixi​ the coordinate the Person ii stands at. Then, x_ixi​ should be an integer satisfying L_i \leq x_i \leq R_iLi​≤xi​≤Ri​. Multiple people can occupy the same coordinate.

We define the dissatisfaction level as the following formula:

\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^{N}|x_j-x_i|i=1∑N−1​j=i+1∑N​∣xj​−xi​∣

Find the minimum possible value of the dissatisfaction level.

C - Min Diff Sum

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=3e5+5;
ll l[N],r[N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n;cin>>n;
    for(int i=1;i<=n;i++) cin>>l[i]>>r[i];
    sort(l+1,l+1+n,greater<>());
    sort(r+1,r+1+n);
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        ans+=max(0ll,l[i]-r[i])*(n-2*(i-1)-1);
    }
    cout<<ans<<'\n';
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值