题意:给一个二维数组,问你能不能通过交换它的某两列(可以是相同的)来试所有的二维数组的某一列是非递减的。
思路:因为所有的列都要是相等的,那么最后的结果一定 是排好序的,那么我们可以提前排好序(md怎么没想到 呢),然后排序后的跟排序前的不一样的话,那么就需要转换,然后把这个下标存起来,然后交换的次数++。最后根据交换的次数(如果大于2就肯定不行了)然后交换,如果还存在递增的就直接"no",否则输出两次的操作。
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N];
void slove()
{
cin>>n>>m;
vector<vector<int> >v(n,vector<int>(m));
vector<int>b;
vector<int>res;
for(int i =0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>v[i][j];
}
}
for(int i =0;i<n&&res.empty();i++)
{
b=v[i];
sort(b.begin(),b.end());
for(int j=0;j<m;j++)
if(b[j]!=v[i][j])
res.push_back(j);
}
if(res.size()==0)
{
cout<<1<<" "<<1<<endl;
}
else if(res.size()>2)
{
cout<<"-1\n";
}
else {
bool flag=0;
for(int i =0;i<n;i++)
{
swap(v[i][res[0]],v[i][res[1]]);
for(int j =1;j<m;j++)
{
if(v[i][j-1]>v[i][j])
flag=1;
}
}
if(flag)cout<<"-1\n";
else cout<<res[0]+1<<" "<<res[1]+1<<endl;
}
}
int main()
{
int t;
sc_int(t);
while(t--)
{
slove();
}
return 0;
}
这个代码是我看着别的大佬照着写的所以看着很像(因为不会这么玩vectorqwq),写完感觉自己对vector的理解又深了一步~