Educational Codeforces Round 57 (Rated for Div. 2) ABCDF题解

ZZ出题人写NTT写成ZZ了吧,全是998244353,不需要取模的东西强行取模搞得我以为答案很大想了好久(指B题)

A.任意输出 \([l,r]\) 内的一组满足 \(x \mid y\) 的 \(x, y\) ,保证有答案

我输出了 \(\lfloor \frac{r}{l} \rfloor \times l\) 输出2l也行

int main() {
    int T;
    in, T;
    while (T--) {
        int l, r;
        in, l, r;
        out, l, ' ', r / l * l, '\n';
    }
  return 0;
}

B.给个字符串s(字符集大小 $ > 1$)问有多少种 \([l,r]\) 满足删掉这个区间内的字符以后剩下的字符串字符集大小为 \(1\) (至少删掉一个字符,可以全部删掉)

根据 s[1]是否和s[len]相同讨论,可以删掉 以1为左端点 或者是 以len为右端点的区间,如果相同还可以删掉中间一段区间

char s[MAXN << 1];

int main() {
    int len;
    in, len, s+1;
    int p = 1;
    while (p < len && s[p + 1] == s[p]) ++p;
    int p1 = len;
        while (p1 > 1 && s[p1 - 1] == s[p1]) --p1;
    ll ans;
    ans = p + (len - p1 + 1) + 1;
    if (s[1] == s[len]) { //[p+1,p1-1]
        ans += p * 1ll * (len-p1+1) % mod;
        ans %= mod;
    }
    out, ans;
  return 0;
}

C.给出一个整数角度 \([1,180)\) ,问至少正几边形的三个顶点之间的角可以是这个角度

发现答案最多是360(当时怕想错就枚举到998244353然后break的)而且不会有无解的情况

正x边形三个顶点之间最大的角度就是他的内角 \(\frac{(x-2)180}{x}\) 如果比他大那么正x边形不符合条件(样例第四个不输出90就是因为这个),最小的角是一条边对应圆弧的圆周角也就是 \(\frac{180}{x}\)(注意这里可能会有小数)
我因为没注意到小数然后被除法安排了五十分钟,转成乘法过了,真是恶心

int main() {
    int T;
    in, T;
    while (T--) { //(n - 2) 180 / n | ang
        int ang, Min = inft;
        in, ang;
        ang <<= 1;
        lop(i, 3, 998244353) {
            if (360 % i) continue;
            if (360 * 1ll * (i-2) >= ang * 1ll * i && ang * 1ll * i % 360 == 0) {
                Min = i;
                break;
            }
        }
        out, Min, '\n';
    }
  return 0;
}

D.给一个字符串 \(s\) 和删除每一位的代价 \(a\),求最少的代价使得s中不含子序列 "hard"

dp[i][0/1/2/3]表示使前i位不含h/ha/har/hard子序列的最小代价

ll dp[MAXN][4];
int len, a[MAXN], MP[256]; char s[MAXN];
// no h/no ha/no har/no hard
int main() {
    in, len, s+1;
    lop1(i,len) in, a[i];
    lop(i,'a','z') MP[i] = -1;
    MP['h'] = 0, MP['a'] = 1, MP['r'] = 2, MP['d'] = 3;
    lop1(i,len) {
        lop0(j,4) dp[i][j] = dp[i-1][j];
        if (MP[s[i]] == -1) continue;
        dp[i][MP[s[i]]] += a[i];
        if (MP[s[i]]) chmin(dp[i][MP[s[i]]], dp[i-1][MP[s[i]]-1]);
    }
    out, dp[len][3];

  return 0;
}

去偷了一个写的好看点的代码

int n, a[MAXN];
LL ans, dp[4];
char s[MAXN];
int main() {
    read(n);
    scanf("%s", s+1);
    memset(dp, 127, sizeof(127)), dp[0] = 0;
    rep(i, 1, n) {
        read(a[i]);
        if(s[i] == 'd') dp[3] += a[i];
        if(s[i] == 'r') chkmin(dp[3], dp[2]), dp[2] += a[i];
        if(s[i] == 'a') chkmin(dp[2], dp[1]), dp[1] += a[i];
        if(s[i] == 'h') chkmin(dp[1], dp[0]), dp[0] += a[i];
    }
    ans = min(min(dp[0], dp[1]), min(dp[2], dp[3]));
    cout<<ans;
    return 0;
}

F.给一个排列(有些是-1,代表等概率是没出现过数字中的任意一个),求期望逆序对个数

分成三部分(x表示已知)

  • -1和-1之间的
  • -1和x之间的
  • x和x之间的

具体柿子在下面

// -1 -1 cnt * (cnt - 1) / 2 / 2
// -1 x cnt(-1)<greater than x> * cnt(-1)<on the left of x> / cnt(-1)
// x -1 cnt(-1)<less than x> * cnt(-1)<on the right of x> / cnt(-1)
// x x 

int a[MAXN], n, gt[MAXN], ls[MAXN], cnt[MAXN]; //cnt[i] [1..i]-1的个数 gt[i] 比 i 大的-1个数 | ls[i] 比 i 小的-1个数
bool vis[MAXN];

struct BIT {
  int t[MAXN];
  inline void add(int k, int v) {
    while (k <= n) t[k] += v, k += k & -k;
  }
  inline int query(int k) {
    int ret = 0;
    while (k) ret += t[k], k -= k & -k;
    return ret;
  }
} bit;
// a[i] -> gt[1,a[i]-1]
// a[i] -> ls[a[i]+1,n]

int main() {
  int ans = 0, tmp = 0;
  in, n;
  lop1(i, n) {
    in, a[i];
    if (~a[i]) vis[a[i]] = 1, bit.add(a[i], 1), ans += ++tmp - bit.query(a[i]), Mod(ans, mod);
    else cnt[i] = 1;

  }
  // out, ans, '\n';
  lop1(i, n) {
    cnt[i] += cnt[i-1];
    if (!vis[i]) ++gt[1], --gt[i], ++ls[i + 1];//, --ls[n + 1];
  }
  ans += cnt[n] * 1ll * (cnt[n] - 1) % mod * Pow(4, mod-2, mod) % mod;
  Mod(ans, mod);
  lop1(i, n) gt[i] += gt[i-1], ls[i] += ls[i-1];
  int Inv = Pow(cnt[n], mod - 2, mod);
  lop1(i, n)
    if (~a[i]) {
      int x = ls[a[i]] * 1ll * (cnt[n] - cnt[i - 1]) % mod + gt[a[i]] * 1ll * cnt[i - 1] % mod;
      Mod(x, mod);
      ans += x * 1ll * Inv % mod;
      Mod(ans, mod);
    }
  out, ans;
  return 0;
}

原文地址:https://www.cnblogs.com/storz/p/10198953.html

时间: 2024-10-07 17:09:47

Educational Codeforces Round 57 (Rated for Div. 2) ABCDF题解的相关文章

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html

Educational Codeforces Round 59 (Rated for Div. 2) DE题解

Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contest/1107/problem/D 题意: 给出一个n*(n/4)的矩阵,这个矩阵原本是一些01矩阵,但是现在四个四个储存进二进制里面,现在给出的矩阵为0~9以及A~F,表示0~15. 然后问这个矩阵能否压缩为一个(n/x)*(n/x)的矩阵,满足原矩阵中大小为x*x的子矩阵所有数都相等(所有子矩阵构

Educational Codeforces Round 57 (Rated for Div. 2) 前三个题补题

感慨 最终就做出来一个题,第二题差一点公式想错了,又是一波掉分,不过我相信我一定能爬上去的 A Find Divisible(思维) 上来就T了,后来直接想到了题解的O(1)解法,直接输出左边界和左边界*2即可 代码 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long x,y,t; cin>>t;

Educational Codeforces Round 80 (Rated for Div. 2)部分题解

A. Deadline 题目链接 题目大意 给你\(n,d\)两个数,问是否存在\(x\)使得\(x+\frac{d}{x+1}\leq n\),其中\(\frac{d}{x+1}\)向上取整. 解题思路 方案一:利用均值不等式公式推导 \(x+\frac{d}{x+1}=x+1+\frac{d}{x+1}-1\geq2\sqrt{d}-1\) 所以 \(\min(x+\frac{x}{d+1})=2\sqrt{d}-1\) 因此去判断\(2\sqrt{d}-1\leq n\)是否成,即\(4\

Educational Codeforces Round 84 (Rated for Div. 2) A-E题解

A. Sum of Odd Integers 首先可以算出从1开始到第k个奇数之和.如果和大于n,则不可能存在k个奇数加和等于n,否则用n减去前k个奇数的和,这个差值若是偶数,直接加到最大的奇数上,就可以满足题意要求,否则输出no. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int main(){ 5 int t; 6 cin>>t; 7 while(t--){ 8 ll n

Educational Codeforces Round 73 (Rated for Div. 2)

比赛链接:Educational Codeforces Round 73 (Rated for Div. 2) 官方题解:Educational Codeforces Round 73 Editorial A. 2048 Game 题意 如果一个只包含 \(2\) 的幂次的集合,问能否从中选择一些数使得和为 \(2048\). 思路 不断合并直到凑到 \(2048\). 代码 #include <bits/stdc++.h> using namespace std; int main() {

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is