C. Column Swapping Codeforces Round #792 (Div. 1 + Div. 2)

传送门

题意:给一个二维数组,问你能不能通过交换它的某两列(可以是相同的)来试所有的二维数组的某一列是非递减的。

思路:因为所有的列都要是相等的,那么最后的结果一定 是排好序的,那么我们可以提前排好序(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的理解又深了一步~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值