Codeforces Edu Round 52 A-E
A. Vasya and Chocolate
模拟题。数据会爆\(int\),要开\(long\) \(long\)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
int main(){
int T; scanf("%d", &T);
while(T--){
LL s, a, b, c;
scanf("%lld%lld%lld%lld", &s, &a, &b, &c);
LL buy = s / c, free = buy / a * b;
printf("%lld\n", buy + free);
}
return 0;
}
B. Vasya and Isolated Vertices
考虑最小时,两两连边,答案为\(max(n - 2 * m, 0)\)
考虑最大时,除了\(m\)为\(1或0\)特判以外,每次尝试用最多的边拓展一个点为不孤立的,则可以放\(now - 1\)条边连向之前所有的边。
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
LL n, m;
int main(){
cin >> n >> m;
cout << max(n - 2 * m, 0ll) << " ";
if(m == 0) printf("%lld\n", n);
else if(m == 1) printf("%lld\n", n - 2);
else{
int now = 2;
while(m - now + 1 > 0 && now < n) m -= (now - 1), now++;
printf("%lld\n", n - now);
}
return 0;
}
C. Make It Equal
我太弱了,只想到了\(O(nlogn)\)的做法...就是用树状数组维护前缀和,就能用\(O(logn)\)的时间计算出代价,然后弄一个指针从大往小搜就可以了...
#include <cstdio>
#include <iostream>
#include <cmath>
#include <limits.h>
typedef long long LL;
using namespace std;
const int N = 200010;
int n, k, a[N], cnt[N], ans = 0, maxn = -1, minn = INT_MAX;
LL c[N];
void add(int x, LL k){
for(; x <= maxn; x += x & -x) c[x] += k;
}
LL ask(int x){
LL res = 0;
for(; x; x -= x & -x) res += c[x];
return res;
}
LL inline get(int x){
return ask(maxn) - ask(x - 1);
}
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d", a + i), cnt[a[i]]++, maxn = max(maxn, a[i]), minn = min(minn, a[i]);
for(int i = maxn; i >= 1; i--)
add(i, (LL)cnt[i] * i);
for(int i = maxn; i >= 1; i--)
cnt[i] += cnt[i + 1];
int i = maxn - 1;
while(i >= minn){
ans++;
while(i - 1 >= minn && get(i) - (LL)(i - 1) * cnt[i] <= k) i--;
add(i, (LL)i * (cnt[i + 1]) - get(i + 1));
i--;
}
printf("%d\n", ans);
return 0;
}
其实可以在指针从上往下跳的过程中处理后缀和,所以可以用\(O(n)\)的时间解决辣:
#include <cstdio>
#include <iostream>
#include <cmath>
#include <limits.h>
typedef long long LL;
using namespace std;
const int N = 200010;
int n, k, a[N], cnt[N], po[N], ans = 0, maxn = -1, minn = INT_MAX;
LL c[N];
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d", a + i), po[a[i]]++, cnt[a[i]]++, maxn = max(maxn, a[i]), minn = min(minn, a[i]);
for(int i = maxn; i >= 1; i--)
cnt[i] += cnt[i + 1];
int i = maxn - 1;
c[maxn] = (LL)maxn * po[maxn];
while(i >= minn){
ans++;
c[i] = c[i + 1] + (LL)i * po[i];
while(i - 1 >= minn && c[i] - (LL)(i - 1) * cnt[i] <= k)
i--, c[i] = c[i + 1] + (LL)i * po[i];;
c[i] += (LL)i * (cnt[i + 1]) - c[i + 1];
i--;
}
printf("%d\n", ans);
return 0;
}
D. Three Pieces
\(pair<int, int>\)自带比较函数,所以省了不少功夫。写了一个优先队列\(bfs\)就过惹...
一共会有$3 *N ^ 4 $个状态,每次状态扩展最多要扩展\(4 * N\)级别,其实还会少。
总共复杂度为\(O(12 * N ^ 5)\),不会\(TLE\)。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 15;
int n, a[N][N];
PII num[N * N];
//0: 车、1: 马: 2、象
int dx[3][8] = {
{1, -1, 0, 0},
{1, 1, -1, -1, -2, -2, 2, 2},
{1, 1, -1, -1},
};
int dy[3][8] = {
{0, 0, 1, -1},
{2, -2, 2, -2, 1, -1, 1, -1},
{1, -1, 1, -1},
};
int size[3] = {4, 8, 4};
int ne[3] = {N, 1, N};
PII step[N][N][N * N][3];
struct Node{
int x, y, k, m, t, c;
};
bool operator < (const Node &x, const Node &y){
return x.t == y.t ? x.c > y.c : x.t > y.t;
}
bool inline check(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= n;
}
PII bfs(){
priority_queue<Node> q;
for(int i = 0; i < 3; i++){
q.push((Node){ num[1].first, num[1].second, 1, i, 0, 0});
step[num[1].first][num[1].second][0][i] = make_pair(0, 0);
}
while(!q.empty()){
Node u = q.top(); q.pop();
if(u.k >= n * n){
return make_pair(u.t, u.c);
}
for(int i = 0; i < size[u.m]; i++){
for(int j = 1; j <= ne[u.m]; j++){
int nx = u.x + dx[u.m][i] * j, ny = u.y + dy[u.m][i] * j;
int nm = u.m, nt = u.t + 1, nc = u.c;
int nk = (num[u.k + 1] == make_pair(nx, ny)) ? u.k + 1 : u.k;
PII now = make_pair(nt, nc);
if(!check(nx, ny)) break;
if(now < step[nx][ny][nk][nm]){
step[nx][ny][nk][nm] = now;
q.push((Node){nx, ny, nk, nm, nt, nc});
}
}
}
for(int i = 0; i < 3; i++){
if(i == u.m) continue;
int nx = u.x, ny = u.y;
int nm = i, nt = u.t + 1, nc = u.c + 1;
int nk = u.k;
PII now = make_pair(nt, nc);
if(!check(nx, ny)) break;
if(now < step[nx][ny][nk][nm]){
step[nx][ny][nk][nm] = now;
q.push((Node){nx, ny, nk, nm, nt, nc});
}
}
}
return make_pair(-1, -1);
}
int main(){
memset(step, 0x3f, sizeof step);
scanf("%d", &n);
ne[0] = ne[2] = n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &a[i][j]), num[a[i][j]] = make_pair(i, j);
PII res = bfs();
printf("%d %d\n", res.first, res.second);
return 0;
}
E. Side Transmutations
组合数问题,我肯定是不会的...
设\(A\)为字符集合的长度。
对于每一段 $ [b_{i - 1} + 1,b_i] $ $ (1 <= i <= m)$
设这一段的长度\(len = b[i] - (b[i - 1] + 1) + 1 = b[i] - b[i - 1]\)
它有$A ^ {len} $种不同的选择方案:
- 翻过去不同,那么对应过去就有\(A ^ {len} - 1\) 种方案(不包括翻过去相同那种),由于可能算上等价操作,他俩算一个,所以它的贡献为 \(\frac{A ^ {len} * (A ^ {len} - 1)}{2}\)
- 翻过去相同,每种方案对应过去式唯一的,所以为\(A ^ {len}\)。
这两种方案相加再\(*\)入\(ans\)中即可。
对于\([b_m + 1, n - b_m]\)这段,选不选都不会造成重复,对答案的贡献是:
\(A ^ {n - b_m - (b _m + 1) + 1} = A ^ {n - 2 * b_m }\)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MOD = 998244353;
const int N = 200010;
int n, m, A, b[N];
int power(int a, int b){
int res = 1;
while(b){
if(b & 1) res = (LL)res * a % MOD;
a = (LL)a * a % MOD;
b >>= 1;
}
return res;
}
int main(){
cin >> n >> m >> A;
for(int i = 1; i <= m; i++) scanf("%d", b + i);
LL ans = 1;
for(int i = 1; i <= m; i++){
LL now = power(A, b[i] - b[i - 1]);
ans = (ans * ((now + now * (now - 1) / 2) % MOD)) % MOD;
}
ans = (ans * power(A, (n - 2 * b[m]))) % MOD;
cout << ans;
return 0;
}
Codeforces Edu Round 52 A-E的更多相关文章
- Codeforces Beta Round #52 (Div. 2)
Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...
- [CodeForces]Educational Round 52
幸好我没有打这场,我VP的时候在C题就卡死了,我果然还是太菜了. A Vasya and Chocolate 题意:一个巧克力\(c\)元,买\(a\)赠\(b\),一共有\(n\)元,问能买几个巧克 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- CH Round #52 还教室[线段树 方差]
还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...
- 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 ...
随机推荐
- PF_PACKET&&tcpdump
linux下抓包原理 linux下的抓包是通过注册一种虚拟的底层网络协议来完成对网络设备消息的处理权.当网卡接收到一个网络报文之后,它会遍历系统中所有已经注册的网络协议,当抓包模块把自己伪装成一个网络 ...
- 《.NET 5.0 背锅案》第5集-案情大转弯:都是我们的错,让 .NET 5.0 背锅
第1集:验证 .NET 5.0 正式版 docker 镜像问题 第2集:码中的小窟窿,背后的大坑,发现重要嫌犯 EnyimMemcachedCore 第3集-剧情反转:EnyimMemcachedCo ...
- 解密Cookie,这一篇就够了
一.Cookie介绍 因为HTTP协议是无状态的,每次请求都是独立的,服务器端无法判断两次请求是否来自同一个用户,进而也就无法判断用户的登录状态,也不知道用户上一次做了什么.所以Cookie就是用来绕 ...
- Ceph部署mon出现0.0.0.0地址
前言 最近在群里两次看到出现mon地址不对的问题,都是显示0.0.0.0:0地址,如下所示: [root@lab8106 ceph]# ceph -s cluster 3137d009-e41e-41 ...
- appium 数据参数化 登录模块
下面是我最近学习的PYTHON的登录代码: class test(object): def getdic(self): d = {'username': '13', 'password': '1111 ...
- 10大排序算法——Java实现
算法与实现 选择排序 算法思想 从数组中选择最小元素,将它与数组的第一个元素交换位置.再从数组剩下的元素中选择出最小的元素,将它与数组的第二个元素交换位置.不断进行这样的操作,直到将整个数组排序. 动 ...
- WIN10—更改电脑桌面路径
电脑默认的桌面路径一般都在C盘,而我们又特别喜欢把文件都放在桌面,因为桌面既方便又好找.可时间久了,桌面文件会越来越多,C盘空间会越来越小,会拖慢系统速度.怎么把系统桌面路径设置在非C盘呢?本期教程将 ...
- C/C++编程日记:逻辑井字棋(圈叉)(用空格初始化)
问题描述: 3*3的棋盘中,只要一条线上出现三个一样的棋子就获胜(玩家或电脑):如果棋盘已经放满还未出现三个棋子一条线则打成平手. 具体细节: 初始化棋盘(用空格初始化) //初始化棋盘 vo ...
- MySQL第01课- CentOS + 单实例MySql编译安装总结
2016年2月,从oracle转向MySql ,碰上几个坑,特此记录 总结 1.注意环境变量.配置文件,操作过程不能出错 2.相比rpm方式安装,编译安装方式可以指定安装路径,再说安装是简单活,将来安 ...
- 深度分析:Java虚拟机类加载机制、过程与类加载器
虚拟机类加载机制是把描述类的数据从 Class 文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的 Java 类型. 需要注意的是 Java 语言与其他编译时需要进 ...