intmain(){ scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); int ans = inf; for (int l = 0; l <= n; l++) { set<int> st; int flag = 0; for (int i = 1; i <= l; i++) { if (st.count(a[i])) { flag = 1; break; } st.insert(a[i]); } if (flag) break; ans = min(ans, n - l); for (int r = n; r > l; r--) { if (st.count(a[r])) break; st.insert(a[r]); ans = min(ans, r - l - 1); } } printf("%d\n", ans); return0; }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<utility> #include<cassert> #ifdef XLor #define dbg(args...) do { cout << #args << " ->"; err(args); } while (0) void err() { std::cout << std::endl; } template<typename T, typename...Args> void err(T a, Args...args) { std::cout << a << ' '; err(args...); } #else #define dbg(...) #endif #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 200000 + 5; int n, p[maxn]; ll s[maxn]; struct BIT { ll a[maxn]; inline int lowbit(int x) { return x & -x; } void insert(int i, int x) { for (; i <= n; i += lowbit(i)) a[i] += x; } ll query(int i) { ll r = 0; for (; i; i -= lowbit(i)) r += a[i]; return r; } } f; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", s + i); for (int i = 1; i <= n; i++) f.insert(i, i); for (int i = n; i >= 1; i--) { int l = 0, r = n, ans = -1; while (l <= r) { int m = (l + r) / 2; if (f.query(m) <= s[i]) ans = m + 1, l = m + 1; else r = m - 1; } dbg(ans); assert(ans != -1); f.insert(ans, -ans); p[i] = ans; // int x = f.findx(s[i] + 1); // p[i] = x; // f.insert(x, -1); } for (int i = 1; i <= n; i++) printf("%d ", p[i]); return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<set> #include<utility> #include<functional> #include<cassert> #ifdef XLor #define dbg(args...) do { cout << #args << " ->"; err(args); } while (0) void err() { std::cout << std::endl; } template<typename T, typename...Args> void err(T a, Args...args) { std::cout << a << ' '; err(args...); } #else #define dbg(...) #endif #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 1000000 + 5; int n, w; ll ans[maxn]; int dp[21][maxn]; void solve(const vector<int>& a) { int n = (int)a.size(); if (n == w) { for (int i = 0; i < w; i++) { ans[i] += a[i]; ans[i + 1] -= a[i]; } return ; } for (int i = 0; i < n; i++) dp[0][i] = i; for (int j = 1; j <= 20; j++) { for (int i = 0; i + (1 << j) <= n; i++) { int l = dp[j - 1][i]; int r = dp[j - 1][i + (1 << (j - 1))]; if (a[l] > a[r]) dp[j][i] = l; else dp[j][i] = r; } } auto qmax = [&](int l, int r) -> int { int k = __lg(r - l + 1); int x = dp[k][l], y = dp[k][r - (1 << k) + 1]; if (a[x] > a[y]) return x; else return y; }; for (int i = 0; i < w; i++) { int l = max(i - w + n, 0); int r = min(i, n - 1); assert(l <= r); int mx = a[qmax(l, r)]; if (mx < 0 && (i >= n || i < w - n)) { mx = 0; } ans[i] += mx; if (l == 0 && r == n - 1 && i >= n && w >= 2 * n + 1) { i = max(i, w - 1 - n); } ans[i + 1] -= mx; } } int main() { scanf("%d%d", &n, &w); for (int i = 1, x, m; i <= n; i++) { scanf("%d", &m); vector<int> line; for (int j = 1; j <= m; j++) { scanf("%d", &x); line.push_back(x); } solve(line); } for (int i = 0; i < w; i++) printf("%I64d ", ans[i] += ans[i - 1]); return 0; }