Codeforces Round #533 (Div. 2)题解
orz olinr AK Codeforces Round #533 (Div. 2)
中文水平和英文水平都太渣..翻译不准确见谅
T1.给定n<=1000个整数,你需要钦定一个值t,使得每个整数到线段[t-1,t+1]距离和最小,求最小距离,每个数<=100
枚举t
#include <cstdio>
using namespace std;
int n, a[1010];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int minans = 0x3f3f3f3f, fuck = -233;
for (int ans = 1; ans <= 100; ans++)
{
int cost = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] > ans)
cost += a[i] - ans - 1;
else if (a[i] < ans)
cost += ans - a[i] - 1;
}
if (cost < minans)
{
minans = cost;
fuck = ans;
}
}
printf("%d %d\n", fuck, minans);
return 0;
}
T2.给定长度为n<=1e5的字符串s和一个数k<=1e5,要求求出一个最大的x,使得字符串中出现x个相同的不重叠的子串,每个子串内只含有一种字符
枚举每段连续区间,开桶记录个数 注意循环完毕字符最后还要算下答案(代码里直接多循环了一次处理)
#include <cstdio>
using namespace std;
int bucket[30];
char s[200010];
int n, k;
int main()
{
scanf("%d%d", &n, &k);
scanf("%s", s);
char lastch = 0;
int len = 0;
for (int i = 0; i == 0 || s[i - 1] != 0; i++)
{
if (s[i] == lastch)
len++;
else
{
if (lastch != 0)
bucket[lastch - 96] += len / k;
len = 1;
lastch = s[i];
}
}
int ans = 0;
for (int i = 1; i <= 26; i++)
if (ans < bucket[i])
ans = bucket[i];
printf("%d\n", ans);
return 0;
}
T3.求长度为n,数组里每个元素在[l,r]之间并且元素和%3=0的数组数量%1e9+7 n<=2e5,l<=r<=1e9
处理出[l,r]内%3=0,1,2的数字数量,然后无脑dp
#include <cstdio>
#include <iostream>
using namespace std;
int n, l, r, p = 1000000007;
int cnt[3];
long long f[200010][3];
int main()
{
scanf("%d%d%d", &n, &l, &r), l--;
cnt[0] = (r + 3) / 3 - (l + 3) / 3;
cnt[1] = (r + 2) / 3 - (l + 2) / 3;
cnt[2] = (r + 1) / 3 - (l + 1) / 3;
f[0][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int now = 0; now <= 2; now++)
{
for (int last = 0; last <= 2; last++)
{
int dis = (now - last) % 3;
if (dis < 0) dis += 3;
f[i][now] += f[i - 1][last] * cnt[dis] % p;
f[i][now] %= p;
}
}
}
cout << f[n][0] << endl;
return 0;
}
T4.N*M(N<=1000,M<=1000)的棋盘上Van一个游戏,一共p<=9个人,每个人有个速度si,每次每个人将他棋盘上所有棋子能走<=si步的所有位置都放上棋子,要求不能经过墙和别的玩家的棋子
bfs套bfs?第一层bfs维护每个玩家的等待扩展的棋子队列,第二层bfs在每个玩家扩展棋子时候维护,对第二层bfs扩展到距离为si的位置加入第一层bfs的队列即可(因为只有他们才能继续扩展)
代码略长
#include <bits/stdc++.h>
using namespace std;
int n, m, p, s[15];
int mp[1010][1010];
int dis[1010][1010];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main()
{
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++)
mp[i][0] = mp[i][m + 1] = -1;
for (int j = 1; j <= m; j++)
mp[0][j] = mp[n + 1][j] = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
char s;
scanf(" %c", &s);
if (s == '#')
mp[i][j] = -1;
if (s >= '1' && s <= '9')
mp[i][j] = s - 48;
}
}
queue<int> qx, qy;
for (int pl = 1; pl <= p; pl++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (mp[i][j] == pl)
{
qx.push(i), qy.push(j);
}
}
}
}
while (!qx.empty())
{
int x = qx.front(), y = qy.front();
qx.pop(), qy.pop();
int col = mp[x][y];
queue<int> qx1, qy1;
dis[x][y] = 0;
qx1.push(x), qy1.push(y);
while (!qx1.empty())
{
int x1 = qx1.front(), y1 = qy1.front();
qx1.pop(), qy1.pop();
for (int d = 0; d < 4; d++)
{
int nx = x1 + dx[d], ny = y1 + dy[d];
if (mp[nx][ny] != 0) continue;
mp[nx][ny] = col;
dis[nx][ny] = dis[x1][y1] + 1;
if (dis[nx][ny] == s[col])
{
qx.push(nx), qy.push(ny);
}
else
qx1.push(nx), qy1.push(ny);
}
}
}
int ans[13];
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (mp[i][j] > 0)
ans[mp[i][j]]++;
for (int i = 1; i <= p; i++)
printf("%d ", ans[i]);
return 0;
}
做题时候变量名写错了WA了一发。。。
T5.你有一个长度为n(n<=1e5)的事件序列,包含两种事件:1.你可以修改你的社交账户名(修改为你的m个朋友之一 m<=40)2.朋友s访问你的社交账户。如果一个朋友是高兴的,当且仅当每次他访问你时候你的社交用户名都是他的名字。最大化高兴的朋友的数量
将两个事件1之间的所有事件2定义为一个区间,那么这个区间内所有朋友是互斥的,即这些朋友最多有一个是高兴的,那么我们可以构建无向图,每个区间内所有朋友两两连边,最后求最大独立集,可以转化为最大团(其实他俩差不多)
由于m<=40,所以不能枚举集合,官方题解给出了meet in the middle做法,不过这种最大团都可以被随机序列怒艹。。。
#include <bits/stdc++.h>
using namespace std;
bool vis[50][50];
int n, tot;
map<string, int> id;
bool in[50];
int seq[50];
bool valid(int x)
{
for (int j = 1; j <= n; j++)
if (in[j] == true && vis[j][x] == true) return false;
return true;
}
int work()
{
int cnt = 0;
memset(in, 0, sizeof(in));
for (int i = 1; i <= n; i++)
if (valid(seq[i])) in[seq[i]] = true, cnt++;
return cnt;
}
void clean()
{
for (int i = 1; i <= n; i++)
if (in[i] == true)
for (int j = 1; j <= n; j++)
if (in[j] == true)
vis[i][j] = true;
memset(in, 0, sizeof(in));
}
int main()
{
int cnt;
srand(time(0));
scanf("%d%d", &cnt, &n);
for (int i = 1; i <= cnt; i++)
{
int opd;
scanf("%d", &opd);
if (opd == 1) clean();
else
{
string name;
cin >> name;
if (id.count(name) == false) id[name] = ++tot;
in[id[name]] = true;
}
}
clean();
for (int i = 1; i <= n; i++) seq[i] = i;
int ans = 0, t = 100000;
while (clock() / (double)CLOCKS_PER_SEC <= 1.95)
{
random_shuffle(seq + 1, seq + 1 + n);
ans = max(ans, work());
}
printf("%d\n", ans);
return 0;
}
学校太操蛋,9点55放学,10点10分睡觉,没时间打CF
这次终于有个时间合适的比赛了
第一次打CF,网太卡没hack,最后rating就涨了201,感觉一般般,还是因为自己太菜
前40分钟之内切掉了前4题,然后第五题题意没读懂,问出题人,出题人给回了句原题中的话,给一个词加了粗,然后懂了,懂了后转化出最大团的模型,就是不会做
忘了有个东西叫meet in the middle,也忘了有个东西叫随机化,这就非常cd了
由于学校太操蛋,以后在线打CF的机会肯定不多
打CF主要是练手速和一遍A,毕竟一遍不A扣50分也很恶心的。。。比如说这次B和D都没一遍A
Codeforces Round #533 (Div. 2)题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- 解决django不能以本机ip地址访问
在使用django框架来架设网站时,我们测试一般是通过django的开发服务器来完成,但是我们可以看到生成的地址是127.0.0.1:8000这样的话,我们在外网就无法访问了. 解决办法是通过传入第三 ...
- python window使用paramiko简单监控数据指标数据采集
#!/usr/bin/python #-*- coding: utf-8 -*- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- Delphi IOS (二)
1.Mac 中 simulator模拟器Home快捷键:command(Win键盘,Ctrl与Alt之间的键)+shift+h来代替,也可以点击菜单>HardWare>Home 2.iPh ...
- eclipse中的TODO和FIXME
最近使用eclipse开发代码时,公司要求按他们制定代码规范编写代码,其他都还好,因为基本都养成良好习惯了,但TODO和FIXME就有点陌生,查了一下资料,发现笔者寡闻了,果然学海无涯,好了,下边解释 ...
- spring配置c3p0连接池
- from xml.etree import cElementTree as ET
- java字符编码转换研究(转)
1. 概述 本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等. 在下面的描述中,将以"中文"两个字为例,经查表可以知道其GB2312编码是" ...
- vs调试时不抛出异常的解决方案
选项->调试->常规->启用“仅我的代码” 打勾
- NCBI下载SRA数据
从NCBI下载数据本来是一件很简单的事情,但是今天碰到几个坑: 1.paper里没有提供SRA数据号.也没有提供路径: 2.不知道文件在ftp的地址,不能直接用wget下载 所以通过在NCBI官网,直 ...
- Mat_类
Mat_类是对 Mat 类的一个包装,其定义如下: template<typename _Tp> class Mat_ : public Mat { public: //只定义了几 ...