A - Online Shopping,B - Glass and Mug
前两题是模拟题,题目说干啥就干啥就能过。
C - T-shirts
因为要买尽可能少的特殊T恤,所以在1的时候有普通T恤就穿普通的。
感觉和模拟差不多
补题:D - Swapping Puzzle
题目大意:给你A,B矩阵,你可以任意交换A的相邻两行或者相邻两列,求最少操作次数将A矩阵变成B矩阵,如果不行,输出-1
贪心放行和列会被27.txt和28.txt两组数据卡掉,只能换思路了,死磕没用,贪心是错的。
考虑枚举行列的状态,初始行1,2,3,4,5,初始列1,2,3,4,5
全排列行和列,复杂度5!*5!=14400。
#include <bits/stdc++.h>
//#define int long long
#define fr first
#define se second
#define endl '\n'
using namespace std;const int N=10;
int h,w,a[N][N],b[N][N],c[N],d[N],ta[N][N],ans=INT_MAX;bool same(int x[10][10]){for(int i=1;i<=h;++i)for(int j=1;j<=w;++j)if(x[i][j]!=b[i][j])return false;return true;
}void updateans(int x[10],int y[10]){int res=0,z[10];for(int i=1;i<=h;++i)z[i]=x[i];//直接冒泡求逆序对数量for(int j=1;j<=h;++j)for(int i=1;i<h;++i)if(z[i]>z[i+1])swap(z[i],z[i+1]),res++;for(int i=1;i<=w;++i)z[i]=y[i];for(int j=1;j<=w;++j)for(int i=1;i<w;++i)if(z[i]>z[i+1])swap(z[i],z[i+1]),res++;ans=min(ans,res);
}void solve(){cin>>h>>w;for(int i=1;i<=h;++i)for(int j=1;j<=w;++j)cin>>a[i][j];for(int i=1;i<=h;++i)for(int j=1;j<=w;++j)cin>>b[i][j];for(int i=1;i<=5;++i)c[i]=i,d[i]=i;// 1 2 3 4 5//1 1 2 3 4 5//2 5 6 7 8 9do{do{for(int i=1;i<=h;++i)for(int j=1;j<=w;++j)ta[i][j]=a[c[i]][d[j]];//根据全排列c,d决定状态if(same(ta))updateans(c,d);}while(next_permutation(d+1,d+w+1));}while(next_permutation(c+1,c+h+1));if(ans==INT_MAX)ans=-1;cout<<ans<<endl;
}signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);int t=1;while(t--)solve();return 0;
}