「周练」Codeforces Round #526 (Div. 2)

一. Codeforces Round #526 (Div. 2)

比赛网址链接:http://codeforces.com/contest/1084

A. The Fair Nut and Elevator (枚举)

题意

  要选一个电梯停放处,使得所有人一天内使用电梯的总时间最短,输出这个最短时间

思路

  范围小,直接暴力枚举电梯停放楼层,每次计算一个时间,找最小即可

代码

#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=120;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
int n,t,m,mid;
int a[maxn];
int main()
{
    cin>>n;
    int ans=inf;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int x=1;x<=n;x++){
        int sum=0;
        for(int i=1;i<=n;i++){
            if(a[i]){
                if(x<=i){
                    sum+=(4*i-4)*a[i];
                }
                else {
                    sum+=4*(x-1)*a[i];
                }
            }
        }
        ans=min(ans,sum);
    }
    cout<<ans<<endl;
    return 0;
}

B. Kvass and the Fair Nut (贪心)

题意

  有 n n n 桶啤酒,第 i i i 桶有 a i a_i ai 升啤酒,现在从这 n n n 桶啤酒中倒出 s s s 升,问怎样倒使得剩下的 n n n 桶啤酒的最小值尽可能大

思路

− 1 -1 1 的情况肯定是所有啤酒升数加起来也没有 s s s。然后我们对这 n n n 桶啤酒从小到大排个序,现在最小值是 a 1 a_1 a1,看把所有桶的啤酒都倒的剩下最多 a 1 a_1 a1,看 s s s 是否耗尽,若在此过程耗尽了,那么最小值就是 a 1 a_1 a1,否则,就均匀的倒,直到 s s s 耗尽。

代码

#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=120;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
int n,t,m,mid;
ll s,ans;
int a[N];
int main()
{
    cin>>n>>s;
    ll sum=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    if(sum<s){
        puts("-1");return 0;
    }
    sort(a+1,a+1+n);
    sum=0;
    for(int i=2;i<=n;i++)sum+=a[i]-a[1];
    if(sum>=s)ans=a[1];
    else {
        ll ca=s-sum;
        ans=a[1]-(ca/n+(ca%n!=0));
    }
    cout<<ans<<'\n';
    return 0;
}

C. The Fair Nut and String (思维+组合数学)

题意

给一个字符串 s s s,让你从 s s s 中选一个子序列,有两个要求:
  1.全为 a a a 字符。
  2.每两个 a a a 字符之间在原序列中一定夹着至少一个 b b b 字符,求能构造出多少个这样的序列

思路

  记录 s s s 中连续的一段字符串区间(满足不含 b b b,含 a a a 字符最多)的 a a a 的字符个数,以 b b b 为分界线,每个区间的 a a a 可以选择全不选,也可以选择选其中的任意一个 a a a,若其中一个这样的区间有 c i c_i ci a a a,那么就有 c i + 1 c_i+1 ci+1 种选法,每个区间都是这样(设这样的区间共 n n n 个),就有 s u m = ∏ i = 1 n 1 + c i sum=\prod_{i=1}^n {1+c_i} sum=i=1n1+ci,则最终结果为 a n s = s u m − 1 ans=sum-1 ans=sum1,因为如果每个区间都一个 a a a 也不选也包含在内,不符合题意的情况要减去。

代码

#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e5+7;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,t,m;
char s[maxn];
ll a[maxn];
vector<int> v;
int main()
{
    cin>>(s+1);
    n=strlen(s+1);
    int sum=0;
    for(int i=1;i<=n;i++){
        if(s[i]=='a')sum++;
        else if(s[i]=='b'){
            if(sum!=0){
                v.pb(sum);
                sum=0;
            }
        }
    }
    if(sum)v.pb(sum);
    ll ans=1;
    for(auto x: v){
        ans=(ans*(x+1))%mod;
    }
    ans=(ans-1+mod)%mod;
    cout<<ans<<'\n';
    return 0;
}

D. The Fair Nut and the Best Path (树形dp)

题意

  一棵树,每个结点有值,边也有值,让你找一条路径,使得该路径上的所有点的权值和减去边的权值和的结果最大

思路

  算是经典树形 d p dp dp了,任意选一个根,每个结点维护两个值,一个是其到叶子的路径最大值,一个是次大值,也即是定义 f [ u ] [ 0 ] f[u][0] f[u][0]:结点 u u u 到叶子路径的最大权值和, f [ u ] [ 1 ] f[u][1] f[u][1]:结点 v v v 到叶子路径的次大权值和,然后最终结果即为: m a x ( f [ u ] [ 0 ] + f [ u ] [ 1 ] − a [ u ] ) max(f[u][0]+f[u][1]-a[u]) max(f[u][0]+f[u][1]a[u]) a [ u ] a[u] a[u] u u u 结点权值,减去是因为其在 f [ u ] [ 0 ] f[u][0] f[u][0] 算了一遍, f [ u ] [ 1 ] f[u][1] f[u][1] 也算了一遍。注意只有一个结点的情况。

代码

#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=3e5+7;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m;
int a[maxn];
vector<P> g[maxn];
ll f[maxn][2],ans;
void dfs(int u,int fa){
    f[u][0]=a[u];
    for(auto no: g[u]){
        int v=no.ft,w=no.sd;
        if(v==fa)continue;
        dfs(v,u);
        if(f[v][0]+a[u]-w>f[u][0]){//维护最大值与次大值
            f[u][1]=f[u][0];
            f[u][0]=f[v][0]+a[u]-w;
        }
        else if(f[v][0]+a[u]-w>f[u][1]){
            f[u][1]=f[v][0]+a[u]-w;
        }
    }
    ans=max(ans,f[u][0]+f[u][1]-a[u]);
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        ans=max(ans,(ll)a[i]);
    }
    for(int i=1,u,v,w;i<n;i++){
        scanf("%d%d%d",&u,&v,&w);
        g[u].pb(P(v,w));
        g[v].pb(P(u,w));
    }
    dfs(1,0);
    cout<<ans<<'\n';
    return 0;
}

E. The Fair Nut and Strings (字典树/找规律)

题意

  在给定的长度为 n n n 的字符串 a a a b b b 中,找到字典序介于两者之间的至多 k k k 个长度也为 n n n 的字符串,使得它们不同的前缀字符串的数量最多。

思路

  根据两个字符串建立一棵字典树,然后结果就是前 n + 1 n+1 n+1 层每层的结点数(不包括根节点),如果某层节点数大于 k k k,就取 k k k 个即可
例如样例二的字典树为:
在这里插入图片描述

代码

#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=3e5+7;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,k;
string a,b;
int main()
{
    cin>>n>>k>>a>>b;
    int sum=1;
    ll ans=0;
    for(int i=0;i<n;i++){
        sum<<=1;
        sum-=(a[i]=='b')+(b[i]=='a');//a[i]为b时,该层最左端会少一个,b[i]为a时同理
        if(sum>=k){//后面的层肯定都不小于k,所以每层只有k个
            ans+=(ll)k*(n-i);break;
        }
        ans+=sum;
    }
    cout<<ans<<'\n';
    return 0;
}

二. 搜索练习

A. Find a way (两次bfs)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2612

题意

  分处两地的两个人要去同一个咖啡厅,问去哪个两人加起来总费时最少,并输出

思路

  最短路径,一般用 b f s bfs bfs,那就对两个人各 b f s bfs bfs 一次,每个人到咖啡厅的时间记录下,最后输出最小的那个

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=200+7;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
int n,k,t,m;
char s[maxn][maxn];
P pos1,pos2;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
bool vis[maxn][maxn];
int dp[maxn][maxn],ans;
struct node{
    int x,y,s;
    node(){}
    node(int x,int y,int s):x(x),y(y),s(s){}
};
void bfs(P pos){
    queue<node> q;
    while(!q.empty())q.pop();
    q.push(node(pos.ft,pos.sd,0));
    while(!q.empty()){
        node no=q.front();q.pop();
        if(s[no.x][no.y]=='@'){
            if(dp[no.x][no.y]==-1)dp[no.x][no.y]=no.s;//一个人的记录
            else {
                dp[no.x][no.y]+=no.s;//两个人的记录
                ans=min(ans,dp[no.x][no.y]);
            }
        }
        for(int i=0;i<4;i++){
            int nx=no.x+dx[i],ny=no.y+dy[i];
            if(nx<0||nx>=n||ny<0||ny>=m)continue;
            if(!vis[nx][ny]&&s[nx][ny]!='#'){
                vis[nx][ny]=1;
                q.push(node(nx,ny,no.s+1));
            }
        }
    }
}
int main()
{
    while(cin>>n>>m){
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                vis[i][j]=0;
                dp[i][j]=-1;
            }
        }
        ans=inf;
        for(int i=0;i<n;i++)cin>>s[i];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(s[i][j]=='Y'){
                    pos1=P(i,j);
                }
                else if(s[i][j]=='M'){
                    pos2=P(i,j);
                }
            }
        }
        bfs(pos1);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                vis[i][j]=0;
            }
        }
        bfs(pos2);
        cout<<ans*11<<'\n';
    }
    return 0;
}

B. Xor-Paths (折半枚举)

题目链接

http://codeforces.com/problemset/problem/1006/F

题意

  一个 n × m n×m n×m 的数字矩阵,起点在左上角 ( 1 , 1 ) (1,1) (1,1) 处,终点在右下角 ( n , m ) (n,m) (n,m) 处,求有多少条路径满足路径数字异或和为 k k k

思路

  由于 n , m = 20 n,m=20 n,m=20,直接暴力搜索复杂度太高,为 O ( 2 n + m ) O(2^{n+m}) O(2n+m),所以可以考虑折半搜索,可以先选一条对角线,我选的是 n + m 2 + 1 \frac{n+m}{2}+1 2n+m+1,然后分别从起点、终点往对角线上搜索,在对角线处结束。一般折半搜索都会先搜索一半,并把所搜的结果保存起来,一般用 m a p map map s e t set set,然后搜索另外一半时利用搜索的结果去前一半搜索的结果中去找符合条件的结果集。这题也是一样,从起点搜索一半后,每一行用一个 m a p map map 来存当前行的搜索结果(异或值),然后从终点往对角线搜索时,到了对角线就利用当前的路径异或值取看看当前行的 m a p map map 里是否存在满足条件的值。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=27;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
int n,t,m,mid;
bool vis[maxn][maxn];
ll a[maxn][maxn],k;
map<ll,int> mp[maxn];
ll ans=0;
void dfs(int x,int y,ll w){//起点搜一半,结果存map
    if(x>n||y>m)return;
    w^=a[x][y];
    vis[x][y]=1;
    if(x+y==(n+m)/2+1){
        mp[x][w]++;return;
    }
    dfs(x+1,y,w);
    dfs(x,y+1,w);
}
void _dfs(int x,int y,ll w){//终点搜一半,map找符合
    if(x<1||y<1)return;
    if(!vis[x][y]){
        w^=a[x][y];
    }
    else {
        ans+=mp[x][k^w];
        return;
    }
    _dfs(x-1,y,w);
    _dfs(x,y-1,w);
}
int main()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%lld",&a[i][j]);
        }
    }
    dfs(1,1,0);
    _dfs(n,m,0);
    cout<<ans<<'\n';
    return 0;
}

C. Honeycomb (bfs)

题目链接

https://nanti.jisuanke.com/t/A2204

题意

  就是问一个蜜蜂从起点 S S S 到终点 T T T 的最短距离,不存在输出 − 1 -1 1

思路

  就是普通的 b f s bfs bfs 找最短路,没啥坑,也就图复杂一点

代码

#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=4110;
const int maxm=6110;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,t,m,ans;
P bpos,epos;
char s[maxn][maxm];
bool vis[maxn][maxm];
int len[maxn];
int dx[6]={-4,4,-2,-2,2,2},dy[6]={0,0,-6,6,-6,6};
struct node{
    int x,y,s;
    node(){}
    node(int x,int y,int s):x(x),y(y),s(s){}
};
void bfs(){
    queue<node> q;
    while(!q.empty())q.pop();
    q.push(node(bpos.ft,bpos.sd,1));
    vis[bpos.ft][bpos.sd]=1;
    while(!q.empty()){
        node no=q.front();q.pop();
        for(int i=0;i<6;i++){
            int nx=no.x+dx[i],ny=no.y+dy[i];//上、下、左上、右上、左下、右下
            int midx=no.x+dx[i]/2,midy=no.y+dy[i]/2;//中间障碍是否存在
            if(nx<0||nx>=n||ny<0||ny>=m)continue;
            if(!vis[nx][ny]&&s[midx][midy]==' '){
                q.push(node(nx,ny,no.s+1));
                vis[nx][ny]=1;
                if(nx==epos.ft&&ny==epos.sd){
                    ans=no.s+1;break;
                }
            }
        }
        if(ans!=-1)break;
    }
    printf("%d\n",ans);
}
void solve(){
    scanf("%d%d",&n,&m);
    n=4*n+3,m=6*m+3,ans=-1;
    getchar();
    for(int i=0;i<n;i++){
        gets(s[i]);
        len[i]=strlen(s[i]);
        for(int j=0;j<len[i];j++){
            if(s[i][j]=='S'){
                bpos.ft=i,bpos.sd=j;
            }
            else if(s[i][j]=='T'){
                epos.ft=i,epos.sd=j;
            }
        }
    }
    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++)vis[i][j]=0;
    }
    bfs();
}
int main()
{
    cin>>t;
    while(t--)solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值