Codeforces Edu Round 49 A-E
A. Palindromic Twist
由于必须改变。所以要使\(a[i] = a[n - i + 1]\)。
要么同向走,但必须满足之前的\(a[i] = a[n - i + 1]\)。
要么相遇,必须满足两字符相差\(2\)的距离。
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 110;
int n;
char str[N];
bool judge(){
for(int i = 1; i <= n / 2; i++)
if(abs(str[i] - str[n - i + 1]) != 2 && abs(str[i] - str[n - i + 1]) != 0) return false;
return true;
}
int main(){
int T; scanf("%d", &T);
while(T--){
scanf("%d%s", &n, str + 1);
if(judge()) puts("YES");
else puts("NO");
}
return 0;
}
B. Numbers on the Chessboard
找规律题,如果\(n\)是奇数,则每行有可能\((n - 1) / 2\) 或 \((n + 1) / 2\)个数字,可以分治考虑:
#include <iostream>
#include <cstdio>
#define int long long
using namespace std;
int n, q;
signed main(){
scanf("%lld%lld", &n, &q);
for(int i = 1; i <= q; i++){
int x, y; scanf("%lld%lld", &x, &y);
int num = 0;
if(n % 2){
if((x + y) % 2 == 0){
if(x % 2) num = (x - 1) * n / 2 + y / 2 + 1;
else num = (x - 2 + 1) * n / 2 + 1 + y / 2;
}else{
if(x % 2) num = n * n / 2 + 1 + (x - 1) * n / 2 + y / 2;
else num = n * n / 2 + (x - 2 + 1) * n / 2 + 1 + y / 2 + 1;
}
}else{
if((x + y) % 2 == 0){
if(x % 2) num = (x - 1) * (n / 2) + y / 2 + 1;
else num = (x - 1) * (n / 2) + y / 2;
}else{
if(x % 2) num = n * n / 2 + (x - 1) * (n / 2) + y / 2;
else num = n * n / 2 + (x - 1) * (n / 2) + y / 2 + 1;
}
}
printf("%lld\n", num);
}
return 0;
}
C. Minimum Value Rectangle
设矩形的两边长为\(a\)、\(b\),且\(a <= b\),则:
\(\frac{P ^ 2}{S} = \frac{(2(a + b)) ^ 2}{a * b}= \frac{4a ^ 2 + 4b ^ 2 + 8ab }{a * b} = 4(\frac{a}{b} + \frac{b}{a}) + 8\)
使\(\frac{a}{b} + \frac{b}{a}\) 最小,只需使\(\frac{b}{a}\)最小既可,因为\(\frac{b}{a} >= 1\),但\(\frac{a}{b} <= 1\),故后者对总和没有影响。
#include <iostream>
#include <cstdio>
#include <limits.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int SIZE = 10010;
int n, a, cnt[SIZE], d[SIZE << 1], tot, ansA, ansB;
double c, minn;
int main(){
int T; scanf("%d", &T);
while(T--){
memset(cnt, 0, sizeof cnt);
bool success = false; tot = 0;
minn = 10001;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a), cnt[a]++;
if(cnt[a] == 2) d[++tot] = a;
if(cnt[a] == 4) d[++tot] = a;
}
sort(d + 1, d + 1 + tot);
for(int i = 1; i < tot; i++){
if(d[i] == d[i + 1]){ ansA = d[i], ansB = d[i + 1]; break; }
c = (double)d[i + 1] / d[i];
if(c < minn)ansA = d[i], ansB = d[i + 1], minn = c;
}
printf("%d %d %d %d\n", ansA, ansA, ansB, ansB);
}
return 0;
}
D. Mouse Hunt
对于每个联通块,选择环中(包括子环)的一个位置设置捕鼠夹既可。
在符合条件的位置上取最小值的和即为答案。
若选择多个点,可证明其代价要大于选一个点。

若不选择绿点,选择1 + 20,但为了保证每个房间都能干掉老鼠,所以2000是必选的,所以选择联通快中多个点的花销 > 只选一个点。
#include <iostream>
#include <cstdio>
#include <vector>
#include <limits.h>
using namespace std;
const int N = 200010;
int n, c[N], a[N], ans;
bool st[N], vis[N];
vector<int> G[N], edge, val;
void dfs(int u){
if(vis[u]){
val.push_back(u);
while(edge.size() && edge.back() != u)
val.push_back(edge.back()), edge.pop_back();
return ;
}
vis[u] = true, edge.push_back(u), dfs(a[u]);
}
void mark(int u){
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(!st[v]) st[v] = true, mark(v);
}
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", c + i);
for(int i = 1; i <= n; i++) {
scanf("%d", a + i);
G[i].push_back(a[i]);
G[a[i]].push_back(i);
}
for(int i = 1 ; i <= n; i++){
if(!st[i]){
val.clear(); edge.clear();
dfs(i);
int res = INT_MAX;
for(int i = 0; i < val.size(); i++)
res = min(res, c[val[i]]);
ans += res;
st[i] = true, mark(i);
}
}
printf("%d", ans);
return 0;
}
E. Inverse Coloring
自闭,看了题解之后稍微理解了一点点。
设\(f[i][j][k]\) 为长度为\(i\), 最长连续填色数为\(k\), 尾部最长连续涂色数最长为\(j\)的方案数
可以想到,这个状态可以延展到的状态有:
- 当新结尾颜色保持跟之前一样,\(f[i + 1][j + 1][max(j + 1, k)]\)
- 不一样,\(f[i + 1][1][max(1, k)]\)
\(cnt[i]\) 实际上是长度为\(i\)的所有方案数,那么只要符合:
\(i * j < k\),\(cnt[i] * cnt[j]\)就能被加入答案
…...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 510, mod = 998244353;
typedef long long LL;
int n, k, f[2][N][N], cnt[N];
void update(int &a, int b){ a = (a + b) % mod; }
int main(){
scanf("%d%d", &n, &k);
f[0][0][0] = 1;
for(int i = 0; i < n; i++){
int pre = i & 1, now = pre ^ 1;
memset(f[now], 0, sizeof f[now]);
for(int j = 0; j <= n; j++){
for(int k = 0; k <= n; k++){
update(f[now][j + 1][max(j + 1, k)], f[pre][j][k]);
update(f[now][1][max(1, k)], f[pre][j][k]);
}
}
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
update(cnt[i], f[n & 1][j][i]);
}
}
LL ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i * j < k)
ans = (ans + (LL)cnt[i] * cnt[j]) % mod;
}
}
printf("%lld", (ans / 2) % mod);
return 0;
}
Codeforces Edu Round 49 A-E的更多相关文章
- Codeforces Beta Round #49 (Div. 2)
Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #46 (Div. 2)
Codeforces Beta Round #46 (Div. 2) http://codeforces.com/contest/49 A #include<bits/stdc++.h> ...
- CodeForce Round#49 untitled (Hdu 5339)
Untitled Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- 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 ...
随机推荐
- 转 Cache一致性和内存模型
卢本伟牛逼,写得很好 https://wudaijun.com/2019/04/cpu-cache-and-memory-model/ 本文主要谈谈CPU Cache的设计,内存屏障的原理和用法,最后 ...
- MVCC(转)
什么是 MVCC MVCC (Multiversion Concurrency Control) 中文全程叫多版本并发控制,是现代数据库(包括 MySQL.Oracle.PostgreSQL 等)引擎 ...
- rhel8/centos8网络网卡设置ping不通,连接不上,各种问题
[解决问题]: 1-ping不通宿主机 2-ping不通外网 3-ping不通网关 4-网络中心VMnet8 VMnet1 VMnet0 不见了 5-rhel8网络设置全攻略 环境:win10宿主机+ ...
- Proftp最简匿名访问配置
前言 每一次做ftp的配置都要弄半天,找文档,各种权限控制的坑,折腾半天,这次还是准备记录下来,以备不时之需,这里不配置什么高级的功能,就去实现一个最简单的配置 匿名用户的上传和下载 配置proftp ...
- Python_面试题汇总【正在整理中...】
1.十大算法 阶乘 冒泡 1 #使用递归实现阶乘 2 3 def f(n): 4 if n ==1: 5 return 1 6 else: 7 return n*(f(n-1)) 使用递归实现阶乘 1 ...
- 干货 MySQL常见的面试题 + 索引原理分析
常见的面试必备之MySQL索引底层原理分析: MySQL索引的本质 MySQL索引的底层原理 MySQL索引的实战经验 面试 1)问题:数据库中最常见的慢查询优化方式是什么? 回答:加索引 2)问题: ...
- html图片拖放
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- 关于Redis的一些思考
1.从Java语言考虑,已经有ConcurrentHashMap等并发集合类了,与Redis相比,区别于差异在哪? 一直有这么个疑问,今天有搜了很久,很巧,搜到个有同样想法的问答,如下: When p ...
- 新鲜出炉!阿里巴巴,京东,美团面经汇总,已拿offer!
最近在后台收到了很多小伙伴的私信,说自己最近想好好准备一下,在不久后的秋招跳个槽往大厂冲击一下,想要我给大家整理出一份大厂面试题好用来做好准备.之前公司有点事比较忙就没回私信.最近好一点了我也是立马联 ...
- 如何修改IDM下载器的临时文件夹位置
所有的应用程序在下载时,都会有一些默认的选项.比如产生的临时文件存放在C盘目录下,或者定期自动更新等设置.那么当我们的计算机上安装了很多程序之后,C盘的空间就会渐渐地变小了,从而有了空间不足等等情况, ...