QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#767580 | #4936. Shopping Changes | Fireball0424# | RE | 0ms | 3836kb | C++17 | 2.5kb | 2024-11-20 21:18:30 | 2024-11-20 21:18:31 |
Judging History
answer
// #pragma GCC optimizer("Ofast")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define ull unsigned long long
#define fr first
#define fi first
#define sc second
#define se second
#define all(x) x.begin(), x.end()
#define pii pair<int,int>
//#define sz(x) (int)x.size()
using namespace std;
#ifndef fireball
#define tofu ios::sync_with_stdio(0); cin.tie(0);
#else
#define tofu freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
void db() {cout << '\n';}
template <typename T, typename ...U> void db(T a, U ...b) {cout << a << ' ', db(b...);}
const int N = 1e5 + 5;
int bit[N] = {};
vector<int> undo;
void init(){
for(int i : undo) bit[i] = 0;
undo.clear();
}
void add(int pos, int val){
for(int i = pos; i < N; i += (i & (-i))){
bit[i] += val;
undo.push_back(i);
}
}
int query(int pos){
int res = 0;
for(int i = pos; i; i -= (i & (-i))) res += bit[i];
return res;
}
signed main(){
tofu;
int n, m;
cin >> n >> m;
vector<int> v, a(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
v.push_back(a[i]);
}
vector<vector<int> > b;
for(int i = 0; i < m; ++i){
int l; cin >> l;
b.push_back(vector<int>(l));
for(int j = 0; j < l; ++j){
cin >> b[i][j];
v.push_back(b[i][j]);
}
}
sort(all(v));
v.erase(unique(all(v)), v.end());
for(int i = 0; i < n; ++i) a[i] = lower_bound(all(v), a[i]) - v.begin() + 1;
for(int i = 0; i < m; ++i){
for(int &j : b[i]) j = lower_bound(all(v), j) - v.begin() + 1;
}
int fix = 0;
// in-first
for(int i = n - 1; i >= 0; --i){
fix += query(a[i] - 1);
add(a[i], 1);
}
sort(all(a));
// in-second
for(int i = 0; i < m; ++i){
int l = (int)b[i].size();
init();
int in = 0, btw = 0;
for(int j = l - 1; j >= 0; --j){
in += query(b[i][j] - 1);
add(b[i][j], 1);
btw += lower_bound(all(a), b[i][j]) - a.begin();
}
//db(in, btw);
int ans = fix + in + btw;
for(int j = l - 1; j >= 0; --j){
btw -= lower_bound(all(a), b[i][j]) - a.begin();
btw += n - (upper_bound(all(a), b[i][j]) - a.begin());
ans = min(ans, fix + in + btw);
}
db(ans);
//db();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3548kb
input:
3 3 5 6 7 6 2 3 4 8 9 10 2 100 99 3 5 6 7
output:
0 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
3 2 7 6 5 6 2 3 4 8 9 10 6 10 9 8 4 3 2
output:
3 27
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
1 1 589284012 1 767928734
output:
0
result:
ok single line: '0 '
Test #4:
score: -100
Runtime Error
input:
10000 9999 298772847 712804102 869795012 527998188 804246450 598105371 843966934 639805471 937482040 887551242 254734680 188704975 17408126 626523673 553956319 697723205 231690822 637079761 232393146 377026277 962223856 338922458 912529500 710873344 942955137 51167037 195729799 529674367 990599310 4...