QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#266622 | #7615. Sequence Folding | ucup-team173# | ML | 43ms | 315412kb | C++20 | 1.7kb | 2023-11-26 15:57:59 | 2023-11-26 15:58:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define Mp make_pair
#define SZ(x) ((int)(x).size())
typedef double db;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define int ll
const int MAXN = 1.1e5 + 5;
pair<int, ll> s[MAXN * 60];
int f[MAXN * 60][2];
map<pair<int, ll>, int> mp;
set<int> Son[MAXN * 60];
ll n, a[MAXN];
int m, tot, rt;
void dfs(int x) {
if(!x) {
f[x][0] = 0, f[x][1] = 1;
return ;
}
if(s[x].first == 0) {
f[x][0] = 1, f[x][1] = 0;
return ;
}
int ls = (Son[x].size() >= 1 ? *Son[x].begin() : 0);
int rs = (Son[x].size() >= 2 ? *Son[x].rbegin() : 0);
dfs(ls), dfs(rs);
f[x][0] = f[ls][0] + f[rs][0];
f[x][1] = f[ls][1] + f[rs][1];
f[x][0] = min(f[x][0], f[x][1] + 1);
f[x][1] = min(f[x][1], f[x][0] + 1);
}
void solve() {
tot = 0, mp.clear();
cin >> n >> m;
for(int i = 1; i <= m; i++) {
cin >> a[i];
s[++tot] = Mp(0, a[i]), mp[Mp(0, a[i])] = tot, Son[tot].clear();
ll now = (a[i] > (n >> 1) ? n - a[i] + 1 : a[i]), last = tot;
for(ll t = n >> 1, j = 1; t; t >>= 1, j++) {
if(!mp.count(Mp(j, now))) s[++tot] = Mp(j, now), mp[Mp(j, now)] = tot, Son[tot].clear();
Son[mp[Mp(j, now)]].insert(last), last = mp[Mp(j, now)];
now = (now > (t >> 1) ? t - now + 1 : now);
}
}
rt = max_element(s + 1, s + tot + 1) - s, dfs(rt);
cout << min(f[rt][0], f[rt][1]) << '\n';
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 43ms
memory: 315412kb
input:
8 3 1 5 8
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 40ms
memory: 315316kb
input:
1 1 1
output:
0
result:
ok single line: '0'
Test #3:
score: -100
Memory Limit Exceeded
input:
17179869184 100000 138476 774165 993977 1152277 1236393 1244970 1534017 1693701 1763926 1778781 1842066 1874644 1885666 2120429 2485344 2977941 3158255 3340476 3504862 4000117 4066652 4197639 4338723 4389163 4576965 4761601 5097091 5175412 5295902 5810551 5855982 6001770 6111262 6163309 6351547 6582...
output:
99999