首页 > 其他分享 >Educational Codeforces Round 165 (Rated for Div. 2) - VP记录

Educational Codeforces Round 165 (Rated for Div. 2) - VP记录

时间:2024-10-21 14:47:42浏览次数:1  
标签:main Educational Rated const int scanf Codeforces include

A. Two Friends

一共只有两种情况:

  1. 存在 \(A\) 的最好朋友是 \(B\) 且 \(B\) 的最好朋友是 \(A\) 的情况:此时只需邀请这两个人即可。
  2. 不存在上述情况:设某个人 \(A\) 的最好朋友是 \(B\),\(B\) 的最好朋友是 \(C\),这时邀请 \(A,B,C\) 三个人就可使 \(A,B\) 到场。

根据上述两种情况分别输出 \(2\) 和 \(3\) 即可。

#include<cstdio>
using namespace std;

const int N=55;
int n,p[N];

int main()
{
	int T; scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d",&p[i]);
		bool flag=false;
		for(int i=1;i<=n;i++)
			if(i==p[p[i]]) {flag=true; break;}
		printf("%d\n",flag?2:3);
	}
	return 0;
}

B. Shifts and Sorting

贪心,每次循环移动一截连续的 \(1\) 串,串的长度 \(+1\) 就是这次操作的代价。

这样不断右移 \(1\),最后可使 \(1\) 全部集中在右部。

代码中忽略了开头连续的 \(0\),把一列连续的 \(1\) 和一列连续的 \(0\) 分为一组(例如 11100 这种为一组),这个串所耗的代价就是 \((0\) 的数量 \()\) \(\times\) \((\) 当前总共 \(1\) 的数量 \(+1)\),每次计算前都把这一组 \(1\) 的数量累加到总共 \(1\) 的数量里去。

赛时没开 long long 还 WA 了一发。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=2e5+5;
int n; char s[N];
int cnt[N][3],idx;

int main()
{
	int T; scanf("%d",&T);
	while(T--)
	{
		scanf("%s",s+1);
		n=strlen(s+1);
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='0' && !idx) continue;
			if(s[i]!=s[i-1] && s[i]=='1') idx++;
			cnt[idx][s[i]-'0']++;
		}
		long long ans=0,len=0;
		for(int i=1;i<=idx;i++)
		{
			len+=cnt[i][1];
			ans+=1ll*cnt[i][0]*(len+1);
		}
		printf("%lld\n",ans);
		
		for(int i=1;i<=idx;i++)
			cnt[i][0]=cnt[i][1]=0;
		idx=0;
	}
	return 0;
}

C. Minimizing the Sum

标签:main,Educational,Rated,const,int,scanf,Codeforces,include
From: https://www.cnblogs.com/jerrycyx/p/18489464

相关文章

  • Codeforces Round 979 (Div. 2)
    CodeforcesRound979(Div.2)总结A首先第一位的贡献一定是\(0\),然后考虑接下来\(n-1\)位,我们可以把最大值和最小值放在第一位和第二位,这样贡献就是\((max-min)\times(n-1)\)。#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#includ......
  • 「题解」Codeforces Round 979 (Div. 2)
    before\(A\simD\)的题解A.AGiftFromOrangutanProblemA.AGiftFromOrangutanSol&Code\(c_i-b_i\)最大就是\(a\)的最大值减最小值。将\(a\)的最大值\(x\)和最小值\(y\)放到头两位即可得到答案\((x-y)(n-1)\)。#include<bits/stdc++.h>#defineM......
  • Codeforces Round 980 (Div. 2) C. Concatenation of Arrays
    题目:给定n个数组a1,a2,…,an。每个数组的长度都是2。因此,ai=[ai,1,ai,2]。你需要将这些数组连接成一个长度为2n的单一数组,以便使结果数组中的逆序数最小。注意,你不需要实际计算逆序的数量。更正式地说,你需要选择一个长度为n的排列p,使得数组b=[ap1,1,ap1,2,......
  • Codeforces Round 979 div2 个人题解(A~E)
    CodeforcesRound979div2个人题解(A~E)Dashboard-CodeforcesRound979(Div.2)-Codeforces火车头#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<array>#include<bitset>#include<cmath>#include<cstdio>#inc......
  • Codeforces Round 979 (Div. 2) C. A TRUE Battle
    题目链接:题目大意:Alice和Bob先后轮流向一串01字符串中加入or或and,前者想让结果为1后者想让结果为0,问Alice能不能赢。思路:对于相同的两个布尔值,or和and都不能改变它们,而对于Alice,他倾向于向01之间加入or,Bob则想加入and。一开始我想比较1和0的多少不就行了吗,但是不对,当你......
  • Codeforces Round 979 (Div. 2) B. Minimise Oneness
    题目链接:题目大意:构造长度为nnn的01字符串,使得全为零的子序列和至少有一个1的子序列的数量之差的绝对值最小。思路:很明显,所有子序列中不是全为0就是至少有一个1,所以算......
  • Codeforces Round 980 (Div. 2)
    糖丸了,什么沟史比赛A.ProfitableInterestRate初始有\(a\)个硬币,可以花费硬币开通盈利账户与非盈利账户开通盈利账户需要至少花费\(b\)个金币开通非盈利账户没有限制每在非盈利账户花费\(x\)元,盈利账户的限制\(b\)就减少\(2x\)元求最大的在盈利账户上的花......
  • Nuxt.js 应用中的 app:templatesGenerated 事件钩子详解
    title:Nuxt.js应用中的app:templatesGenerated事件钩子详解date:2024/10/19updated:2024/10/19author:cmdragonexcerpt:app:templatesGenerated是Nuxt.js的一个生命周期钩子,在模板编译到虚拟文件系统(VirtualFileSystem,VFS)之后被调用。这个钩子允许开发......
  • Codeforces Round 977 (Div. 2)
    一万一参赛,赛时排名151A.MeaningMean简单贪心题。显然,排在越后面的数,除以2的次数越少。因此贪心地从小到大计算结果即为答案。#include<bits/stdc++.h>usingnamespacestd;constintN=55;intT,n,a[N];intmain(){ scanf("%d",&T); while(T--){ scanf......
  • Codeforces Round 977 (Div. 2, based on COMPFEST 16 - Final Round) A-C1
    ​A.MeaningMean2024.10.17算法:模拟,贪心思路:居然时没看题解直接做出来的T^T贪心:题目要求最后剩下的一个数,使得最大那么我们从这个最大的最后一个数思考,最后一个数肯定时由最后两个数进行相加,再除以2,同时上下取整而得到的。方便陈述,我们设最大的最后一个数,也就是最终答案......