CCPC绵阳站解题报告

A. Ban or Pick, What’s the Trick

考虑记忆化搜索,接下来便是如何设计状态了。

一个很简单的想法就是[A剩余的英雄][B剩余的英雄][A选择的英雄][B选择的英雄],但是$1\leqslant n \leqslant 10^5$,这种想法根本做不了。

然后很容易发现,英雄总数$n$减去B剩余的和选择的就是A禁用的英雄数,而A禁用和A选择的英雄数就是A的操作数也即第几轮,而从这也可以推出A剩余的英雄数,所以有一个状态是多余的,所以我们只需要记录[*剩余的英雄][A选择的英雄][B选择的英雄]即可,所以只需要[*是A还是B][*剩余的英雄][A选择的英雄][B选择的英雄]

然后记忆化搜素即可。

Code:

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,k,a[(int)1e5+9],b[(int)1e5+9],f[2][(int)2e5+9][11][11];
ll dfs(int who,int turna,int turnb,int ahs,int bhs,int val){
	if(turna==0&&turnb==0)return val;
	if(ahs==k&&bhs==k)return val;
	if(f[who][who?turnb:turna][ahs][bhs]!=0x3f3f3f3f3f3f3f3f)return f[who][who?turnb:turna][ahs][bhs]+val;
	ll res1=114514,res2=415411;
	bool ok1=0,ok2=0;
	if(who){
		if(turnb&&bhs<k)res1=dfs(who^1,turna-(!who),turnb-who,ahs,bhs+1,val-b[turnb]),ok1=1;
		if(turna)res2=dfs(who^1,turna-(who),turnb-(!who),ahs,bhs,val),ok2=1;
	}else{
		if(turna&&ahs<k)res1=dfs(who^1,turna-(!who),turnb-who,ahs+1,bhs,val+a[turna]),ok1=1;
		if(turnb)res2=dfs(who^1,turna-(who),turnb-(!who),ahs,bhs,val),ok2=1;
	}
	ll ans;
	if(ok1&&ok2) ans=(who?min(res1,res2):max(res1,res2));
	else if(ok1) ans=res1;
	else ans=res2;
	f[who][who?turnb:turna][ahs][bhs]=ans-val;
	return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(f,0x3f,sizeof(f));
    cin>>n>>k;
    f(i,1,n)cin>>a[i];
    f(i,1,n)cin>>b[i];
    sort(a+1,a+1+n);
    sort(b+1,b+1+n);
    cout<<dfs(0,n,n,0,0,0);
    return 0;
}

C. Catch You Catch Me

从根结点$1$开始dfs记录每个点子树的最大深度即可,然后答案是所有根结点后继子树的最大深度值之和。

Code:

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,dep[(int)1e5+9];
vi g[(int)1e5+9]; 


void dfs(int u,int fa){
	ll res=0;
	for(auto it:g[u]){
		if(it!=fa){
			dfs(it,u);
		}
		res=max(res,dep[it]);
	}
	dep[u]=res+1;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1,x,y;i<n;++i){
    	cin>>x>>y;
    	g[x].push_back(y),g[y].push_back(x);
    }
    dfs(1,0);
    ll ans=0;
    for(auto it:g[1])ans+=dep[it];
    cout<<ans;
    return 0;
}

H. Life is Hard and Undecidable, but…

容易发现只要斜线构造$2\times k-1$个点就好了

Code:

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll k; 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>k;
    cout<<2*k-1<<"\n";
    f(i,1,2*k-1){
    	cout<<i<<" "<<i<<"\n";
    }
    return 0;
}

G. Let Them Eat Cake

一开始看错题了,以为是both

模拟即可

Code:

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,a[(int)1e5+9],l[(int)1e5+9],r[(int)1e5+9],ans;
bitset<(int)1e5+9> vis;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    vi v(n);
    for(auto &it:v)cin>>it;
    while(v.size()>1){
        ans+=1;
        vis.reset();
        vi tmp;
        ll len=v.size();
        for(int i=0;i<len;++i){
            if(i!=0)if(v[i]<v[i-1])vis[i]=true;
            if(i!=len-1)if(v[i]<v[i+1])vis[i]=true;
        }
        for(int i=0;i<len;++i){if(!vis[i])tmp.push_back(v[i]);}
        v=tmp;
    }
    cout<<ans<<"\n";
    return 0;
}

M. Rock-Paper-Scissors Pyramid

容易发现如果相邻两个格子,后者可以赢前者,那么前者是过不去后者的,所以可以先用栈来模拟,遇到此类情况一直弹出即可。

然后栈内的情况均会是底下的可以赢上面的,所以一直弹出到最底下那个,输出即可。

Code:

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll tt;
char s[(int)1e6+9];
map<char,map<char,int>>win;


int main()
{
    win['S']['P']=1,win['P']['R']=1,win['R']['S']=1;
    win['S']['S']=1,win['P']['P']=1,win['R']['R']=1;
    scanf("%lld",&tt);
    f(sb,1,tt){
        stack<char> st;
        scanf("%s",s+1);
        ll n=strlen(s+1);
        st.push(s[1]);
        for(int i=2;i<=n;++i){
                while(!st.empty()&&win[s[i]][st.top()])st.pop();
                st.push(s[i]);
        }
        while(st.size()>1)st.pop();
        cout<<st.top()<<"\n";
    }
    return 0;
}
上一篇
下一篇