中石大第39次CSP培训Week6题解
A.消消乐
Key:双指针
这道题要我们消除到不能再消除时,字符串长度最大。很明显,我们一定要用尽量前的 A 来消除尽量后的 B,从而使字符串大,剩下用双指针优化模拟过程即可。
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int ans=0;
string s;
void solve(){
cin>>s;
int n=s.size();
s=" "+s;
int l=1;int r=n;
while(l<r){
while(s[l]=='B')l++;
while(s[r]=='A')r--;
if(l<r&&s[l]=='A'&&s[r]=='B'){
ans+=2;
l++;r--;
}
else break;
}
cout<<n-ans<<endl;
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
B.KMP
Key:KMP
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 1e6+10, MAX = 1e9, INF = -1e9;
string s;
string p;
int ne[N];
void solve(){
cin>>s>>p;
int n=s.size();int m=p.size();
s=" "+s;p=" "+p;
for(int i=2,j=0;i<=m;i++){
while(j&&p[i]!=p[j+1])j=ne[j];
if(p[i]==p[j+1])j++;
ne[i]=j;
}
for(int i=1,j=0;i<=n;i++){
while(j&&s[i]!=p[j+1])j=ne[j];
if(s[i]==p[j+1])j++;
if(j==m){
cout<<i-m+1<<endl;
j=ne[j];
}
}
for(int i=1;i<=m;i++){
cout<<ne[i]<<" ";
}
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
C.A+B Problem(高精)
Key:高精度
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
string a,b;
int j=0;
string c="";
void solve(){
cin>>a>>b;
int len=max(a.size(),b.size());
while(a.size()<len)a='0'+a;
while(b.size()<len)b='0'+b;
for(int i=len-1;i>=0;i--){
int t1=a[i]-'0';
int t2=b[i]-'0';
j=t1+t2+j;
c=char(j%10+'0')+c;
j/=10;
}
if(j!=0)c='1'+c;
cout<<c<<endl;
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
D.区间和
Key:离散化
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n,m;
vector<int> p;
vector<PII> add,query;
int a[N],s[N];
int find(int x){
int l=0;int r=p.size()-1;
while(l<r){
int mid=(l+r)>>1;
if(p[mid]>=x)r=mid;
else l=mid+1;
}
return r+1;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x,c;
cin>>x>>c;
add.pb({x,c});
p.pb(x);
}
for(int i=1;i<=m;i++){
int l,r;
cin>>l>>r;
query.pb({l,r});
p.pb(l);p.pb(r);
}
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
for(auto i:add){
int x=find(i.first);
a[x]+=i.second;
}
for(int i=1;i<=p.size();i++){
s[i]=s[i-1]+a[i];
}
for(auto i:query){
int l=find(i.first);int r=find(i.second);
cout<<s[r]-s[l-1]<<endl;
}
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
E.并查集
Key:并查集
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n,m;
int p[N];
int op,a,b;
int find(int x){
if(p[x]!=x)p[x]=find(p[x]);
return p[x];
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++)p[i]=i;
for(int i=1;i<=m;i++){
cin>>op;
if(op==1){
cin>>a>>b;
p[find(b)]=find(a);
}
else{
cin>>a>>b;
cout<<((find(a)==find(b)) ? "Y" : "N")<<endl;
}
}
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
F.取数游戏 II
Key:博弈论
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n;
int a[N];
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)
if(a[i]==0){
if(i%2==0){
cout<<"YES"<<endl;
return ;
}
break;
}
for(int i=n;i>=1;i--)
if(a[i]==0){
if((n-i+1)%2==0){
cout<<"YES"<<endl;
return ;
}
break;
}
cout<<"NO"<<endl;
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
G.单源最短路径(标准版)
Key:Dijkstra(堆优化)
朴素版
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 510, MAX = 1e9, INF = -1e9;
int n,m,s;
vector<vector<int>> v(N);
map<PII,int> mp;
bool st[N];
int dist[N];
void dijkstra(){
memset(dist,0x3f,sizeof dist);
dist[s]=0;
for(int i=1;i<=n;i++){
int t=-1;
for(int j=1;j<=n;j++){
if(!st[j]&&(t==-1||dist[j]<dist[t])){
t=j;
}
}
for(int j=1;j<=n;j++){
if(mp.find({t,j})!=mp.end())
dist[j]=min(dist[j],dist[t]+mp[{t,j}]);
}
st[t]=true;
}
}
void solve(){
cin>>n>>m>>s;
for(int i=1;i<=m;i++){
int a,b,e;
cin>>a>>b>>e;
v[a].pb(b);
if(mp.find({a,b})!=mp.end())mp[{a,b}]=min(mp[{a,b}],e);
else mp[{a,b}]=e;
}
dijkstra();
for(int i=1;i<=n;i++)cout<<dist[i]<<" ";
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
堆优化版
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<PII, vector<PII>, greater<PII>> Q;
const int N = 1e5 + 10, MAX = 1e9, INF = -1e9;
int n,m,s;
vector<vector<int>> v(N);
map<PII,int> mp;
bool st[N];
int dist[N];
Q q;
void dijkstra(){
memset(dist,0x3f,sizeof dist);
dist[s]=0;
q.push({0,s});
while(!q.empty()){
int p=q.top().second;
int distance=q.top().first;
q.pop();
if(st[p])continue;
st[p]=true;
for(auto i:v[p]){
if(dist[i]>dist[p]+mp[{p,i}]){
dist[i]=dist[p]+mp[{p,i}];
q.push({dist[i],i});
}
}
}
}
void solve(){
cin>>n>>m>>s;
for(int i=1;i<=m;i++){
int a,b,e;
cin>>a>>b>>e;
v[a].pb(b);
if(mp.find({a,b})!=mp.end())mp[{a,b}]=min(mp[{a,b}],e);
else mp[{a,b}]=e;
}
dijkstra();
for(int i=1;i<=n;i++)cout<<dist[i]<<" ";
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
H.单调栈
Key:单调栈
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 3e6 + 10, MAX = 1e9, INF = -1e9;
int n,a[N],ans[N];
stack<int>s;
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=n;i>=1;i--)
{
while(!s.empty()&&a[s.top()]<=a[i]) s.pop();
ans[i]=(s.empty() ? 0 : s.top());
s.push(i);
}
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}