Codeforces Round 938 (Div. 3) B. Progressive Square

题目链接:题目

大意:

按某种方式排列n*n方阵,问给出的方阵重新排序后有没有可能成为这种方阵。

思路:

一个这种方阵仅由n,c,d以及a[0][0]决定,所以它唯一可能对应的方阵我们可以找到,然后就是比较两个方阵里的数能不能对应,就像比较两个字符串是不是错排了,只要分别排序再比较就行了。
另一种方法就是,因为可以看做是找配对的问题,我们可以把给出的数字都放到一个集合里,然后看我们所需的数字是否都有,由于数字可能重复,所以不能使用unordered_set,只能使用set所以复杂度还是一样。

代码:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define MOD 1000000007
#define fi first
#define se second
#define pii pair<int,int>
#define vec vector

int solve(int n,int c,int d,vec<int> a){
    sort(a.begin(),a.end());
    vec<int> q;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            q.push_back(a[0]+c*i+d*j);
        }
    }
    sort(q.begin(),q.end());
    for(int i=0;i<n*n;i++){
        if(a[i]!=q[i])return false;
    }
    return true;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n,c,d;
        cin>>n>>c>>d;
        vec<int> a(n*n);
        for(int i=0;i<n*n;i++)cin>>a[i];
        if(solve(n,c,d,a))cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值