Codeforces Round #853 (Div. 2) (A、B、C)

A题

Problem - A - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1789/problem/A题意:
        找是否可以通过调整顺序使得前n长的数组的gcd不超过n

思路:
        我们可以看出,如果我们前两长的前缀gcd能做到 <= 2,那么我们后面的前缀gcd都能做到<= 2.

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 110;

int T;
int a[N];

int gcd(int a, int b)
{
	if(a < b) return gcd(b, a);
	return b ? gcd(b, a % b) : a;
}

signed main()
{
	scanf("%d", &T);
	int n;
	while(T--)
	{
		scanf("%d", &n);
		bool st = false;
		for (int i = 0; i < n; ++ i) scanf("%d", &a[i]);
		for (int i = 0; i < n - 1; ++ i)
			for (int j = i + 1; j < n; ++ j)
			{
				if(gcd(a[i], a[j]) <= 2) {
					st = true;
					break;
				}
			}
		if(st) puts("Yes");
		else puts("No");
	}
	return 0;
}

B题

Problem - B - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1789/problem/B题意:

        给定一个10字符串,判断是否能只用一次操作(操作是将某个连续区间内1转0, 0 转1)就能使得这个10串成为回文串。

思路:

        我们要想在一次操作就达到目的,我们就需要检索10串中前后不相同的连续区间有几个,如果只有一个或者零个那么就可以,否则不可以。

代码:
 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;

const int N = 1e5 + 10;

int T;
char a[N];

signed main()
{
	scanf("%d", &T);
	int n;
	while(T--)
	{
		scanf("%d", &n);
		scanf("%s", a);
		bool st = true;
		int l = 0, r = n - 1;
	    int ll = l, rr = r;
		for (int i = 0; i < n / 2; ++ i)
		{
			if(a[ll] != a[rr]) {
				st = false;
				break;
			}
			ll ++, rr --;
		}
		if(st) puts("Yes");
		else {
		    bool k = false;
		    bool fis = false;
		    bool tmp = false;
		    for (int i = 0; i < n / 2; ++ i)
		    {
		        if(fis && a[l] == a[r]) tmp = true;
		        if(fis && tmp && a[l] != a[r]) {
		            k = true;
		            break;
		        }
		        if(!fis && a[l] != a[r]) fis = true;
		        l ++, r --;
		    }
		    if(k) puts("No");
		    else puts("Yes");
		}
	}
	return 0;
} 

C题

Problem - C - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1789/problem/C题意:
        对于一个长为 n 的数列 a[n] ,其元素两两不相同。现有 m 次修改,每一次修改将位置为 p 的元素改为 b ( 即即a[p]=b ),并且保证修改以后仍然满足数列元素两两不同。假设第 i 次修改之后的数列为 Ai(0≤i≤m) ,任务是求出所有 与Ai与Aj(0≤i<j≤m) 构成的一个长度为 2n 的数列中( 一共有对一共有m∗(m+1)2对 ),不同元素的个数之和。

思路:
        逐个计算贡献度即可,记录每一次修改之后,用当前数列 Aj 与之前出现过的所有数列 Ai(0≤i<j) 进行运算。如何计算呢,我们可以尝试求 Aj 中每一个元素 a[i] 对结果的贡献。因为是拿当前数列和之前所有数列比较,我们可以统计当前数列中的元素在之前一共出现了多少次,每一个重复的值对结果的贡献是-1。举个例子,假设当前是第10轮修改,某一个 a[i]=5 ,并且前9轮的修改结果和初始数列中共有5个5,那么在本轮, a[i]=5 对于答案的贡献就是 10−5=5(用本轮结果与前面的个结果比较时,只有次的比较中产生了贡献)(用本轮结果与前面的10个结果比较时,只有5次的比较中a[i]产生了贡献) 。那么接下来,问题就转换成了如何求本轮在之前的结果中出现了几次的问题,然后用结果的最大可能值(2*n*j)减去这个重复的次数即可得到本轮的答案。求重复次数有很多方法,这里我的方法是设置一个向量记录某一个数上一次出现的位置,当这个数被修改时,即可用当前位置与上一次位置相减得到一个重复出现次数,再借助一个数组记录累计值即可。

代码:
 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long

using namespace std;

const int N = 4e5+10 ;

bool vis[N];
int p[N];       

signed main(){
    
   int T;
   cin>>T;
   while(T--){
    int n,m;
    cin>>n>>m;
    //记录上一次出现位置
    vector<int> st_pos(n+m+1);
    //记录累计次数
    vector<ll> sum(n+m+1);
    int pos,num;
    ll cnt=0;
    ll ans=0;
    for(ll i=1;i<=n;i++){

        cin>>num;
        vis[num]=1;
        p[i]=num;
        //st[num].push_back(0);
        st_pos[num]=0;
    }
    for(ll i=1;i<=m;i++){

        cin>>pos>>num;
        sum[p[pos]]+=(i-1)-st_pos[p[pos]]+1;
        st_pos[num]=i;
         cnt-=sum[p[pos]];
        if(!vis[num]){

            vis[num]=1; 
        }else{

            cnt+=sum[num];
        }
        cnt+=n;
        //可能的最大次数
        ans+=2*n*i-cnt;
        p[pos]=num;
       
    }
    cout<<ans<<endl;
    memset(vis,0,sizeof(vis));
   }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dawpro_加薪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值