【Codeforces Round #526 (Div. 2)】 A B C D E

这场以前vp打过三次 不知道脑子是抽了还是干嘛 崩盘了好几次

A

题意 给你一栋n层的大楼 每层有a[i] 个人 问你选择从一层放电梯 使得最低耗费

做法 数据范围很小 暴力枚举即可 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
int arr[125];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,ans = 0x3f3f3f3f;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
    for(int i = 1;i<=n;++i)
    {
        int tmp = 0;
        for(int j = 1;j<=n;++j)
        {
            if(j<=i)
            {
                tmp+=arr[j]*(i-1)*2*2;
            }
            else
            {
                tmp+=arr[j]*(j-1)*2*2;
            }
        }
        ans = min(ans,tmp);
    }
    printf("%d\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


B

题意 给你n个 每个有a[i]容量的水桶 叫你倒到s升水 问你倒完水剩最少的水桶最大有多少 

做法 满足二分性 所以二分最小值即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 1025;
long long arr[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    long long s,minn = 0,ans=-1,sum = 0;
    scanf("%d%lld",&n,&s);
    for(int i = 1;i<=n;++i) scanf("%lld",&arr[i]),minn = max(minn,arr[i]),sum+=arr[i];
    if(sum<s)
    {
        printf("-1\n");
        return 0;
    }
    long long l = 0 ,r = minn;
    while(l<=r)
    {
        sum = 0;
        bool flag = true;
        long long mid = (l+r)>>1;
        for(int j = 1;j<=n;++j)
        {
            if(arr[j]<mid)
            {
                flag = false;
                break;
            }
            else sum+=arr[j]-mid;
        }
        if(!flag) r = mid - 1;
        else
        {
            if(sum>=s) ans = mid, l = mid  + 1;
            else r = mid - 1;
        }
    }
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}



C

题意 给你一个字符串 问你能满足一组下标使得 每个字符串下标都是a 每两个下标之间存在b

做法 我们只要把a当做联通快求出来 然后答案从左往右  到这个联通快他有本身的贡献 然后他对前面的贡献就是 ans*联通块本身的贡献 我们只要跑一遍就能求出来了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
#define MOD 1000000007
const int MAX_N = 100025;
char str[MAX_N],str_[MAX_N];
long long tmp[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int cnt = 1;
    scanf("%s",str+1);
    int len = strlen(str+1);
    for(int i = 1;i<=len;++i)
    {
        if(str[i]=='a'||str[i]=='b') str_[cnt++] = str[i];
        else continue;
    }
    str_[cnt] = '\0';
    len = strlen(str_+1);
    cnt = 1;
    int now = 0;
    for(int i = 1;i<=len;++i)
    {
        if(str_[i]=='a') now++;
        else if(now)
        {
            tmp[cnt++] = now;
            now = 0;
        }
    }
    tmp[cnt] = now;
    long long ans = 0;
    for(int i = 1;i<=cnt;++i)
    {
        ans = (ans+tmp[i] + ans*(tmp[i]%MOD))%MOD;
    }
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


D

题意 给你一颗树 树上有点 有边权

问你使得一条路径满足 经过的点权和减去边权和最大

做法 考虑树形dp

这个dp值从儿子向根传递

我们对每一层他的最大值假如路径在这个为爸爸的子树内就是dp[i] - w[儿子] 

否则会是这个爸爸为子树的一条边 和另一条边做和 我们只要用vector 排序取出两个最大的就可以更新了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 300025;
long long w[MAX_N],ans,dp[MAX_N];
vector<int > G[MAX_N];
vector<long long > val[MAX_N],tmp;
bool cmp(long long a,long long b)
{
    return a >b;
}
void dfs(int rt,int fa)
{
    dp[rt] = w[rt];
    int sz = G[rt].size();
    for(int i = 0;i<sz;++i)
    {
        int to = G[rt][i];
        if(to==fa) continue;
        long long va = val[rt][i];
        dfs(to,rt);
        dp[rt] = max(dp[rt],w[rt]+dp[to]-va);
    }
    ans = max(ans,dp[rt]);
    tmp.clear();
    for(int i = 0;i<sz;++i)
    {
        int to = G[rt][i];
        long long va = val[rt][i];
        if(to==fa) continue;
        tmp.push_back(dp[to]-va);
    }
    sort(tmp.begin(),tmp.end(),cmp);
    long long tt = w[rt];
    if(tmp.size()>=2)
    {
        if(tmp[0]>=0) tt+=tmp[0];
        if(tmp[1]>=0) tt+=tmp[1];
    }
    ans = max(ans,tt);
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,a,b;
    long long len;
    scanf("%d",&n); for(int i = 1;i<=n;++i) scanf("%lld",&w[i]);
    for(int i = 1;i<n;++i)
    {
        scanf("%d%d%lld",&a,&b,&len);
        G[a].push_back(b);
        G[b].push_back(a);
        val[a].push_back(len);
        val[b].push_back(len);
    }
    dfs(1,-1);
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


E

题意 给你两个字符串 问你字典序在他们之间的不同前缀有多少个

做法 看了学长的图然后去搞边界范围 注意最后边界可能会很大 但是你要取的不会超过k 所以就可以做了

下面是学长原话 学长博客是 lajiyuan 把ab看做01串

如果我们要查011和110之间的字符串,我们发现就是这样一个区域

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 500025;
char str[MAX_N],str_[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    long long k,ans = 0,pre = 0;
    scanf("%d%lld",&n,&k);
    scanf("%s",str+1);
    scanf("%s",str_+1);
    for(int i = 1;i<=n;++i)
    {
        pre = min(pre*2 + (int)str_[i] - str[i],1ll<<35);
        ans+=min(k,pre+1);
    }
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/88666745