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} $种不同的选择方案:

  1. 翻过去不同,那么对应过去就有\(A ^ {len} - 1\) 种方案(不包括翻过去相同那种),由于可能算上等价操作,他俩算一个,所以它的贡献为 \(\frac{A ^ {len} * (A ^ {len} - 1)}{2}\)
  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的更多相关文章

  1. Codeforces Beta Round #52 (Div. 2)

    Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...

  2. [CodeForces]Educational Round 52

    幸好我没有打这场,我VP的时候在C题就卡死了,我果然还是太菜了. A Vasya and Chocolate 题意:一个巧克力\(c\)元,买\(a\)赠\(b\),一共有\(n\)元,问能买几个巧克 ...

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

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

  4. CH Round #52 还教室[线段树 方差]

    还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...

  5. 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]中有多少个数 ...

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

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

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

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

  8. CodeForces Global Round 1

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

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

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

随机推荐

  1. tcp ESTABLISHED 接收数据

    tcp_rcv_established函数的工作原理是把数据包的处理分为2类:fast path和slow path,其含义显而易见.这样分类的目的当然是加快数据包的处理,因为在正常情况下,数据包是按 ...

  2. 使用GitHub API上传文件及GitHub做图床

    本文介绍GitHub API基础及上传文件到仓库API,并应用API将GitHub作为图床 GitHub API官方页面 GitHub API版本 当前版本为v3,官方推荐在请求头中显示添加版本标识. ...

  3. 如何开发一个maven插件

    maven是当下最流行的项目管理工具,其丰富的插件为我们的工作带来了很大的便利. 但是在一些情况下,开源的插件并不能完全满足我们的需求,我们需要自己创建插件,本文就从0开始带大家一起创建自己的插件. ...

  4. 一:NOSQL

    NOSQL =not only SQL 意即为不仅仅是SQL 传统的关系数据库在处理web2.0网站,特别是超大规模和高并发的社交网络服务类型的web2.0纯动态网站已经显得力不从心,出现了很多难以克 ...

  5. rinetd小工具

    一.介绍 tcp端口转发工具,针对ip:port的 连接进行转发.重点开源,且配置简单.不支持ftp,因为ftp要使用多个端口. 1.转发规则 [root@master tmp]# cat a.con ...

  6. 讲武德,你们要的高性能日志工具 Log4j2,来了

    Log4j 介绍过了,SLF4J 介绍过了,Logback 也介绍过了,你以为日志系列的文章就到此终结了? 不不不,我告诉你,还有一个 Log4j 2,顾名思义,它就是 Log4j 的升级版,就好像手 ...

  7. 转换文件格式遭遇 FileNotFoundError:[WinError 2]系统找不到指定的文件

    重启试试

  8. Guitar Pro的10个非常实用的技巧(下)

    Guitar Pro 7具有许多功能和编辑选项,只需点击几下即可随时创建与编辑我们的乐谱,.以下就为大家介绍10个Guitar Pro中实用的技巧,可以大大的节省我们的时间. 上次在<Guita ...

  9. macbook上安装虚拟机软件如何操作?

    很多用户都不太熟悉苹果系统,用惯了Windows之后再过渡到MacOS难免会有些不习惯.为了使我们又可以用回那些熟悉的Windows应用,比较常见的办法就是安装macbook虚拟机.下面小编就教大家一 ...

  10. 从Guarded Block来看Java中的wait和notify方法

    目录 预备知识 概览 线程同步 wait()方法 wait() wait(long timeout) wait(long timeout, int nanos) notify() & noti ...