malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n;   double p[3005][305], f[305][3005], d[305];   inline int read() {     int re = 0;     char ch = getchar();     while (ch < 48 || ch > 57) ch = getchar();     while (ch > 47 && ch < 58) {       re = (re << 3) + (re << 1) + (ch ^ 48);       ch = getchar();     }     return re;   }   inline void update(const int x) {     static double g[3005];     memcpy(g, f[x], sizeof(double) * (n + 1));     f[x][0] = 0;     for (int i = 1; i <= n; ++i)       f[x][i] = f[x][i - 1] * (1 - p[i][x]) + g[i - 1] * p[i][x];     d[x] -= f[x][n];   }   int main() {     int m, t;     double w = 0;     n = read();     m = read();     for (int i = 1; i <= n; ++i)       for (int j = 0; j < m; ++j) p[i][j] = (double)read() / 1e3;     for (int i = 0; i < m; ++i) {       f[i][0] = 1;       for (int j = 1; j <= n; ++j) f[i][j] = f[i][j - 1] * (1 - p[j][i]);       d[i] = 1 - f[i][n];     }     for (int i = 0; i < n; ++i) {       t = 0;       for (int j = 1; j < m; ++j)         if (d[t] < d[j]) t = j;       w += d[t];       update(t);     }     printf( %.12lf , w);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 15000000, M = 5005;   int dp[N], n, k, a[M];   set<int> s[23];   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     s[0].insert(0);     for (int i = 1; i <= k; i++)       for (int j = 1; j <= n; j++) s[i].insert(i * a[j]);     int q, x, ans;     for (scanf( %d , &q); q--;) {       scanf( %d , &x);       ans = -1;       for (int t = 1; t <= k && ans < 0; t++)         for (int i = 1; i <= n && ans < 0; i++) {           for (int j = 1; j <= t; j++)             if (s[t - j].find(x - j * a[i]) != s[t - j].end()) {               ans = t;               break;             }         }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a[60], i, x, n, k;     cin >> n >> k;     for (i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     if ((n + 1 - k) >= 1)       cout << a[n + 1 - k] <<     << a[n + 1 - k];     else       cout <<  -1 ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     float n, k, y, s, per;     cin >> n >> k >> y;     per = (n * y) / 100;     s = per - (int)per;     if (s != 0) per = (int)per + 1;     if ((per - k) > 0) {       cout << per - k;     } else {       cout <<  0 ;     }   }
#include <bits/stdc++.h>   using namespace std;   string s;   int a[10005], f[10005][4], q[28000], h2[10005], h3[10005], n;   int main() {     ios::sync_with_stdio(0);     cin >> s;     n = s.size();     for (int i = 1; i <= n; i++) a[i] = s[i - 1] -  a  + 1;     for (int i = 6; i <= n; i++)       h2[i] = a[i] * 900 + a[i + 1] * 30, h3[i] = h2[i] + a[i + 2];     if (n < 7) {       printf( 0 n );       return 0;     }     f[n + 1][2] = f[n + 1][3] = 1, f[n - 1][2] = 1;     q[h2[n - 1]] = 1;     for (int i = n - 2; i > 5; i--) {       if (f[i + 2][3] || (f[i + 2][2] && h2[i] != h2[i + 2]))         f[i][2] = 1, q[h2[i]] = 1;       if (f[i + 3][2] || (f[i + 3][3] && h3[i] != h3[i + 3]))         f[i][3] = 1, q[h3[i]] = 1;     }     int ans = 0;     for (int i = 1; i <= 27500; i++) ans += q[i];     printf( %d n , ans);     for (int i = 1; i <= 27500; i++) {       if (!q[i]) continue;       if (i % 30)         printf( %c%c%c n , (i / 900) + 96, ((i % 900) / 30) + 96, (i % 30) + 96);       else         printf( %c%c n , (i / 900) + 96, ((i % 900) / 30) + 96);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[200000 + 1];   int main() {     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);       sort(a + 1, a + n + 1);       long long mpos = 0, ppos = 0;       for (int i = 1; i <= n; i++) {         if (a[n - i + 1] > 0) ppos = n - i + 1;         if (a[i] < 0) mpos = i;       }       long long left = a[1], right = a[n];       if (left < 0) left *= -1;       long long sum = 0;       if (ppos != 0) {         long long s = ppos - 1;         if ((n - ppos + 1) % k != 0) {           s += (n - ppos + 1) % k;           sum += a[s] * 2;         }         for (int i = s + k; i <= n; i += k) sum += a[i] * 2;       }       if (mpos != 0) {         long long s = mpos + 1;         if ((mpos) % k != 0) {           s -= (mpos % k);           sum -= a[s] * 2;         }         for (long long i = s - k; i >= 1; i -= k) sum -= a[i] * 2;       }       if (ppos != 0 and mpos == 0)         sum -= a[n];       else if (ppos == 0 and mpos != 0)         sum += a[1];       else {         if (left < right)           sum -= a[n];         else           sum += a[1];       }       printf( %lld n , sum);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   struct node {     int x, y;   } f[2 * N];   int n, pos, vt[N], a[N], b[N], dd[N];   vector<pair<int, int> > vec;   bool check(int x) {     if (x == 0) return 0;     while (x) {       if (x % 10 != 7 && x % 10 != 4) return 0;       x /= 10;     }     return 1;   }   int cnt = 0;   int main() {     scanf( %d , &n);     pos = -1;     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), vec.push_back({a[i], i});     bool ok = 0;     for (int i = 1; i < n; ++i) {       if (a[i + 1] < a[i]) {         ok = 1;         break;       }     }     if (!ok) {       printf( 0 );       return 0;     }     sort(vec.begin(), vec.end());     for (int i = 1; i <= n; ++i) {       if (check(a[i])) pos = i;       a[i] = b[i] = lower_bound(vec.begin(), vec.end(), pair<int, int>(a[i], i)) -                     vec.begin();       vt[a[i]] = i;     }     if (pos == -1) {       printf( -1 );       return 0;     }     int x;     dd[b[pos]] = 1;     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; ++i) {       if (dd[b[i]]) continue;       x = vt[b[i]];       if (x == i) continue;       if (i != pos) {         vt[a[pos]] = i;         vt[a[i]] = pos;         swap(a[i], a[pos]);         f[++cnt] = {pos, i};         pos = i;       }       if (x != pos) {         vt[a[pos]] = x;         vt[a[x]] = pos;         swap(a[x], a[pos]);         f[++cnt] = {pos, x};         pos = x;       }     }     printf( %d n , cnt);     for (int i = 1; i <= cnt; ++i) {       printf( %d %d n , f[i].x, f[i].y);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int A[100010], D[100010], n;   set<int> edge[100010];   vector<int> ans;   void bfs() {     int i, u;     memset(A, 0, sizeof(A));     queue<int> que;     ans.clear();     for (i = 1; i <= n; i++) {       if (D[i] == A[i]) {         que.push(i);       }     }     set<int>::iterator it;     while (!que.empty()) {       u = que.front();       que.pop();       if (A[u] != D[u]) continue;       A[u]++;       ans.push_back(u);       for (it = edge[u].begin(); it != edge[u].end(); it++) {         i = *it;         A[i]++;         if (D[i] == A[i]) {           que.push(i);         }       }     }     printf( %d n , ans.size());     for (i = 0; i + 1 < ans.size(); i++) printf( %d  , ans[i]);     if (ans.size()) printf( %d n , ans[ans.size() - 1]);   }   int main() {     int m, i, a, b;     while (scanf( %d%d , &n, &m) != EOF) {       for (i = 1; i <= n; i++) edge[i].clear();       for (i = 1; i <= m; i++) {         scanf( %d%d , &a, &b);         edge[a].insert(b);         edge[b].insert(a);       }       for (i = 1; i <= n; i++) scanf( %d , &D[i]);       bfs();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int INF = 1e9;   using ll = long long;   using ull = unsigned long long;   using vi = vector<int>;   using vl = vector<ll>;   using vb = vector<bool>;   using vc = vector<char>;   using vii = vector<vi>;   using vll = vector<vl>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using vpii = vector<pii>;   using vpll = vector<pll>;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vl v(n);     ll sum = 0;     for (int i = (0); i < (n); i++) {       cin >> v[i];       sum += v[i];     }     ll m = *max_element(v.begin(), v.end());     sum -= m;     if (sum > m)       cout <<  0 n ;     else       cout << m - sum + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   inline bool mina(A &first, B second) {     return (first > second) ? (first = second, 1) : 0;   }   template <class A, class B>   inline bool maxa(A &first, B second) {     return (first < second) ? (first = second, 1) : 0;   }   int mod_count[(105)];   void mult(long long c[(105)][(105)], long long a[(105)][(105)],             long long b[(105)][(105)], int D, int mod) {     for (int(i) = 0; (i) < (D); ++(i)) {       for (int(j) = 0; (j) < (D); ++(j)) {         c[i][j] = 0;         for (int(k) = 0; (k) < (D); ++(k)) {           c[i][j] += a[i][k] * b[k][j];           c[i][j] %= mod;         }       }     }   }   long long temp[(105)][(105)];   long long res[(105)][(105)];   int main() {     int N, B, K, X;     int a;     scanf( %d %d %d %d , &N, &B, &K, &X);     for (int(i) = 0; (i) < (N); ++(i)) {       scanf( %d , &a);       mod_count[a % X]++;     }     long long mat[(105)][(105)];     memset(mat, 0, sizeof(mat));     for (int i = 0; i < X; i++) {       for (int j = 0; j < X; j++) {         for (int k = 0; k < X; k++) {           if ((j * 10 + k) % X == i) {             mat[i][j] += mod_count[k];           }         }       }     }     B--;     memset(res, 0, sizeof(res));     for (int(i) = 0; (i) < (X); ++(i)) res[i][i] = 1;     while (B) {       if (B & 1) {         mult(temp, res, mat, X, 1000000007);         memcpy(res, temp, sizeof(temp));       }       mult(temp, mat, mat, X, 1000000007);       memcpy(mat, temp, sizeof(temp));       B >>= 1;     }     long long ans = 0;     for (int(i) = 0; (i) < (X); ++(i)) {       ans += res[K][i] * mod_count[i];       ans %= 1000000007;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, M, P[100005], R = 1, a, b;   int findSet(int x) {     if (P[x] == x)       return x;     else       return (P[x] = findSet(P[x]));   }   void merge(int a, int b) { P[findSet(a)] = findSet(b); }   int main() {     scanf( %d%d , &N, &M);     for (int i = 1; i <= N; ++i) P[i] = i;     for (int i = 0; i < M; ++i) {       scanf( %d%d , &a, &b);       if (findSet(a) != findSet(b))         merge(a, b);       else {         R += R;         if (R >= 1000000009) R -= 1000000009;       }       if (R > 0)         printf( %d n , R - 1);       else         printf( 1000000008 n );     }   }
#include <bits/stdc++.h>   using namespace std;   struct Point {     long long x, y;   };   const int N = 1000010;   int n;   long long a[N];   Point st[N];   int tp;   bool cmp(long long x1, long long y1, long long x2, long long y2) {     return x2 * y1 <= x1 * y2;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] += a[i - 1];       while (tp && !cmp(st[tp].x - st[tp - 1].x, st[tp].y - st[tp - 1].y,                         i - st[tp].x, a[i] - st[tp].y))         tp--;       st[++tp] = {i, a[i]};     }     for (int i = 1; i <= tp; i++) {       for (int j = st[i - 1].x + 1; j <= st[i].x; j++) {         printf( %.9lf n ,                (double)(st[i].y - st[i - 1].y) / (st[i].x - st[i - 1].x));       }     }     getchar();     getchar();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int path[26][26];   char s[100001], t[100001];   void Floyd_Warshall(int n) {     int i, j, k;     for (k = 0; k < n; k++) {       for (i = 0; i < n; i++) {         for (j = 0; j < n; j++) {           if (path[i][k] + path[k][j] < path[i][j]) {             path[i][j] = path[i][k] + path[k][j];           }         }       }     }   }   int main() {     char A[10], B[10];     int n;     int i, j;     int w;     int len;     int ans;     int minval;     int minnum;     bool flag;     while (scanf( %s%s%d , s, t, &n) != EOF) {       for (i = 0; i < 26; i++) {         for (j = 0; j < 26; j++) {           if (i != j) {             path[i][j] = 1000000000;           }         }       }       for (i = 0; i < n; i++) {         scanf( %s%s%d , A, B, &w);         path[A[0] -  a ][B[0] -  a ] = min(path[A[0] -  a ][B[0] -  a ], w);       }       if (strlen(s) != strlen(t)) {         printf( -1 n );         continue;       }       Floyd_Warshall(26);       len = strlen(s);       ans = 0;       flag = true;       for (i = 0; i < len; i++) {         if (s[i] != t[i]) {           minval = 1000000000;           for (j = 0; j < 26; j++) {             if (path[s[i] -  a ][j] + path[t[i] -  a ][j] < minval) {               minval = path[s[i] -  a ][j] + path[t[i] -  a ][j];               minnum = j;             }           }           if (minval == 1000000000) {             flag = false;             break;           }           ans += minval;           s[i] = minnum +  a ;         }       }       if (flag) {         printf( %d n%s n , ans, s);       } else {         printf( -1 n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[1005][1005], q, opr, xi;   int row_sum[1005], col_sum[1005], total_row_add, total_col_add;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) scanf( %d , &a[i][j]);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (i != j) {           row_sum[i] = (row_sum[i] + a[i][j]) % 2;           col_sum[j] = (col_sum[j] + a[i][j]) % 2;         }     int ans = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         if (i == j)           ans = (ans + a[i][j] * a[i][j]) % 2;         else           ans = (ans + a[i][j] * a[j][i]) % 2;       }     scanf( %d , &q);     while (q--) {       scanf( %d , &opr);       if (opr == 1) {         scanf( %d , &xi);         xi--;         ans ^= 1;         row_sum[xi] += n - 1;         row_sum[xi] %= 2;         ans = (ans + 2 * (col_sum[xi] + total_col_add)) % 2;         col_sum[xi] = (col_sum[xi] + 1) % 2;         total_col_add = (total_col_add + 1) % 2;       } else if (opr == 2) {         scanf( %d , &xi);         xi--;         ans ^= 1;         col_sum[xi] += n - 1;         col_sum[xi] %= 2;         ans = (ans + 2 * (row_sum[xi] + total_row_add)) % 2;         row_sum[xi] = (row_sum[xi] + 1) % 2;         total_row_add = (total_row_add + 1) % 2;       } else {         printf( %d , ans);       }     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n);     map<int, vector<int>> indices;     for (int i = 0; i < n; i++) cin >> a[i], indices[a[i]].push_back(i);     map<int, int> dp;     int mxn = 0, ans = -1;     for (int i = n - 1; i >= 0; i--) {       dp[a[i]] = 1 + dp[a[i] + 1];       if (dp[a[i]] > mxn) mxn = dp[a[i]], ans = a[i];     }     cout << mxn <<   n ;     int prevI = -1, currA = ans;     while (mxn) {       int currI =           lower_bound(indices[currA].begin(), indices[currA].end(), prevI) -           indices[currA].begin();       cout << indices[currA][currI] + 1 <<    ;       prevI = indices[currA][currI], currA++, mxn--;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     int a, b, n, krok = 0;     cin >> a >> b >> n;     bool podm = false;     int g;     while (n > 0) {       if (krok % 2 == 0) {         g = gcd(a, n);         n -= g;         podm = true;       } else {         g = gcd(b, n);         n -= g;         podm = false;       }       krok++;     }     if (podm)       cout <<  0  << endl;     else       cout <<  1  << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int cnt = 0;     int a;     string s;     cin >> s;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  4  || s[i] ==  7 ) {         cnt++;       }     }     if (cnt == 4 || cnt == 7) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int minFact[1000000 + 5];   vector<int> prime;   int rec(int N, int K) {     int ans = 0;     if (N / K <= 10000) {       ans = 0;       for (int i = 1; i <= N / K; ++i) {         ans += (minFact[i] >= K);       }     } else {       ans = N / K;       for (int i = 0; i < prime.size() and prime[i] < K; ++i) {         ans -= rec(N / K, prime[i]);       }     }     return ans;   }   int main() {     minFact[1] = 2 * 1000000007;     for (int i = 2; i <= 100000; ++i) {       if (minFact[i] == 0) {         prime.push_back(i);         for (int j = i; j <= 100000; j += i) {           if (minFact[j] == 0) minFact[j] = i;         }       }     }     int L, R, K;     cin >> L >> R >> K;     for (int i = 2; i * i <= K; ++i) {       if (K % i == 0) {         cout << 0;         return 0;       }     }     cout << rec(R, K) - rec(L - 1, K);   }
#include <bits/stdc++.h>   using namespace std;   inline long long sum(long long x) {     x %= 1000000007;     return (1LL * x * (x + 1) / 2) % 1000000007;   }   int main() {     long long n, m;     ios::sync_with_stdio(false);     cin >> n >> m;     long long ans = ((n % 1000000007) * (m % 1000000007)) % 1000000007;     int rad = sqrt(n);     for (int i = 1; i <= n / (rad + 1) && i <= m; i++) {       ans -= (1LL * i * (n / i)) % 1000000007;       if (ans < 0) ans += 1000000007;     }     for (int i = 1; i <= rad; i++) {       ans -= (1LL * i *               (sum(min(n / i, m)) - sum(min(n / (i + 1), m)) + 1000000007)) %              1000000007;       if (ans < 0) ans += 1000000007;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   char grid[55][55];   char vis[55][55];   int di[] = {0, 1, -1, 0};   int dj[] = {1, 0, 0, -1};   bool dfs(int pi, int pj, char c, int li = -1, int lj = -1) {     if (vis[pi][pj]) return true;     vis[pi][pj] = true;     for (int d = 0; d < 4; d++) {       int ni = pi + di[d];       int nj = pj + dj[d];       if (ni == li && nj == lj) continue;       if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;       if (grid[ni][nj] != c) continue;       if (dfs(ni, nj, c, pi, pj)) return true;     }     return false;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> grid[i];     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (vis[i][j] == false) {           if (dfs(i, j, grid[i][j])) {             cout <<  Yes  << endl;             return 0;           }         }       }     }     cout <<  No  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool gt(long double a, long double b) { return a > b + 1e-12; }   long long a, b, x;   int main() {     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       if (s ==  UR  || s ==  DL )         a++;       else if (s ==  UL  || s ==  DR )         b++;       else         x++;     }     cout << (x + 1 + a) * (x + 1 + b) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long a, b;     cin >> a >> b;     long i = 1;     while (a >= 0 && b >= 0) {       a -= i;       if (a < 0) {         cout <<  Vladik n ;         return 0;       }       b -= i + 1;       if (b < 0) {         cout <<  Valera n ;         return 0;       }       i += 2;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200000 + 5], b[10][200000 + 5];   int main() {     int n, i;     cin >> n;     char c;     c = getchar();     for (i = 1; i <= n; i++) {       c = getchar();       if (c ==  R ) a[i] = 1;       if (c ==  G ) a[i] = 2;       if (c ==  B ) a[i] = 3;     }     b[1][1] = 1;     b[1][2] = 2;     for (i = 3; i <= n; i++) b[1][i] = 6 - b[1][i - 1] - b[1][i - 2];     for (i = 1; i <= n; i++)       if (b[1][i] != a[i]) b[1][0]++;     b[2][1] = 1;     b[2][2] = 3;     for (i = 3; i <= n; i++) b[2][i] = 6 - b[2][i - 1] - b[2][i - 2];     for (i = 1; i <= n; i++)       if (b[2][i] != a[i]) b[2][0]++;     b[3][1] = 2;     b[3][2] = 1;     for (i = 3; i <= n; i++) b[3][i] = 6 - b[3][i - 1] - b[3][i - 2];     for (i = 1; i <= n; i++)       if (b[3][i] != a[i]) b[3][0]++;     b[4][1] = 2;     b[4][2] = 3;     for (i = 3; i <= n; i++) b[4][i] = 6 - b[4][i - 1] - b[4][i - 2];     for (i = 1; i <= n; i++)       if (b[4][i] != a[i]) b[4][0]++;     b[5][1] = 3;     b[5][2] = 1;     for (i = 3; i <= n; i++) b[5][i] = 6 - b[5][i - 1] - b[5][i - 2];     for (i = 1; i <= n; i++)       if (b[5][i] != a[i]) b[5][0]++;     b[6][1] = 3;     b[6][2] = 2;     for (i = 3; i <= n; i++) b[6][i] = 6 - b[6][i - 1] - b[6][i - 2];     for (i = 1; i <= n; i++)       if (b[6][i] != a[i]) b[6][0]++;     int x = 1;     for (i = 1; i <= 6; i++)       if (b[i][0] < b[x][0]) x = i;     cout << b[x][0] << endl;     for (i = 1; i <= n; i++) {       if (b[x][i] == 1) cout <<  R ;       if (b[x][i] == 2) cout <<  G ;       if (b[x][i] == 3) cout <<  B ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int s[100005], num[100005];   bool cmp(int a, int b) { return a < b; }   int main() {     while (scanf( %d , &n) != EOF) {       int i;       for (i = 0; i < n; i++) scanf( %d , &s[i]);       sort(s, s + n, cmp);       bool flag = true;       for (i = 1; i < n - 1; i++) {         if (s[i] == s[i - 1]) continue;         if (s[i] != s[i - 1] + 1) flag = false;         if (s[i] == s[i - 1] + 1 && s[i] == s[i + 1] - 1) flag = false;       }       if (s[n - 1] != s[n - 2] && s[n - 1] != s[n - 2] + 1) flag = false;       if (!flag) {         printf( NO n );         continue;       }       int ma = 0;       memset(num, 0, sizeof(num));       for (i = 0; i < n; i++) {         num[s[i] - s[0] + 1]++;         ma = max(s[i] - s[0] + 1, ma);       }       num[1]--;       num[ma]--;       for (i = 2; i < ma; i++) num[i] -= 2;       for (i = 1; flag && i < ma; i++) {         if (num[i] > 0 && num[i + 1] < num[i]) flag = false;         num[i + 1] -= num[i];         num[i] = 0;       }       if (num[ma] > 0) flag = false;       if (!flag)         printf( NO n );       else         printf( YES n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char a[100005];   int main() {     int n;     scanf( %d , &n);     scanf( %s , &a);     int x = 0, f = 0, sum = n, y = 0;     for (int i = 1; i < n; i++) {       if (a[i] != a[i - 1]) {         sum -= x;         x = 0;         if (i - 2 >= 0 && i + 1 < n && a[i - 2] == a[i - 1] && a[i] == a[i + 1]) {           if (f < 2) f = 2;         } else if (i - 2 >= 0 && i + 1 >= n && a[i - 2] == a[i - 1]) {           if (f == 0) f = 1;         } else if (i - 2 < 0 && i + 1 < n && a[i] == a[i + 1])           if (f == 0) f = 1;       } else {         x++;         y++;         if (i - 1 == 0 || i == n - 1)           if (f == 0) f = 1;       }       if (y > 1) {         if (f < 2) f = 2;       } else if (y == 1)         if (f < 2) f = 1;     }     sum -= x;     sum += f;     printf( %d n , sum);   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5010;   const int INF = 1e5;   static int N, val[MAXN], dp[MAXN][MAXN][2];   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) scanf( %d , &val[i]);     val[0] = -INF;     memset(dp, 0x3f, sizeof dp);     dp[0][0][0] = dp[1][0][0] = dp[1][1][1] = 0;     for (int i = 2; i <= N; i++) {       dp[i][0][0] = 0;       for (int j = 1; j <= i; j++) {         int sub0 = max(0, val[i] - val[i - 1] + 1);         int sub1 = max(0, val[i - 1] - val[i] + 1);         int sub2 = max(0, max(val[i - 1] - val[i], val[i - 1] - val[i - 2]) + 1);         dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] + sub0);         dp[i][j][1] = min(dp[i - 2][j - 1][0] + sub1, dp[i - 2][j - 1][1] + sub2);       }     }     for (int i = 1; i <= (N + 1) / 2; i++)       printf( %d  , min(dp[N][i][0], dp[N][i][1]));     puts(  );     return 0;   }