Codeforces Round #297 (Div. 2) 题解

A题:

有n个房间排成一条线,每个房间之间都有一个通道通往下个房间,则共有n-1个通道,现在你要从1到n,而且每个房间还放有一个钥匙,如果要开这个通道,通道的钥匙你有,则通过,否则你要买相应的钥匙。

大写代表这个通道,小写代表相应的钥匙。

问你最少需要买多少把钥匙。

样例:

input

3aAbB

output

0

input

4aBaCaB

output

3

input

5xYyXzZaZ

output

2

看样例应该就知道他要问什么了吧。

思路:直接扫一遍过去,看缺多少则买多少。

 1 #include<cstdio>
 2 #include<cstring>
 3 const int maxn=100000+10;
 4 char s[maxn*2];
 5 int key[30];
 6 int main()
 7 {
 8     int n;
 9     while(scanf("%d",&n)!=EOF){
10         n--;
11         scanf("%s",&s);
12         memset(key,0,sizeof(key));
13         int ans=0;
14         for(int i=1;i<n*2;i++){
15             key[s[i-1]-‘a‘]++;
16             i++;
17             s[i-1]=s[i-1]+32;
18             if(key[s[i-1]-‘a‘]>0)
19                 key[s[i-1]-‘a‘]--;
20             else
21                 ans++;
22         }
23         printf("%d\n",ans);
24     }
25     return 0;
26 }

B题:

给出一个字符串,长度为s,然后给出m个操作,每一个操作,给出一个数a,然后对字符串(1到s)的第a个到第s-a+1这一段子序列进行反转,问这m个操作后,最后字符串是什么。数据保证a*2<=s。

样例:

abcdef12

output

aedcbf

input

vwxyz22 2

output

vwxyz

input

abcdef31 2 3

output

fbdcea

对每一个操作都进行,一定超时。对第i个位置,翻转偶次数,等于没有翻转。a[i]表示从第i个位置开始的字符串翻转的次数。然后初始化后开始记录,然后扫一遍a[i],确定字符串的第i个位置是否需要翻转。最后输出字符串。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=200000+10;
 6 char s[maxn];
 7 int a[maxn];
 8 int main()
 9 {
10     while(scanf("%s",&s)!=EOF){
11         int len=strlen(s);
12         memset(a,0,sizeof(a));
13         int m;
14         scanf("%d",&m);
15         int u;
16         for(int i=1;i<=m;i++){
17             scanf("%d",&u);
18             a[u]++;
19             a[len-u+1]++;
20         }
21         for(int i=1;i<=len/2;i++){
22             a[i]=a[i]%2;
23             if(a[i])
24                 a[i+1]++;
25         }
26         for(int i=len/2+1;i<=len;i++)
27             a[i]=a[len-i+1];
28         for(int i=1;i<=len;i++){
29             if(a[i])
30                 printf("%c",s[len-i]);
31             else
32                 printf("%c",s[i-1]);
33         }
34         printf("\n");
35     }
36     return 0;
37 }

比赛时只做了2道题,C,D,E会再补上来。
时间: 2024-08-09 12:40:16

Codeforces Round #297 (Div. 2) 题解的相关文章

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 1

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

题目传送门 1 /* 2 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 3 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 13 co

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem

模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 const int MAXN = 2e5 + 10; 13

Codeforces Round #534 (Div. 2)题解

Codeforces Round #534 (Div. 2)题解 A. Splitting into digits 题目大意 将一个数字分成几部分,几部分求和既是原数,问如何分可以使得分出来的各个数之间的差值尽可能小 解题思路 将n分成n个1相加即可 AC代码 #include<cstring> #include<string> #include<iostream> #include<cstdio> using namespace std; int main

Codeforces Round #561 (Div. 2) 题解

Codeforces Round #561 (Div. 2) 题解 题目链接 A. Silent Classroom 水题. Code #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; int n; char s[N], t[N]; int main() { cin >> n; for(int i = 1; i <= n; i++) { scanf(&q