A. Death Note

简单模拟,可用\(\%\)和 \(/\)来减少代码量

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 200010;
int n, m, a[N], cnt = 0, tot = 0;
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%d", a + i);
cnt = (tot + a[i]) / m;
tot = (tot + a[i]) % m;
printf("%d ", cnt);
}
}

B - Segment Occurrences

预处理\(A、B\)的\(Hash\)表,可以将时间复杂度降到\(O(qn)\)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef unsigned long long ULL;
const int N = 1010, B = 221;
int n, m, q, len;
char s[N], t[N];
ULL P[N], S[2][N];
ULL inline get(int l, int r, int c){
return S[c][r] - S[c][l - 1] * P[r - l + 1];
}
int main(){
scanf("%d%d%d%s%s", &n, &m, &q, s + 1, t + 1);
P[0] = 1;
for(int i = 1; i <= n; i++){
P[i] = P[i - 1] * B;
S[0][i] = S[0][i - 1] * B + s[i];
S[1][i] = S[1][i - 1] * B + t[i];
}
while(q--){
int l, r, res = 0; scanf("%d%d", &l, &r);
for(int i = l; i <= r - m + 1; i++)
if(get(i, i + m - 1, 0) == get(1, m, 1))res++;
printf("%d\n", res);
}
return 0;
}

C - Vasya And The Mushrooms

硬核模拟 + 前缀和处理。要不重复的绕完 \(2 * N\) 的格子,从左上角出发,要么\(S\)形绕几次然后绕大圈,要么绕整个的一圈。

可以枚举绕\(S\)格子的次数,\(S\)的部分可以前缀和计算,系数是一样的,后面的部分计算比较复杂。

第一种情况,前面的部分绕了了几个完整的 \(U\)。要从上方出发。

求(\(now\)代表\(U\)字结束(包括)的列数):

上半部分:$\sum_{i=now + 1}^n a[0][i] * (i * 2) $

我们发现,数列后缀和的后缀和是:

\(sufx[j] = \sum_{i = j} ^ n a[i] * (i - j + 1) = a[j] * 1 + a[j + 1] * 2 + … + a[n] * (n - j + 1)\)

故,把\(sufx[now + 1]\)再加上\(\sum_{i = j} ^ n a[i] * (i * 2 - 1)\),即为答案,后面这部分可用后缀和\(O(1)\)计算。

下半部分:$\sum_{i=now + 1}^n a[1][i] * (n + (n - i + 1)) $

我们想要这样的东西:

\(a[i] * 3 + a[i + 1] * 2 + a[i + 2] * 1\)

这个看上去很像\(Hash\)表的原理,只不过\(b = 1\),可以用hash表的思想在\(O(1)\) 求出这个,然后在用后缀和加上系数既可。

对于第二种情况,只不过将上下颠倒,我们互换一下处理方式既可。

预处理前缀和、后缀和和枚举\(S\)的时间都为\(O(n)\),每次求解只需\(O(1)\),故总共复杂度为$ O(n) $的

#include <cstdio>
#include <iostream>
#include <cmath> using namespace std;
typedef long long LL;
const int N = 300010;
//s形的预处理
int n, a[2][N];
LL pre[2][N], s[2][N], suf[2][N], sum[N], prex[2][N], sufx[2][N], ans = -1; int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[0][i]);
for(int i = 1; i <= n; i++) scanf("%d", &a[1][i]);
int S = n >> 1, tot = 0;
for(int i = 1; i <= S * 2; i+=2){
s[0][i] = (LL)a[0][i] * (tot++); s[1][i] = (LL)a[1][i] * (tot++);
s[1][i + 1] = (LL)a[1][i + 1] * (tot++); s[0][i + 1] = (LL)a[0][i + 1] * (tot++);
} for(int i = 0; i < 2; i++)
for(int j = 1; j <= n; j++){
pre[i][j] = pre[i][j - 1] + a[i][j];
prex[i][j] = prex[i][j - 1] + pre[i][j];
} for(int j = 0; j < 2; j++)
for(int i = n; i >= 1; i--){
suf[j][i] = suf[j][i + 1] + a[j][i];
sufx[j][i] = sufx[j][i + 1] + suf[j][i];
} for(int i = 0; i <= n; i++){
LL tot = sum[i] = sum[i - 1] + s[0][i] + s[1][i];
//如果是奇数,则在下面出发
if(i % 2){
tot += (n + i - 1) * suf[0][i + 1] + (prex[0][n] - prex[0][i] - (n - i) * pre[0][i]);
tot += (i * 2 - 1) * suf[1][i + 1] + (sufx[1][i + 1]);
}else{
//从上面出发
tot += (i * 2 - 1) * suf[0][i + 1] + (sufx[0][i + 1]);
tot += (n + i - 1) * suf[1][i + 1] + (prex[1][n] - prex[1][i] - (n - i) * pre[1][i]);
}
ans = max(ans, tot);
} printf("%lld", ans);
return 0;
}

D - Vasya And The Matrix

参考题解。 存在性参考异或的性质,\(x\) \(xor\) $ x = 0$ 。

考虑构造一个合理的序列,只需将除了最后一行,最后一列的所有数添上\(0\)。

除右下角外其他数照搬数据,右下角用异或尝试既可。

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 110;
int n, m, a[N], b[N], ans = 0;
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", a + i), ans ^= a[i];
for(int i = 1; i <= m; i++) scanf("%d", b + i), ans ^= b[i]; if(ans != 0)puts("NO");
else{
puts("YES");
ans = b[m];
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++)
printf("0 ");
printf("%d\n", a[i]);
ans ^= a[i];
}
b[m] = ans;
for(int i = 1; i <= m; i++) printf("%d ", b[i]);
}
return 0;
}

Codeforces Edu Round 48 A-D的更多相关文章

  1. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  6. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  7. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

随机推荐

  1. MySQL死锁问题(转)

    线上某服务时不时报出如下异常(大约一天二十多次):"Deadlock found when trying to get lock;". Oh, My God! 是死锁问题.尽管报错 ...

  2. mysql参数总结

    1.innodb_old_blocks_pct 确定modpoint位置,默认37,(3/8=37%)可以通过这个调整young与old比. innodb_old_blocks_time:当有大的查询 ...

  3. 如何统计Ceph的RBD真实使用容量

    前言 ceph的rbd一直有个问题就是无法清楚的知道这个分配的空间里面到底使用了多少,这个在Jewel里面提供了一个新的接口去查询,对于老版本来说可能同样有这个需求,本篇将详细介绍如何解决这个问题 查 ...

  4. 2020年SpringCloud 必知的18道面试题

    今天跟大家分享下SpringCloud常见面试题的知识. 1.什么是Spring Cloud? Spring cloud流应用程序启动器是基于Spring Boot的Spring集成应用程序,提供与外 ...

  5. [LeetCode题解]21. 合并两个有序链表 | 递归

    解题思路 使用递归实现: 定义函数功能:合并两个有序链表,并返回链表的头 结束条件:两个链表其中一个为空,返回另一个链表 递推公式: l1.val < l2.val:l1.next = Merg ...

  6. 支付宝电脑网站支付 alipay.trade.page.pay

    只涉及支付接口 其他接口没有使用 支付宝官方文档:https://docs.open.alipay.com/270/105899/ 支付接口文档 https://docs.open.alipay.co ...

  7. ArrayList和LinkedList 的联系和区别

    ArrayList和LinkedList 的联系和区别 1.联系: 都实现了List接口 有序 不唯一(可重复) 2.区别 ArrayList LinkedList

  8. 在CorelDRAW中为对象添加块阴影效果

    我们可以使用CorelDRAW来绘制矢量图形,在勾画出简单的图形后,往往还需要对它们进行一些或简单或复杂的处理,以增加一定的艺术效果.CDR中可供选择的效果有很多,作用的对象可以是文字,也可以是图案. ...

  9. Contest 982

    A 直接模拟即可,为了方便边界判断建议用 !=. 时间复杂度 \(O\left(n\right)\). B \(w\) 排序来处理内向者,坐人后丢进大根堆来处理外向者. 时间复杂度 \(O\left( ...

  10. 【操作系统】先来先服务和短作业优先算法(C语言实现)

    [操作系统] 先来先服务算法和短作业优先算法实现 介绍: 1.先来先服务 (FCFS: first come first service) 如果早就绪的进程排在就绪队列的前面,迟就绪的进程排在就绪队列 ...