Codeforces Edu Round 48 A-D
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的更多相关文章
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- 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]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
随机推荐
- IP 层收发报文简要剖析6--ip_forward 报文转发
//在函数ip_route_input_slow->ip_mkroute_input注册, /* * IP数据包的转发是由ip_forward()处理,该函数在ip_rcv_finish() * ...
- 224、Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- ubuntu12.10安装sun java jdk
add-apt-repository ppa:webupd8team/java apt-get update apt-get install oracle-java6-installer root@k ...
- Netty源码解析 -- 内存对齐类SizeClasses
在学习Netty内存池之前,我们先了解一下Netty的内存对齐类SizeClasses,它为Netty内存池中的内存块提供大小对齐,索引计算等服务方法. 源码分析基于Netty 4.1.52 Nett ...
- python-基础入门-2
这里介绍两个,相当于c中的scanf函数 第一个raw_input 1 age=raw_input("how old are you ") 2 print "you ar ...
- 图像分割必备知识点 | Dice损失 理论+代码
本文包含代码案例和讲解,建议收藏,也顺便点个赞吧.欢迎各路朋友爱好者加我的微信讨论问题:cyx645016617. 在很多关于医学图像分割的竞赛.论文和项目中,发现 Dice 系数(Dice coef ...
- 下载器Folx专业版有没有iTunes整合功能
对于使用Mac系统的用户来说,相信对iTunes都不陌生.Folx专业版提供的iTunes整合功能,能将下载的音频.电影等文件自动同步到iTunes. 该功能将会有助于用户的音频.视频整合,对于喜欢听 ...
- objetive-C中属性变量和成员变量
属性变量 @property和@synthesize可以自动生成某个类成员变量的存取方法. readwrite:这个属性是默认的情况,会自动生成存取器 assign:这个属性一般用来处理基础类型,比如 ...
- 一看就懂的:MySQL数据页以及页分裂机制
文章公号 首发!连载中~ 欢迎各位大佬关注, 回复:"抽奖" 还可参加抽活动 文末有二维码 一.知识回顾 回顾一下之前和大家分享的知识点 看了前面的文章,想必你肯定了解了什么是Bu ...
- JavaSE 学习笔记06丨并发
Chapter 12. 并发 12.1 并发与并行 并发:指两个或多个事件在同一个时间段内发生. 并行:指两个或多个事件在同一时刻发生(同时发生). 在操作系统中,并发指的是在一段时间内宏观上有多个程 ...