【Codeforces Round #569 (Div. 2) A - E】解题报告

Codeforces Round #569 解题心得分享
本文分享了参与Codeforces Round #569比赛时对A到E题目的解题思路和经验。A题关键在于理解菱形阶数与边数的关系;B题解决策略是找到偶数个负数以最大化乘积;C题通过观察规律避免循环;D题通过构造不同向量完成路径覆盖;E题涉及动态调整价格和金钱区间,寻找最后购买的最贵菜品。

CF569A
题意 定义第一阶菱形是一个正方形 n阶就是n-1阶菱形表面有多少个边加多少个
一开始没看懂题 以为中间是正方形 盲猜了一发 n==1?1:(2n-2)(2*n-2)+4;
后来发现其实规律是 ans[i] = ans[i-1]+(i-1)*4
因为每次多贡献四个边

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#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>
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;
const ll mod =  (int)1e9+7;

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);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
int ans[500];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    ans[1] = 1;
    for(int i = 2;i<=100;++i)
    {
        ans [i] = ans[i-1]+(i-1)*4;
    }
    printf("%d\n",ans[n]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

cf569B
这题也蛮有意思
给你n长度的数组 你可以把一项 arr[i] 变为 -arr[i] - 1
问你乘积最大的时候数组长啥样
做法不难发现一个数只有是负数的时候绝对值最大
所以我们取偶数个负数使得他绝对值最大而且又为正数
我们首先把数组所有元素转换成负数 因为-arr[i]-1保证负数存在
如果一个数组是偶数长度 我们直接取所有负数
否则我们取 n-1 个负数 并且让绝对值最大的负数变成正数
举个例子 -6 转换一下是 5
那么你少乘一个6变成乘一个5
答案变成原答案绝对值 5/6
但是你 -2 转换一下是1
少乘一个2变成乘一个1
答案变成原答案绝对值 1/2 显然亏了
所以把最小的负数转换即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#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>
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;
const ll mod =  (int)1e9+7;

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);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
const int MAX_N = 100025;
int arr[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i)
        scanf("%d",&arr[i]);
    if(n%2==0)
    {
        for(int i = 1;i<=n;++i)
            if(arr[i]>=0) arr[i] = -arr[i]-1;
    }
    else
    {
        for(int i = 1;i<=n;++i)
            if(arr[i]>=0) arr[i] = -arr[i] - 1;
        int minn = 1,ck;
        for(int i = 1;i<=n;++i)
        {
            if(arr[i]<minn)
            {
                minn = arr[i];
                ck = i;
            }
        }
            arr[ck]=-arr[ck]-1;
    }
    for(int i = 1;i<=n;++i)
        i==n?printf("%d\n",arr[i]):printf("%d ",arr[i]);

    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


CF569C
其实这题是找规律做的
就是前n操作不会造成循环 后面n-1会形成循环 开数组记录就行

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#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>
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;
const ll mod =  (int)1e9+7;

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);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
const int MAX_N = 100025;
int STA[MAX_N],END[MAX_N],STA_[MAX_N],END_[MAX_N],arr[MAX_N],ANS[MAX_N][3],ANS_[MAX_N][3];
queue<int > q,q_;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;++i)
    {
        scanf("%d",&arr[i]);
        if(i!=1&&i!=n)
            q.push(arr[i]);
    }
    STA[0] = arr[1];
    END[0] = arr[n];

    for(int i = 1;i<=n;++i)
    {
        int tmp = STA[i-1];
        int tmp_ = q.front();
        q.pop();
        q.push(END[i-1]);
        ANS[i][0] = tmp;
        ANS[i][1] = tmp_;
        if(tmp>tmp_)
        {
            STA[i] = tmp;
            END[i] = tmp_;
        }
        else
        {
            STA[i] = tmp_;
            END[i] = tmp;
        }
    }
    STA_[0] = STA[n];
    END_[0] = END[n];
    while(!q.empty())
    {
        int tmp = q.front();
        q.pop();
        q_.push(tmp);
    }

    for(int i = 1;i<n;++i)
    {
        int tmp = STA_[i-1];
        int tmp_ = q_.front();
        q_.pop();
        q_.push(END_[i-1]);
        ANS_[i][0] = tmp;
        ANS_[i][1] = tmp_;
        if(tmp>tmp_)
        {
            STA_[i] = tmp;
            END_[i] = tmp_;
        }
        else
        {
            STA_[i] = tmp_;
            END_[i] = tmp;
        }
    }
    if(n==2)
    {
        for(int i = 1;i<=m;++i)
        {
            ll ask;
            scanf("%lld",&ask);
            if(ask==1)
            {
                printf("%d %d\n",arr[1],arr[2]);
            }
            else
            {
                printf("%d %d\n",max(arr[1],arr[2]),min(arr[1],arr[2]));
            }
        }
    }
    else
    {
        for(int i = 1;i<=m;++i)
        {
            ll ask;
            scanf("%lld",&ask);
            if(ask<=n)
            {
                printf("%d %d\n",ANS[ask][0],ANS[ask][1]);
            }
            else
            {
                ask-=n;
                ask%=(n-1);
                if(ask==0) ask = n-1;
                printf("%d %d\n",ANS_[ask][0],ANS_[ask][1]);
            }
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

CF569D
题意就是让你用不同的向量(dx,dy)
x+dx,y+dy走路使得每一个格子都被走
构造题
用左右横跳来构造答案 1,1 -> n,m -> 1,2 -> n,m-1 -> … -> n,1
如果n为奇数最后一行也要按照上下横跳顺序解决

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#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>
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;
const ll mod =  (int)1e9+7;

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);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n/2;++i)
    {
        for(int j = 1;j<=m;++j)
        {
            printf("%d %d\n",i,j);
            printf("%d %d\n",n-i+1,m-j+1);
        }
    }
    if(n%2==1)
    {
        int down = 1,up = m;
        for(int i = 1;i<=m;++i)
        {
            if(i%2==1)
            {
                printf("%d %d\n",n/2+1,down++);
            }
            else
            {
                printf("%d %d\n",n/2+1,up--);
            }
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

CF1180E
给你 n,m,q
n长度的arr数组 有n道菜的价格
m长度的brr数组 有m个人的钱
q长度的 opt数组,x数组,y数组
如果opt 是 1 把第x个菜价格改成 y
如果opt 是 2 把 第x个人钱改成 y
大家排队买 如果能买就买能买的最贵的
问你一个人他最后等大家买完了再买能买到最贵的是什么

我们这样想 把可行域放在数轴上
如果有一道菜 就把 1 - arr[i] 区间加 1 代表这里有食物
有一个人 代表 1 - brr[i] 区间减 1 代表能买

所以问的就是1 - N最大的maxx[i]等于 0 的下标
离散化一下即可求出来

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#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>
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;
const ll mod =  (int)1e9+7;

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);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 300025;
int arr[MAX_N],brr[MAX_N],tmp[MAX_N*3],opt[MAX_N],x[MAX_N],y[MAX_N];
const int N = MAX_N*3;
int Max(int a,int b){return a>b?a:b;}
map<int ,int >mp;
namespace sgt
{
    #define mid ((l+r)>>1)
    int maxx[N<<2],col[N<<2];
    void up(int rt)
    {
        maxx[rt] = Max(maxx[rt<<1],maxx[rt<<1|1]);
    }
    void down(int rt,int l,int r)
    {
        if(col[rt])
        {
            col[rt<<1]+=col[rt];
            col[rt<<1|1]+=col[rt];
            maxx[rt<<1]+=col[rt];
            maxx[rt<<1|1]+=col[rt];
            col[rt] = 0 ;
        }
    }
    void update(int rt,int l,int r,int x,int y,int v)
    {
        if(x<=l&&r<=y)
        {
            col[rt]+=v;
            maxx[rt]+=v;
            return ;
        }
        down(rt,l,r);
        if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
        else if(y<=mid) update(rt<<1,l,mid,x,y,v);
        else update(rt<<1,l,mid,x,y,v),update(rt<<1|1,mid+1,r,x,y,v);
        up(rt);
    }
    int query(int rt,int l,int r,int x,int y)
    {
        if(maxx[rt]<=0) return -1;
        if(l==r) return l;
        down(rt,l,r);
        int ans = -1;
        if(maxx[rt<<1|1]>0&&mid<y) ans = query(rt<<1|1,mid+1,r,x,y);
        if(ans==-1&&x<=mid) ans = query(rt<<1,l,mid,x,y);
        return ans;
    }
    #undef mid
}


int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m,q,cnt = 0;
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),tmp[++cnt] = arr[i];
    for(int i = 1;i<=m;++i) scanf("%d",&brr[i]),tmp[++cnt] = brr[i];
    scanf("%d",&q);
    for(int i = 1;i<=q;++i)
    {
        scanf("%d%d%d",&opt[i],&x[i],&y[i]);
        tmp[++cnt] = y[i];
    }
    sort(tmp+1,tmp+1+cnt);
    int sz = unique(tmp+1,tmp+1+cnt)-tmp-1;
    for(int i = 1;i<=n;++i) {int t = arr[i];arr[i] = lower_bound(tmp+1,tmp+1+sz,arr[i]) - tmp;mp[arr[i]] = t;}
    for(int i = 1;i<=m;++i) {int t = brr[i];brr[i] = lower_bound(tmp+1,tmp+1+sz,brr[i]) - tmp;mp[brr[i]] = t;}
    for(int i = 1;i<=q;++i) {int t = y[i];y[i] = lower_bound(tmp+1,tmp+1+sz,y[i]) - tmp;mp[y[i]] = t;}
    for(int i = 1;i<=n;++i)
        sgt::update(1,1,N,1,arr[i],1);
    for(int i = 1;i<=m;++i)
        sgt::update(1,1,N,1,brr[i],-1);
    for(int i = 1;i<=q;++i)
    {
        if(opt[i]==1)
        {
            sgt::update(1,1,N,1,arr[x[i]],-1);
            arr[x[i]] = y[i];
            sgt::update(1,1,N,1,arr[x[i]],1);
            if(sgt::query(1,1,N,1,N)==-1) printf("-1\n");
            else printf("%d\n",mp[sgt::query(1,1,N,1,N)]);
        }
        else
        {
            sgt::update(1,1,N,1,brr[x[i]],1);
            brr[x[i]] = y[i];
            sgt::update(1,1,N,1,brr[x[i]],-1);
            if(sgt::query(1,1,N,1,N)==-1) printf("-1\n");
            else printf("%d\n",mp[sgt::query(1,1,N,1,N)]);
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值