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;
}