赞
踩
vector<vector<int>> a(n, vector<int>(m)); // n 行 m 列 的二维数组
行和列的下标都是从0开始,即(0 ~ n - 1 , 0 ~ m - 1); 数组元素值默认为 0。
vector<vector<int>> a(n, vector<int>(m, value)); // n 行 m 列 的二维数组
初始化为value。
- #include<bits/stdc++.h>
- using namespace std;
- #define endl '\n'
-
- const int N = 2e5 + 10;
- int n, m;
- typedef pair<int, int>PII;
-
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
-
- int t;
- cin >> t;
- while(t--)
- {
- cin >> n >> m;
- vector<vector<int>> a(n, vector<int>(m)), b(n, vector<int>(m)); // n * m 的二维数组
- for(int i = 0; i < n; i++)
- for(int j = 0; j < m; j++)
- {
- cin >> a[i][j];
- b[i][j] = a[i][j];
- }
-
- for(int i = 0; i < n; i++)
- sort(a[i].begin(), a[i].end());
-
- int cnt = 0;
- bool ok = true;
- set<PII>st;
- for(int i = 0; i < n; i++)
- {
- cnt = 0;
- int l = 0, r = 0;
- for(int j = 0; j < m; j++)
- {
- if(a[i][j] != b[i][j])
- {
- cnt++;
- if(l == 0) l = j + 1;
- else r = j + 1;
- }
- }
- if(cnt >= 3) ok = false;
- else if(cnt == 2) st.insert({l, r});
- }
- if(!ok) cout << "-1" << endl;
- else
- {
- if(st.size() == 0) cout << 1 << " " << 1 << endl;
- else if(st.size() == 1) {
- int l, r;
- for(auto p : st){
- l = p.first, r = p.second;
- }
- ok = false;
- for(int i = 0; i < n; i++)
- if(b[i][l - 1] < b[i][r - 1]) ok = true;
- if(ok) cout << "-1" << endl;
- else cout << l << " " << r << endl;
- }
- else cout << "-1" << endl;
- }
-
- }
-
- return 0;
- }
vector 开三维数组
- // d[n + 1][m + 1][5] = {INF}
- vector< vector < vector<int> > > d(n + 1,vector< vector<int> >(m + 1,vector<int>(5,INF)));
-
- // vis[n + 1][m + 1][5] = {false}
- vector< vector < vector<bool> > > vis(n + 1,vector< vector<bool> >(m + 1,vector<bool>(5,false)));
- vector dp(N+1,vector(K+1,vector<long long>(D,-1)));
-
- dp[N][K][D]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。