QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360188 | #7615. Sequence Folding | willow# | TL | 1ms | 3832kb | C++17 | 2.3kb | 2024-03-21 14:41:32 | 2024-03-21 14:41:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long n, dp[maxn][2], tdp[maxn][2];
int m;
vector<long long> pos, ps;
map<long long, int> id, tmp;
set<long long> s;
long long Solve(long long n, int len) {
// cerr << "Solve(" << n << ", " << len << ")" << endl;
// for(auto x : pos)
// cerr << x << " ";
// cerr << endl;
// for(int i = 0; i < len; ++ i)
// cerr << dp[i][0] << " ";
// cerr << endl;
// for(int i = 0; i < len; ++ i)
// cerr << dp[i][1] << " ";
// cerr << endl;
for(int l = 0, r = len - 1; l < r; ++ l, -- r) {
for(int i = 0; i < 2; ++ i) {
tdp[l][i] = 2e18;
for(int j = 0; j < 2; ++ j) {
for(int k = 0; k < 2; ++ k) {
tdp[l][i] = min(tdp[l][i], dp[l][j] + dp[r][k] + (i != j) + (i != k));
}
}
}
}
if(n == 2) {
// cerr << tdp[0][0] << " " << tdp[0][1] << endl;
return min(tdp[0][0], tdp[0][1]);
}
long long mid = n / 2;
ps.clear(), tmp.clear();
for(int l = 0; l < len / 2; ++ l) {
assert(pos[l] <= mid);
ps.push_back(pos[l]);
ps.push_back(mid + 1 - pos[l]);
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
int nl = ps.size();
for(int i = 0; i < nl; ++ i) {
tmp[ps[i]] = i;
if(id.count(ps[i])) {
dp[i][0] = tdp[id[ps[i]]][0];
dp[i][1] = tdp[id[ps[i]]][1];
}
else {
dp[i][0] = 0;
dp[i][1] = 1;
}
}
swap(id, tmp);
swap(pos, ps);
return Solve(n / 2, nl);
}
mt19937_64 rnd(random_device{}());
int main() {
scanf("%lld%d", &n, &m);
// n = 1ll << 59;
// m = 1e5;
for(int i = 0; i < m; ++ i) {
long long x;
scanf("%lld", &x);
// x = rnd() % n + 1;
s.insert(x);
pos.push_back(x);
pos.push_back(n + 1 - x);
}
sort(pos.begin(), pos.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end());
int len = pos.size();
for(int i = 0; i < len; ++ i) {
int v = s.count(pos[i]);
id[pos[i]] = i;
dp[i][v] = 0;
dp[i][v ^ 1] = 1;
}
printf("%lld\n", Solve(n, len));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3832kb
input:
8 3 1 5 8
output:
2
result:
ok single line: '2'
Test #2:
score: -100
Time Limit Exceeded
input:
1 1 1