340 C(ABC)
cin>>5
Here is an example of how Takahashi performs the operations:
- Initially, there is one 33 written on the blackboard.
- He chooses 33. He pays 33 yen, erases one 33 from the blackboard, and writes ⌊3/2⌋=1⌊2/3⌋=1 and ⌈3/2⌉=2 ⌈2/3⌉=2 on the blackboard.
- There is one 22 and one 11 written on the blackboard.
- He chooses 22. He pays 22 yen, erases one 22 from the blackboard, and writes ⌊2/2⌋=1 ⌊2/2⌋=1 and ⌈2/2⌉=1 ⌈2/2⌉=1 on the blackboard.
- There are three 11s written on the blackboard.
- Since all integers not less than 22 have been removed from the blackboard, the process is finished.
Takahashi has paid a total of 3+2=53+2=5 yen for the entire process, so print 55.
#include <bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> mp;
int dfs(int x)
{
if(x<2)return 0;
if(mp[x]) return mp[x];//直接用
int t=x;
if(x&1)
{
t+=dfs(x>>1)+dfs((x>>1)+1);
mp[x]=t;
return t;
}
else
{
t+=dfs(x>>1)*2;
mp[x]=t;
return t;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >>n;
cout<<dfs(n);
return 0;
}
341(ABC)
348 C
3 2 5 1 2 9 cout<<Yes
In this input, a week consists of seven days, with the first through second days being holidays and the third through seventh days being weekdays.
Let us assume today is the seventh day of the week. In this case, one day later would be the first day of the week, two days later would be the second day of the week, and nine days later would also be the second day of the week, making all plans scheduled on holidays. Therefore, it is possible for all of Takahashi's N plans to be scheduled on holidays.
此题格外注意边界情况的讨论
xxx....xxx
...xxxxxx两种情况
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,a,b;
int op[maxn];
void solve()
{
for(int i=1;i<=n;i++)
{
cin>>op[i];
op[i]=op[i]%(a+b);
}
sort(op+1,op+n+1);
if(op[n]-op[1]<a)
{
cout<<"Yes\n";
return;
}
for(int i=1;i<n;i++)
{
if(op[i+1]-op[i]>b)
{
cout<<"Yes\n";//两段时间间隔超过了这么多就可以合并到一起
return ;
}
}
cout<<"No\n";
return ;
}
map<int,int>mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>a>>b;
solve();
return 0;
}
346 B
There is an infinitely long piano keyboard. Is there a continuous segment within this keyboard that consists of W white keys and B black keys?
Let S be the string formed by infinitely repeating the string wbwbwwbwbwbw
.
Is there a substring of S that consists of W occurrences of w
and B occurrences of b
?
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,a,b;
int op[12]={0,1,0,1,0,0,1,0,1,0,1,0};
void solve()
{
for(int i=0;i<12;i++)
{
int s1=0,s2=0;
for(int j=0;j<a+b;j++)
{
if(op[(i+j)%12]==0) s1++;
else s2++;
}
if(s1==a&&s2==b)
{
cout<<"Yes\n";
return ;
}
}
cout<<"No\n";
return ;
}
map<int,int>mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>a>>b;
solve();
return 0;
}
345 C
You are given a string S. Find the number of strings that can result from performing the following operation exactly once.
- Let N be the length of S. Choose a pair of integers (i,j) such that 1≤i<j≤N and swap the i-th and j-th characters of S.
It can be proved that you can always perform it under the constraints of this problem.
若是有两个一样的字母则还为原顺序,需要加1
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=103;
map<char,int> mp;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,ans=0,cnt=0;
bool flag=0;
string s;
cin>>s;
int len=s.size();
for(int i=0;i<len;i++)
{
mp[s[i]]++;
}
for(int i='a';i<='z'&&len>0;i++)
{
if(mp[i]>=2) flag=1;
ans+=mp[i]*(len-mp[i]);
len-=mp[i];
}
if(flag==1) ans++;
cout<<ans<<endl;
return 0;
}
344 C
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=110;
int a[maxn],b[maxn],c[maxn];
map<int,int>mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m,l,q,x;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
cin>>m;
for(int i=1;i<=m;i++) cin>>b[i];
cin>>l;
for(int i=1;i<=l;i++) cin>>c[i];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
for(int k=1;k<=l;k++)
mp[a[i]+b[j]+c[k]]++;
cin>>q;
for(int i=1;i<=q;i++)
{
cin>>x;
if(mp[x]>0) cout<<"Yes\n";
else cout<<"No\n";
}
}
342 D
You are given a sequence of non-negative integers A=(A1,…,AN) of length N. Find the number of pairs of integers (i,j) that satisfy both of the following conditions:
- 1≤i<j≤N
- AiAj is a square number.
Here, a non-negative integer a is called a square number when it can be expressed as 2a=d^2 using some non-negative integer d.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int a[maxn];
map<int,int> mp;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,ans=0,maxx=0;
cin >>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
maxx=max(maxx,a[i]);
if(a[i])
{
for(int j=2;j*j<=a[i];j++)
while(a[i]%(j*j)==0)
a[i]/=j*j;
}
mp[a[i]]++;
}
for(int i=0;i<=maxx;i++)
{
ans+=mp[i]*(mp[i]-1)/2;
}
ans+=mp[0]*(n-mp[0]);
cout<<ans;
return 0;
}