Codeforces Round #297 (Div. 2)(模拟+字符串+排序)

A.

题目链接:点击打开链接

解题思路:

大意就是说奇数位给小写字母,偶数位给大写字母,然后小写对应钥匙,大写对应门,问最少消耗几把钥匙能打开所有门。

简单模拟即可,初始化一个英文字母数组,如果遇到小写字母,我们把相应的计数器++,遇到大写,如果它对应的数组值不为0,那么我们将其--,

否则购买一把钥匙。

完整代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <climits>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 500001;
int n;
string s;
int vis[26];

int main()
{

    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif
    while(cin >> n)
    {
        memset(vis , 0 , sizeof(vis));
        cin >> s;
        int len = s.length();
        int cnt = 0;
        for(int i = 0 ; i < len ; i ++)
        {
            if((i+1) % 2 != 0)
            {
                vis[s[i] - 'a'] ++;
            }
            else
            {
                if(vis[s[i] - 'A'])
                {
                    vis[s[i] - 'A'] --;
                }
                else
                {
                    cnt ++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

B.

题目链接:点击打开链接

解题思路:

感觉智商又压制住了。。。明显暴力的O(m^2)会超时,我还是侥幸的试了试。。。果然TLE。

正解是开一个sum数组记录每个位置出现的反转次数,然后从0~len / 2去检查sum数组,设 k = 0,每次k += sum[i] ,如果k是奇数的话,那么就交换s[i]和s[len - i - 1]。这样求解是正确的,虽然没怎么太想明白。不过至少有一点是明确的,就是一个区间的字符串反转偶数次的话,那相当于没反转,所以只要考虑那些反转奇数次的即可。

完整代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
const int maxn = 200001;
string s;
int n;
int sum[maxn];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    while(cin >> s)
    {
        cin >> n;
        int key;
        int len = s.length();
        memset(sum , 0 , sizeof(sum));
        for(int i = 0 ; i < n ; i ++)
        {
            cin >> key;
            sum[key - 1] ++;
        }
        int k = 0;
        for(int i = 0 ; i < len / 2 ; i ++)
        {
            k += sum[i];
            if(k % 2 != 0)
            {
                swap(s[i] , s[len - i - 1]);
            }
        }
        cout << s << endl;
    }
    return 0;
}

C.

题目链接:点击打开链接

解题思路:

这场的C题感觉比B题简单,读完题第一个想法就是排个序,然后相邻比较下,再做和。唯一可能出问题的是数据太大,担心long long 存不下,于是开成unsigned long long。

可是这道题还有一个坑,那就是不一定要相邻的两个比较,因为照这个思路每个矩形是相邻四个数值构成的,写成i -= 2意味着当两对其中之一不成立时,另一对相当于被我们舍弃,这样最终得到的一定不是最大解,所以要写成i--,若满足条件,我们再i--。

完整代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
typedef unsigned long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
const int maxn = 2000001;
int n;

LL s[maxn];

bool cmp(LL a , LL b)
{
    return a < b;
}

LL min(LL a , LL b)
{
    return a < b ? a : b;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    while(cin >> n)
    {
        memset(s , 0 , sizeof(s));
        for(int i = 0 ; i < n ; i ++)
        {
            cin >> s[i];
        }
        sort(s , s + n , cmp);
        LL sum = 0;
        LL flag = -1;
        for(int i = n - 1 ; i >= 0 ; i --)
        {
            if( s[i] - s[i-1] <= 1)
            {
                LL k = s[i-1];
                if(flag == -1)
                {
                    flag = k;
                }
                else
                {
                    sum += k * flag;
                    flag = -1;
                }
                i --;
            }
            //cout << sum << endl;
        }
        cout << sum << endl;
    }
    return 0;
}
时间: 2024-07-30 14:02:41

Codeforces Round #297 (Div. 2)(模拟+字符串+排序)的相关文章

模拟 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 #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

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 #297 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/525 算是比较简单的一场了,拖了好久现在才补 A. Vitaliy and Pie time limit per test:2 seconds memory limit per test:256 megabytes After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that sim

Codeforces Round #541 (Div. 2) E 字符串 + 思维 + 猜性质

https://codeforces.com/contest/1131/problem/D 题意 给你n个字符串,字符串长度总和加起来不会超过1e5,定义字符串相乘为\(s*s1=s1+s[0]+s1+s[1]+s1+...+s1+s[size-1]+s1+s[size]+s1\),求n个字符串依次相乘后最长连续字符相同的子序列长度 题解 鬼畜的题意 or 难以优化的复杂度,都需要观察性质才能做,第二串要插入第一个串每个字符之间,可以看出字符数增长的速度很快,所以并不能把整个字符存下来 只看一种

Codeforces Round #297 (Div. 2)

A题 题目大意: 给你一个字符串,奇数的时候是钥匙,偶数的时候是门,一把钥匙只能开对应的门,然后问你最少额外需要多少把钥匙. 分析: 用的数组记录一下就行,(注意的是先开门,再拿钥匙!开始错在这里了,决心好好学英语) #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<queue> #inc

Codeforces Round #297 (Div. 2) 题解

A题: 有n个房间排成一条线,每个房间之间都有一个通道通往下个房间,则共有n-1个通道,现在你要从1到n,而且每个房间还放有一个钥匙,如果要开这个通道,通道的钥匙你有,则通过,否则你要买相应的钥匙. 大写代表这个通道,小写代表相应的钥匙. 问你最少需要买多少把钥匙. 样例: input 3aAbB output 0 input 4aBaCaB output 3 input 5xYyXzZaZ output 2 看样例应该就知道他要问什么了吧. 思路:直接扫一遍过去,看缺多少则买多少. 1 #in

Codeforces Round #250 (Div. 1)B(排序+并查集)

B. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains 

Codeforces Round #297 (Div. 2)E. Anya and Cubes

题目链接:http://codeforces.com/problemset/problem/525/E 题意: 给定n个数,k个感叹号,常数S 下面给出这n个数. 目标: 任意给其中一些数变成阶乘,至多变k个. 再任意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制状压,0代表不取,1代表取阶乘,2代表直接取: 中途查找,节约空间: 代码如下: #include<cstdio> #include<cstring> #include<iostream> #i