QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#397947#3756. 字典序SamponYW#AC ✓272ms19740kbC++141.8kb2024-04-24 20:35:042024-04-24 20:35:05

Judging History

This is the latest submission verdict.

  • [2024-04-24 20:35:05]
  • Judged
  • Verdict: AC
  • Time: 272ms
  • Memory: 19740kb
  • [2024-04-24 20:35:04]
  • Submitted

answer

#include <bits/stdc++.h>
#define db double
#define il inline
#define re register
#define ll long long
#define ui unsigned
#define ull ui ll
#define i128 __int128
#define pii pair<int, int>
#define fi first
#define se second
#define eb emplace_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define mems(v, x) memset(v, x, sizeof(v))
#define memc(a, b) memcpy(a, b, sizeof(a))
#define FOR(i, L, R) for(re int i = (L); i <= (R); ++i)
#define ROF(i, R, L) for(re int i = (R); i >= (L); --i)
#define LS i << 1, l, mid
#define RS i << 1 | 1, mid + 1, r
#define popc(x) __builtin_popcount(x)
using namespace std;
#define N 2005
#define P 1000000007
il int add(int x, int y) {return x + y < P ? x + y : x + y - P;}
il void addr(int &x, int y) {(x += y) >= P && (x -= P);}
il int qpow(int p, int n = P - 2) {
  int s = 1;
  while(n) {
    if(n & 1) s = 1ll * s * p % P;
    p = 1ll * p * p % P, n >>= 1;
  }
  return s;
}
int n, m, a[N][N], cnt[N], ans[N];
vector<int> p[N]; bool u[N];
il void WORK() {
  FOR(i, 1, n) {
    FOR(j, 1, m) cin >> a[i][j];
    vector<int>().swap(p[i]), u[i] = 0;
  }
  priority_queue<int, vector<int>, greater<int>> Q;
  FOR(i, 1, m) {
    cnt[i] = 0;
    FOR(j, 1, n - 1) if(a[j][i] > a[j + 1][i])
      ++cnt[i], p[j].eb(i);
    if(!cnt[i]) Q.emplace(i);
  }
  FOR(i, 1, m) {
    // cerr << i << "\n";
    if(Q.empty()) {cout << "-1\n"; return ;}
    int x = Q.top(); ans[i] = x, Q.pop();
    FOR(i, 1, n - 1) if(!u[i] && a[i][x] < a[i + 1][x]) {
      u[i] = 1; for(auto j : p[i]) if(!--cnt[j]) Q.emplace(j);
    }
  }
  FOR(i, 1, m) cout << ans[i] << " \n"[i == m];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  while(cin >> n >> m) WORK();
  cerr << 1.0 * clock() / CLOCKS_PER_SEC;
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 272ms
memory: 19740kb

input:

8 3
6 1 2
3 2 2
7 2 2
7 2 2
7 2 2
9 2 2
10 2 2
4 10 2
6 2
3 1
3 4
3 4
3 7
3 7
3 8
7 7
1 3 3 2 6 3 3
1 3 3 2 6 3 3
1 3 3 2 6 3 3
6 3 3 2 1 4 3
6 3 3 2 4 4 3
7 2 4 2 4 10 6
7 3 4 10 4 3 3
4 3
2 3 9
2 4 9
2 4 9
2 4 9
8 10
9 6 6 3 6 10 1 8 2 5
9 6 6 3 6 10 1 9 2 5
5 3 8 6 7 2 1 7 2 10
5 10 8 7 4 7 1 4 8...

output:

2 1 3
1 2
1 2 3 4 5 6 7
1 2 3
7 3 1 2 4 5 6 8 9 10
3 2 1 4 5
1 3 2 4 5 6 7 8 9
1 2 3 4 5 6
1 2 3 4 5 6
2 1 3 4 5 6
6 4 3 5 1 2 7 8 9 10
1
2 1
2 4 6 7 1 3 5 8
1 2
2 1
1 2 3
1
9 4 3 5 1 2 6 7 8 10
1 2 3
1
3 4 2 1 5 6
1 3 4 5 6 7 2 8 9
2 1 3 4
1 2 3 4 6 5
5 1 2 4 3 6 7 8 9
1 2 3 4 5
1
3 4 1 2 5 6 7 8 9...

result:

ok 110618 tokens