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
R
,P
, andS
. The i-th character of S indicates Aoki's move in the i-th game:R
for Rock,P
for Paper, andS
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
R
,P
, andS
.- N is an integer.
Input
The input is given from Standard Input in the following format:
N SOutput
Print the maximum number of games Takahashi could have won.
Sample Input 1
6 PRSSRSSample Output 1
5In 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 SSSSSSSSSSSample Output
5
Sample Input 3
24 SPRPSRRRRRPPRPRPSSRSPRSSSample 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;
}