D题已经加入
A. Divide and Multiply
题意:总共T组样例,对于每组样例给出n个数,要求每一次可以找到两个数(至少有一个奇数),使得其中一个偶数除以2,另一个数乘以2,求该数组和最大值为多少;
解题思路:相信大家都知道“指数爆炸”这个概念,对于每个数组,我们只要将所有的乘2操作给最大的奇数,使得这个奇数最大。所以只需要先将数组内所有的偶数一直除以2直到变为奇数,再在这个奇数数组中找出最大的奇数,将所有的乘2操作给这个奇数,最后算一遍总和,数据范围来看需要开long long;
AC代码:
typedef long long ll;
const int N = 100005;
ll a[20];
void solve(){
int n;
scanf("%d", &n);
for(int i = 1; i<=n; i++){
scanf("%lld", &a[i]);
}
ll ans = 1;
for(int i = 1; i<=n; i++){
while(a[i]%2 == 0)ans*=2, a[i]/=2;
}
sort(a+1, a+n+1);
ll res = 0;
a[n]*=ans;
for(int i = 1; i<=n; i++){
res+=a[i];
}
printf("%lld\n", res);
return;
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
solve();
}
return 0;
}
B. William the Vigilant
题意:给出一个只含有’a’, ‘b’, 'c’字符的字符串s,q个操作,对于每一次,先将pow位置上的字符变为给定的字符,然后对于改变后的字符串将该字符串变为没有字串“abc”的字符串最少需要改变几个字符,每次输出最少需要改变的次数;
解题思路:B题还是很好想的,首先明确子数组的概念,题中已给出说明,由此推出对与一个abc字符串将他变为没有字串“abc”的字符串最少需要改变的字符数就是该串中子串“abc”的数量(对于每个字串abc只要将其中字符b改变,则一定减少一个“abc”子串),所以此题只需先算出原串中子串“abc”的数量,之后由于每次修改只修改一个字符,字串数量的变化是-1~+1的,每次改变找出字串的变化量,每次输出当前字串“abc”的数量就行了。
AC代码(代码比较啰嗦,但是直接暴力省脑细胞):
typedef long long ll;
const int N = 100005;
ll a[20];
void solve(){
int n, q;
scanf("%d%d", &n, &q);
string s;
cin>>s;
int x;
char c;
int ans = 0;
for(int i = 0; i<n; i++){
if(s[i] == 'a'&&s[i+1] == 'b'&&s[i+2] == 'c') ans++;
}
while(q--){
scanf("%d %c", &x, &c);
x--;
if(s[x] == 'a'){
if(s[x+1] == 'b'&&s[x+2] == 'c'){
if(c != 'a') ans--;
}
if(c == 'b'&&s[x-1] == 'a'&&s[x+1] == 'c')ans++;
if(c == 'c'&&s[x-2] == 'a'&&s[x-1] == 'b')ans++;
}
if(s[x] == 'b'){
if(s[x-1] == 'a'&&s[x+1] == 'c'){
if(c!='b') ans--;
}
if(c == 'a'&&s[x+1] == 'b'&&s[x+2] == 'c')ans++;
if(c == 'c'&&s[x-1] == 'b'&&s[x-2] == 'a')ans++;
}
if(s[x] == 'c'){
if(s[x-1] == 'b'&&s[x-2] == 'a'){
if(c!='c')ans--;
}
if(c == 'a'&&s[x+1] == 'b'&&s[x+2] == 'c') ans++;
if(c == 'b'&&s[x+1] == 'c'&&s[x-1] == 'a') ans++;
}
s[x] = c;
printf("%d\n", ans);
}
return;
}
int main()
{
int t;
// scanf("%d", &t);
t = 1;
while(t--){
solve();
}
return 0;
}
C. Complex Market Analysis
题意:对于一个长度为n的数组a,给一个偏移量e,求使得a[i]*a[i+e]a[i+2e]⋅…a[i+ke]的值为质数的(i,k)对有多少对。
解题思路:首先清楚质数的定义,要使得k个数相乘为质数,则k个数中有且仅有一个数为质数,其余的数都为1,否则乘积一定不为质数;随后直接对数组a进行遍历,对于每个质数,找到他前后连续的1(偏移量为e的情况下),随后利用乘法原理计算这个质数总的贡献值(前面的1数量加后面1的数量加两数相乘);
AC代码:
typedef long long ll;
const int N = 1000006;//不知道为啥re了一次,就直接开大了
bool st[1000016];
int primes[100005];
int cnt;
int a[N];
void get_primes(int n)//先把质数筛出来,直接判断质数可能会超时
{
for (int i = 2; i <= n; i ++ )
{
if (!st[i]) primes[cnt ++ ] = i;
for (int j = 0; primes[j] <= n / i; j ++ )
{
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
void solve(){
int n, e;
scanf("%d%d", &n, &e);
for(int i = 1; i<=n; i++) scanf("%d", &a[i]);
ll ans = 0;
for(int i = 1; i<=n; i++){
if(!st[a[i]]){//质数的时候
ll res = 0, num = 0;
for(int j = i+e; j<=n; j+=e){
if(a[j] == 1) {
ans++;
res++;
}
else break;
}
for(int j = i-e; j>=1; j-=e){
if(a[j] == 1){
ans++;
num++;
}
else break;
}
ans+=res*num;
}
}
printf("%lld\n", ans);
return;
}
int main()
{
int t;
scanf("%d", &t);
get_primes(1000005);
st[1] = 1;
while(t--){
solve();
}
return 0;
}
D. Social Network
题意:这就是个并查集纯板子题,只是题意非常的晦涩难懂(谜语人走开)昨晚看这个题意看不懂就跑路了,今天直接找个聚聚翻译了一下,大致就是一个聚会有n个人,且最初这n个人相互不认识,主人可以相互介绍客人,使得他们相互认识(如果两个人相互认识那么他们认识的人也相互认识),主人最多介绍d次,每次有一个限制条件(x,y),要求这次介绍之后,x和y需要相互认识,求每次介绍,拥有的最大的认识的人的数量最大是多少;
解题思路:这就很容易看出来是并查集了,由于每个i相互独立,且是先介绍后检查是否满足(x,y)的条件,用并查集维护连通块结点数量,当有一个(x,y)条件在介绍之前就已经满足的时候,那么这次的介绍就可以将两个最大的连通块连接,使得最大认识人最多的人数最多,由于每一次都是独立计算的,当出现这种情况的时候计数器ans加一,意味此次介绍,可以额外连接ans条边使得最大的连通块结点数最多;
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100005;
int p[N], sz[N], t[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
bool cmp(int x, int y){
return x>y;
}
int main()
{
int n, d;
scanf("%d%d", &n, &d);
for(int i = 1; i<=n; i++) p[i] = i, sz[i] = 1;
int x, y;
int ans = 0;
int res = 0;
while(d--){
scanf("%d%d", &x, &y);
x = find(x), y = find(y);
if(x == y){
ans++;
res+=t[ans+1];
}
else{
res = 0;
p[y] = x;
sz[x]+=sz[y];sz[y] = 0;
for(int i = 1; i<=n; i++) t[i] = sz[i];
sort(t+1, t+n+1, cmp);
for(int i = 1; i<=ans+1; i++) res += t[i];
}
printf("%d\n", res-1);
}
return 0;
}
完结,撒花!