[CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes
Description
You are given a string \(s\). Each character is either 0 or 1.
You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.
You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?
Input
The first line contains one integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.
Then \(t\) lines follow, each representing a test case. Each line contains one string \(s\) (\(1 \le |s| \le 100\)); each character of \(s\) is either 0 or 1.
Output
Print \(t\) integers, where the \(i\)-th integer is the answer to the \(i\)-th testcase (the minimum number of 0's that you have to erase from \(s\)).
Sample Input
3
010011
0
1111000
Sample Output
2
0
0
思路
签到,首尾1
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5+7;
char s[150];
int main() {
int _;
cin>>_;
while(_--){
cin>>s+1;
int n=strlen(s+1);
int l=-1,r=-1;
for(int i=1;i<=n;++i){
if(s[i]=='1'){
l=i;
break;
}
}
for(int i=n;i>=1;--i){
if(s[i]=='1'){
r=i;
break;
}
}
if(l==-1){
puts("0");
continue;
}
int ans=0;
for(int i=l;i<=r;++i){
if(s[i]=='0')ans++;
}
printf("%d\n",ans);
}
return 0;
}
B. National Project
Description
Your company was appointed to lay new asphalt on the highway of length \(n\). You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.
Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are \(g\) days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next \(b\) days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again \(g\) good days, \(b\) bad days and so on.
You can be sure that you start repairing at the start of a good season, in other words, days \(1, 2, \dots, g\) are good.
You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the \(n = 5\) then at least \(3\) units of the highway should have high quality; if \(n = 4\) then at least \(2\) units should have high quality.
What is the minimum number of days is needed to finish the repair of the whole highway?
Input
The first line contains a single integer \(T\) (\(1 \le T \le 10^4\)) — the number of test cases.
Next \(T\) lines contain test cases — one per line. Each line contains three integers \(n\), \(g\) and \(b\) (\(1 \le n, g, b \le 10^9\)) — the length of the highway and the number of good and bad days respectively.
Output
Print \(T\) integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
Sample Input
3
5 1 1
8 10 10
1000000 1 1000000
Sample Output
5
8
499999500000
思路
要注意一定要修\(n\)天,我的做法是先判断前\(n\)有没有到一半,然后如果不到一半,那就只需要判断剩下需要多少轮好天气的.总之就是一堆判断.
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
int _;
cin>>_;
while(_--) {
ll n,a,b;
cin>>n>>a>>b;
ll k=a+b;
ll tmp=n/k*a+min(a,n%k);//前n个有多少好的
if(tmp*2>=n) printf("%lld\n",n);
else{
ll ans=n/k*k;
tmp=(n+1)/2-n/k*a;//差
ll p=tmp/a;
if(tmp%a) ans+=p*k+tmp%a;
else ans+=(p-1)*k+a;
printf("%lld\n",ans);
}
}
return 0;
}
其实也有简单做法:
cout << max(n, (n - 1) / 2 / g * (g + b) + (n - 1) / 2 % g + 1) << '\n';
这个其实挺像打怪兽那个题.也是判断最后一块有多少的
可以断言除了等于\(n\)的情况最后一块一定是以g结尾的.
总结一下设总数为\(N\),每轮为\(k\),则需要\((N-1)/k+1\)轮,最后一块是$(N-1) mod (k) +1 $.
C. Perfect Keyboard
Description
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all \(26\) lowercase Latin letters will be arranged in some order.
Polycarp uses the same password \(s\) on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in \(s\), they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in \(s\), so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer \(T\) (\(1 \le T \le 1000\)) — the number of test cases.
Then \(T\) lines follow, each containing one string \(s\) (\(1 \le |s| \le 200\)) representing the test case. \(s\) consists of lowercase Latin letters only. There are no two adjacent equal characters in \(s\).
Output
For each test case, do the following:
if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of \(26\) lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Sample Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Sample Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
思路
一个大模拟,符合的是无环,并且每个点度数最多为2
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5+7;
char s[250];
vector<int>G[30];
bool vis[30][30];
bool us[30],uss[30];
int cnt,ans,sum;
void dfs(int u){
us[u]=cnt;
ans++;
sum+=G[u].size();
for(int v:G[u]){
if(!us[v]){
dfs(v);
}
}
}
void print(int u,int fa){
uss[u]=1;
printf("%c",u+'a');
for(int v:G[u]){
if(v!=fa){
print(v,u);
}
}
}
int main() {
int _;
cin>>_;
while(_--){
cin>>s;
cnt=0;
for(int i=0;i<26;++i) G[i].clear(),us[i]=uss[i]=0;
memset(vis,0,sizeof(vis));
for(int i=1;s[i];++i){
if(!vis[s[i]-'a'][s[i-1]-'a']){
G[s[i]-'a'].push_back(s[i-1]-'a');
G[s[i-1]-'a'].push_back(s[i]-'a');
vis[s[i]-'a'][s[i-1]-'a']=1;
vis[s[i-1]-'a'][s[i]-'a']=1;
}
}
bool flag=0;
for(int i=0;i<26;++i){
if(G[i].size()>2){
flag=1;
break;
}
if(!us[i]){
cnt++;
ans=0;
sum=0;
dfs(i);
if(sum/2!=ans-1){
flag=1;
break;
}
}
}
if(flag==0){
puts("YES");
for(int i=0;i<26;++i){
if(!uss[i]&&G[i].size()==1){
print(i,i);
}
}
for(int i=0;i<26;++i){
if(!uss[i]){
printf("%c",i+'a');
}
}
printf("\n");
}else{
puts("NO");
}
}
return 0;
}
D. Fill The Bag
Description
You have a bag of size \(n\). Also you have \(m\) boxes. The size of \(i\)-th box is \(a_i\), where each \(a_i\) is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if \(n = 10\) and \(a = [1, 1, 32]\) then you have to divide the box of size \(32\) into two parts of size \(16\), and then divide the box of size \(16\). So you can fill the bag with boxes of size \(1\), \(1\) and \(8\).
Calculate the minimum number of divisions required to fill the bag of size \(n\).
Input
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.
The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^{18}, 1 \le m \le 10^5\)) — the size of bag and the number of boxes, respectively.
The second line of each test case contains \(m\) integers \(a_1, a_2, \dots , a_m\) (\(1 \le a_i \le 10^9\)) — the sizes of boxes. It is guaranteed that each \(a_i\) is a power of two.
It is also guaranteed that sum of all \(m\) over all test cases does not exceed \(10^5\).
Output
For each test case print one integer — the minimum number of divisions required to fill the bag of size \(n\) (or \(-1\), if it is impossible).
Sample Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Sample Output
2
-1
0
思路
我这个是照着代码最短的那个代码写的,如果按照题意模拟二进制可能更容易理解一点.
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
int _,n;
ll m;
cin>>_;
while(_--) {
priority_queue<int>q;
cin>>m>>n;
ll ans=0,sum=0;
for(int i=1,s;i<=n;++i){
cin>>s;q.push(s);
ans+=s;
}
if(ans<m){puts("-1");continue;}
while(m&&!q.empty()){
int x=q.top();
q.pop();
if(x<=m) m-=x,ans-=x;
else if(ans-x<m) sum++,q.push(x>>1),q.push(x>>1);
else ans-=x;
}
cout<<sum<<'\n';
}
return 0;
}
E. Erase Subsequences
Description
You are given a string \(s\). You can build new string \(p\) from \(s\) using the following operation no more than two times:
choose any subsequence \(s_{i_1}, s_{i_2}, \dots, s_{i_k}\) where \(1 \le i_1 < i_2 < \dots < i_k \le |s|\); erase the chosen subsequence from \(s\) (\(s\) can become empty); concatenate chosen subsequence to the right of the string \(p\) (in other words, \(p = p + s_{i_1}s_{i_2}\dots s_{i_k}\)). Of course, initially the string \(p\) is empty.
For example, let \(s = \text{ababcd}\). At first, let's choose subsequence \(s_1 s_4 s_5 = \text{abc}\) — we will get \(s = \text{bad}\) and \(p = \text{abc}\). At second, let's choose \(s_1 s_2 = \text{ba}\) — we will get \(s = \text{d}\) and \(p = \text{abcba}\). So we can build \(\text{abcba}\) from \(\text{ababcd}\).
Can you build a given string \(t\) using the algorithm above?
Input
The first line contains the single integer \(T\) (\(1 \le T \le 100\)) — the number of test cases.
Next \(2T\) lines contain test cases — two per test case. The first line contains string \(s\) consisting of lowercase Latin letters (\(1 \le |s| \le 400\)) — the initial string.
The second line contains string \(t\) consisting of lowercase Latin letters (\(1 \le |t| \le |s|\)) — the string you'd like to build.
It's guaranteed that the total length of strings \(s\) doesn't exceed \(400\).
Output
Print \(T\) answers — one per test case. Print YES (case insensitive) if it's possible to build \(t\) and NO (case insensitive) otherwise.
Sample Input
4
ababcd
abcba
a
b
defi
fed
xyz
x
Sample Output
YES
NO
NO
YES
思路
有点像最长公共子序列那种的dp.这个只是判断是否,最初如果认为是三维的话:\((bool)dp[i][j][k]\) 可以看做s串遍历到i,t1串遍历到j时,与t2遍历到k是否匹配,但是这要枚举t1和t2分开的位置。
这样就变成\(n^4\)了,所以要优化,把这种bool优化成两维,s串遍历到i,t1串遍历到j时,与t2匹配的最长长度为k。
转移方程就判断各种情况,看看他会从什么地方转移过来
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
/*[i][j]=k s串遍历到i,t1串遍历到j时,与t2匹配的最长长度为k*/
int dp[450][450],n,m;
char s[450],t[450];
bool solve(int p){
for(int i=0;i<=n;++i) for(int j=0;j<=p;++j) dp[i][j]=-1;
dp[0][0]=0;
for(int i=1;i<=n;++i){
for(int j=0;j<=p;++j){
if(j>0&&s[i]==t[j]) dp[i][j]=max(dp[i-1][j-1],dp[i][j]);
if(dp[i-1][j]>=0&&s[i]==t[p+dp[i-1][j]+1]) dp[i][j]=max(dp[i-1][j]+1,dp[i][j]);
dp[i][j]=max(dp[i-1][j],dp[i][j]);
}
}
return dp[n][p]==m-p;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int _;
scanf("%d",&_);
while(_--){
scanf("%s%s",s+1,t+1);
n=strlen(s+1),m=strlen(t+1);
bool flag=0;
for(int i=1;i<=m;++i){
if(solve(i)) {
flag=1;
break;
}
}
puts(flag?"YES":"NO");
}
return 0;
}
[CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)的更多相关文章
- Educational Codeforces Round 82 (Rated for Div. 2)
题外话 开始没看懂D题意跳了,发现F题难写又跳回来了.. 语文好差,码力好差 A 判第一个\(1\)跟最后一个\(1\)中\(0\)的个数即可 B 乘乘除除就完事了 C 用并查集判一下联通,每个联通块 ...
- Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- Educational Codeforces Round 82 (Rated for Div. 2)E(DP,序列自动机)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],t[]; int n,m; ][]; ...
- Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)
从低位到高位枚举,当前位没有就去高位找到有的将其一步步拆分,当前位多余的合并到更高一位 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h&g ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
随机推荐
- Linus Torvalds正式宣布Linux Kernel 5.1RC2 发布,相当正常
导读 Linus Torvalds刚刚发布了Linux Kernel 5.2-rc2,这是继上周关闭合并窗口和随后的RC1之后的第一个内核测试版本. 在本周合并后的窗口活动中,Linus评论道,“嘿, ...
- 阿里云香港服务器IIS发布网站不成功解决方法
刚刚弄好了一个阿里云上服务器,费老劲儿了.我买了一个香港的服务器,最低配置,专有网络,买着玩的,一个.win的域名,省的国内备案了. 遇到的问题是怎么也访问不了我IIS上发布的网站,我把我解决方法说下 ...
- 3分钟教你用python制作一个简单词云
首先需要安装三个包: # 安装:pip install matplotlib # 安装:pip install jieba # 安装pip install wordcloud 1.制作英文字母的词云 ...
- 最简单的前端获取后台的json值(后台怎么返回一个json对象到前台)
(说一下这个外部包jackson一般不用了,现在大家都用马云儿子的FastJson 下面服务器代码我就不改了大家随意用什么外部包)2019.1.14日改 我使用了外部包jackson(杰克逊哈哈哈啊) ...
- PHP购物网站
我使用的phpsteam经常用着用着就闪退,所以做起来挺麻烦的.里面的代码有抄袭借鉴网上的代码,就是那个php做购物网站点击量最高的那个. 但是我很多代码也是自己写的不和其相同. PHP是一门选修课, ...
- HDU - 5695 Gym Class (优先队列+拓扑排序)
题意:有N个人,每个人的ID为1~N,部分同学A不希望部分同学B排在他之前,排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.在满足这个前提的情况下,将N个人排 ...
- PHP的操作符与控制结构
一.操作符 操作符是用来对数组和变量进行某种操作运算的符号. 算术操作符 操作符 名称 示例 + 加 $a+$b - 减 $a-$b * 乘 $a*$b / 除 $a/$b % 取余 $a%$b 复 ...
- JVM探秘:jinfo查看JVM运行时参数
本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 如何查看JVM运行时参数,对于线上JVM调优是很关键的,因为只有知道了当前使用的JVM ...
- 二、在SAP中创建一个程序
一.我们来到SE38 二.添加一个程序的名字,需要以Y或者Z开头,点击创建就可以了 三.我们输入hello Sap,然后选择可执行程序,然后保存 四.创建对象目录时,可以选择把这个加入到包中,或者选择 ...
- 三十、CI框架之使用cookies实现用户登录和退出。
一.在控制器中,写入3个函数.一个是login用于登录,一个是logout用于退出,一个show用来展示界面. login代码如下: logout和showuser函数如下: 二.我们的登录界面 三. ...