AtCoder Beginner Contest 365

D

Problem Statement

Takahashi and Aoki played rock-paper-scissors N times. [Note: In this game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.]

Aoki's moves are represented by a string S of length N consisting of the characters RP, and S. The i-th character of S indicates Aoki's move in the i-th game: R for Rock, P for Paper, and S for Scissors.

Takahashi's moves satisfy the following conditions:

  • Takahashi never lost to Aoki.
  • For i=1,2,…,N−1, Takahashi's move in the i-th game is different from his move in the(i+1)-th game.

Determine the maximum number of games Takahashi could have won.

It is guaranteed that there exists a sequence of moves for Takahashi that satisfies these conditions.

Constraints

  • 1≤N≤2×10^5
  • S is a string of length N consisting of RP, and S.
  • N is an integer.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the maximum number of games Takahashi could have won.


Sample Input 1

6
PRSSRS

Sample Output 1

5

In the six games of rock-paper-scissors, Aoki played Paper, Rock, Scissors, Scissors, Rock, and Scissors.

Takahashi can play Scissors, Paper, Rock, Scissors, Paper, and Rock to win the 1st, 2nd, 3rd, 5th, and 6th games.

There is no sequence of moves for Takahashi that satisfies the conditions and wins all six games, so print 55.


Sample Input 2

10
SSSSSSSSSS

Sample Output 

5

Sample Input 3

24
SPRPSRRRRRPPRPRPSSRSPRSS

Sample Output 3

18

要保证不输 

#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
const int N=2e5+10;
const int M=110;
const int inf=1e9;
int T;
int n;
string s;
int a[N];
int dp[N][4];//Takahashi的第i步选的第j个选项 
void solve()
{
  cin>>n;
  cin>>s;
  s=" "+s;
  
  for(int i=1;i<=n;i++)
  {
  	if(s[i]=='R')
  	{
  		a[i]=1;
	}
	else if(s[i]=='P')
	{
		a[i]=2;
	}
	else if(s[i]=='S')
	{
		a[i]=3;
	}
  }
  
  for(int i=1;i<=n;i++)
  {
  	for(int j=1;j<=3;j++)
  	{
  		if(a[i]==1)
  		{
  			if(j==1)
		    {
		  		dp[i][j]=max(dp[i-1][2],dp[i-1][3]);//没赢 
		    } 
		    else if(j==2)
			{
				dp[i][j]=max(dp[i-1][1],dp[i-1][3])+1;//赢了 
			} 
		}
		else if(a[i]==2)
		{
			if(j==2)
			{
				dp[i][j]=max(dp[i-1][1],dp[i-1][3]);//没赢 
			}
			else if(j==3)
			{
				dp[i][j]=max(dp[i-1][2],dp[i-1][1])+1;//赢了 
			}
		}
		else if(a[i]==3)
		{
			if(j==3)
			{
				dp[i][j]=max(dp[i-1][2],dp[i-1][1]);//没赢 
			}
			else if(j==1)
			{
				dp[i][j]=max(dp[i-1][2],dp[i-1][3])+1;//赢了 
			}
		} 
	}
  }
  cout<<max({dp[n][1],dp[n][2],dp[n][3]})<<endl;
}
int main()
{
//	cin>>T;	
	T=1;
	while(T--)
	{
	  solve();
	}

  return 0;
}

 E

 考虑第k位的贡献,但是这时候需要对每一位进行异或前缀和记录,因为涉及到所有区间,根据第一类我们可以知道假设这一位是1,那么我们需要考虑之前异或前缀为0的数量,反之这一位为0,我们需要知道异或前缀为1的数量,答案累加就是第k位的贡献。

#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
const int N=2e5+10;
const int M=110;
const int inf=1e9;
int T;
int n;
int a[N];
ll cnt[35];
void solve()
{
  cin>>n;
  fp(i,1,n)cin>>a[i],a[i]^=a[i-1];
  
  
  for(int i=1;i<=n;i++)
  {
  	for(int j=0;j<32;j++)
  	{
  		if(a[i] >> j & 1)cnt[j]++;
	}
  }
  
  ll ans=0;
  
  for(int i=0;i<32;i++)
  {
  	ans+=cnt[i]*(n+1-cnt[i])*(1ll<<i);
  }
  
  cout<<ans<<"\n";
  
}
int main()
{
//	cin>>T;	
	T=1;
	while(T--)
	{
	  solve();
	}

  return 0;
}

差不多的题,减去一部分就行。

#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
const int N=2e5+10;
const int M=110;
const int inf=1e9;
int T;
int n;
int a[N];
ll cnt[35];
void solve()
{
	
  ll ans=0;
  cin>>n;
  fp(i,1,n)cin>>a[i],ans-=a[i],a[i]^=a[i-1];
  
  
  for(int i=1;i<=n;i++)
  {
  	for(int j=0;j<32;j++)
  	{
  		if(a[i] >> j & 1)cnt[j]++;
	}
  }
    
  for(int i=0;i<32;i++)
  {
  	ans+=cnt[i]*(n+1-cnt[i])*(1ll<<i);
  }
  
  cout<<ans<<"\n";
  
}
int main()
{
//	cin>>T;	
	T=1;
	while(T--)
	{
	  solve();
	}

  return 0;
}

 

  • 30
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值