teamnote default

This commit is contained in:
2026-06-03 09:20:51 +09:00
commit f50ed902fe
65 changed files with 6033 additions and 0 deletions

12
source/DP/DnC.cpp Normal file
View File

@@ -0,0 +1,12 @@
//D[t][s...e]를 구해야 하고, j의 탐색 범위는 [l, r]
void f(int t, int s, int e, int l, int r){
if(s > e) return;
int m = s + e >> 1;
int opt = l;
for(int i=l; i<=r; i++){
if(D[t-1][opt] + C[opt][m] > D[t-1][i] + C[i][m]) opt = i;
}
D[t][m] = D[t-1][opt] + C[opt][m];
f(t, s, m-1, l, opt);
f(t, m+1, e, opt, r);
}

17
source/DP/LIS.cpp Normal file
View File

@@ -0,0 +1,17 @@
template<class I> vi lis(const vector<I>& S) {
if (S.empty()) return {};
vi prev(sz(S));
typedef pair<I, int> p;
vector<p> res;
rep(i,0,sz(S)) {
// change 0 -> i for longest non-decreasing subsequence
auto it = lower_bound(all(res), p{S[i], 0});
if (it == res.end()) res.emplace_back(), it = res.end()-1;
*it = {S[i], i};
prev[i] = it == res.begin() ? 0 : (it-1)->second;
}
int L = sz(res), cur = res.back().second;
vi ans(L);
while (L--) ans[L] = cur, cur = prev[cur];
return ans;
}

View File

@@ -0,0 +1,30 @@
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};

View File

@@ -0,0 +1,38 @@
namespace LC
{
vll line;
int getline(ll c)
{
int k = 0, st = 20;
while(st+1)
{
int now = k+(1<<st); st--;
if(now >= line.size()) continue;
if(line[now].fi*c+line[now].se < line[now-1].fi*c+line[now-1].se) k = now;
}
return k;
}
void pushline(pll C)
{
while(line.size() >= 2)
{
pll A = line[line.size()-2];
pll B = line.back();
A = {A.fi-C.fi, A.se-C.se};
B = {B.fi-C.fi, B.se-C.se};
A.se = -A.se; B.se = -B.se;
if(A.fi<0) A = {-A.fi, -A.se};
if(B.fi<0) B = {-B.fi, -B.se};
if(A.se*B.fi >= B.se*A.fi) line.pop_back();
else break;
}
line.pb(C);
}
} // namespace LC

52
source/DP/SlopeTrick.cpp Normal file
View File

@@ -0,0 +1,52 @@
const int N = 1e6+7;
int arr[N];
priority_queue<int> pq;
ll ans = 0;
int main()
{
getint(n);
forr(i, n) scanf("%d", arr+i);
pq.push(arr[1]); int t=0; ll val = 0;
fors(i, 2, n)
{
t++;
int r = t + pq.top();
if(r <= arr[i]) pq.push(arr[i]-t);
else
{
pq.push(arr[i]-t); pq.push(arr[i]-t); pq.pop();
ans += r-arr[i];
}
}
printf("%lld", ans);
}
int arr[N];
priority_queue<int> pq;
int ans2[N];
int main()
{
getint(n);
forr(i, n) scanf("%d", arr+i);
pq.push(arr[1]); ll ans = 0;
ans2[1] = arr[1];
fors(i, 2, n)
{
int r = (i-1) + pq.top();
if(r <= arr[i]) pq.push(arr[i]-(i-1));
else
{
pq.push(arr[i]-(i-1)); pq.push(arr[i]-(i-1)); pq.pop();
ans += r-arr[i];
}
ans2[i] = pq.top() + (i-1);
}
fore(i, n-1, 1) ans2[i] = min(ans2[i], ans2[i+1]-1);
forr(i, n) printf("%d\n", ans2[i]);
}

22
source/DP/SoS.cpp Normal file
View File

@@ -0,0 +1,22 @@
int n = 20;
vector<int> a(1 << n);
// keeps track of the sum over subsets
// with a certain amount of matching bits in the prefix
vector<vector<int>> dp(1 << n, vector<int>(n));
vector<int> sos(1 << n);
for (int mask = 0; mask < (1 << n); mask++) {
dp[mask][-1] = a[mask];
for (int x = 0; x < n; x++) {
dp[mask][x] = dp[mask][x - 1];
if (mask & (1 << x)) { dp[mask][x] += dp[mask - (1 << x)][x - 1]; }
}
sos[mask] = dp[mask][n - 1];
}
////////////////////////////////////////////
D[i] i에
fors(d, 0, 19) fors(i,0,(1<<20)-1)
if(i & (1<<d)) D[i] += D[i^(1<<d)];
-> D[i] : sum of subset of mask i