Codeforces Round #524 (Div. 2) A-D题解报告

A  向上取整即可

#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstring>
#define MP make_pair
#define PB push_back
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define rep(i,a,b) for(repType i=(a); i<=(b); ++i)
#define per(i,a,b) for(repType i=(a); i>=(b); --i)
#define ZERO(x) memset(x, 0, sizeof(x))
#define MS(x,y) memset(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define QUICKIO                  \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

using namespace std;
using pi=pair<int,int>;
using repType=int;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
int n,m;

int main()
{
    ll n,m;
    cin>>n>>m;
    ll ans=ceil((double)(n*2)/m);
    ans+=ceil((double)(n*5)/m);
    ans+=ceil((double)(n*8)/m);
    cout<<ans<<endl;

    return 0;
}

B  判断L出现的位置以及区间的长度

#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstring>
#define MP make_pair
#define PB push_back
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define rep(i,a,b) for(repType i=(a); i<=(b); ++i)
#define per(i,a,b) for(repType i=(a); i>=(b); --i)
#define ZERO(x) memset(x, 0, sizeof(x))
#define MS(x,y) memset(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define QUICKIO                  \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

using namespace std;
using pi=pair<int,int>;
using repType=int;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
int n,m;

int main()
{
    ios::sync_with_stdio(false);
    int l,r;
    int t;
    cin>>t;
    while(t--)
    {
        ll ans=0;
        cin>>l>>r;
        int flag_dan=0;
        int dlt=r-l+1;
        if(dlt%2)
        {
            dlt--;
            flag_dan=1;
        }

        if(l%2==0)
        {
            ans-=dlt/2;
        }
        else
        {
            ans+=dlt/2;
        }
        if(flag_dan==1)
        {
            if(r%2==0)
            {
                ans+=r;
            }
            else
            {
                ans-=r;
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

C

题意:黑白相见的棋盘,第一次选定一个矩形全部涂成白色, 第二次选定一个矩形全部涂成黑色,问最后白黑格子的总数

思路:矩阵的两边场均为奇数的时候会多一个白块或者黑块,当两个矩阵有重合的部分时。。先求出重合的面积然后分三步写。

重合的面积就是取:(x1,y1),(x2,y2)

        x1=max(a[1].x1,a[2].x1);
        y1=max(a[1].y1,a[2].y1);
        x2=min(a[1].x2,a[2].x2);
        y2=min(a[1].y2,a[2].y2);

画个图很快就出来了。。这个地方第一次写的时候模拟了100行。。

然后分步模拟就行

#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstring>
#define MP make_pair
#define PB push_back
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define rep(i,a,b) for(repType i=(a); i<=(b); ++i)
#define per(i,a,b) for(repType i=(a); i>=(b); --i)
#define ZERO(x) memset(x, 0, sizeof(x))
#define MS(x,y) memset(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define QUICKIO                  \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

using namespace std;
using pi=pair<int,int>;
using repType=int;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
int n,m;

struct rec
{
    ll x1,y1,x2,y2;
};

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    ll n,m;
    while(t--)
    {
        cin>>n>>m;
        ll wh,bl;
        if((n*m)%2)
        {
            wh=n*m/2+1;
            bl=n*m/2;
        }
        else
        {
            bl=wh=n*m/2;
        }
        //cout<<"step 1  "<<wh<<' '<<bl<<endl;
        rec a[3];
        cin>>a[1].x1>>a[1].y1>>a[1].x2>>a[1].y2;
        cin>>a[2].x1>>a[2].y1>>a[2].x2>>a[2].y2;
        ll l1=a[1].x2-a[1].x1+1,l2=a[1].y2-a[1].y1+1;
        if((l1*l2)%2==1)//总数为奇数
        {
            if((a[1].x1+a[1].y1)%2==0)//原来这块的白的比黑的多1
            {
                wh+=l1*l2/2;
                bl-=l1*l2/2;
            }
            else
            {
                wh+=(l1*l2/2+1);
                bl-=(l1*l2/2+1);
            }
        }
        else
        {
            wh+=l1*l2/2;
            bl-=l1*l2/2;
        }
        //cout<<"step 2  "<<wh<<' '<<bl<<endl;
        l1=a[2].x2-a[2].x1+1,l2=a[2].y2-a[2].y1+1;
        if(l1*l2%2==1)//全部涂黑
        {
            if((a[2].x1+a[2].y1)%2==0)//原来这块的白的比黑的多1
            {
                bl+=(l1*l2/2+1);
                wh-=(l1*l2/2+1);
            }
            else
            {
                bl+=(l1*l2/2);
                wh-=(l1*l2/2);
            }
        }
        else
        {
            bl+=l1*l2/2;
            wh-=l1*l2/2;
        }
        //cout<<"step 3  "<<wh<<' '<<bl<<endl;
        ll x1,y1,x2,y2;
        x1=max(a[1].x1,a[2].x1);
        y1=max(a[1].y1,a[2].y1);
        x2=min(a[1].x2,a[2].x2);
        y2=min(a[1].y2,a[2].y2);
        if(y2>=y1&&x2>=x1)
        {
            ll s=(y2-y1+1)*(x2-x1+1);
            if(s%2)
            {
                if((x1+y1)%2==1)//黑的多1
                {
                    wh--;bl++;
                    bl+=(s/2);wh-=(s/2);
                }
                else//白的多1
                {
                    wh++;bl--;
                    bl+=(s/2+1);wh-=(s/2+1);
                }
            }
            else
            {
                bl+=(s/2);wh-=(s/2);
            }
        }

        //cout<<"step 4  "<<wh<<' '<<bl<<endl;
        cout<<wh<<' '<<bl<<endl;
    }


    return 0;
}

 

D  题意:有一个边长为2^n的正方形(刚开始是一个完整的正方形),有k步操作可以走,每次操作可以选定一个正方形,然后把正方形切成右上左上右下左下4块,问可不可以划分一条路径(只能往上和往右)从左下角连到右上角,满足路径上的经过的所有小正方形的边长相等。如果可行的话输出YES 和小正方形的边长。

思路:构造题,题目可以转化为沿着左边界和上边界,要求走过的所有正方形边长相等。

如果单纯构造出一个只有左边界和上边界被切了K次的路径(其他正方形不变)

类似于下图的图形:

切一次的最小花费为1,切2次的最小花费为1+3,且3次的最小花费为1+3+7

f(n)=2*f(n-1)+1

g(n)=\sum\limits_{i=1}^{n}f_{i}就是一个边界路径上正方形边长为N-n的最小花费

而最多花费就是全部都变成1x1的正方形,总花费为cost=\frac{4^n-1}{3},可以看出在n==31的时候

最大cost已经超过k的上限了

所以n大于31的情况无论如何我们都可以构造出一个路径上边长为n-1,共有3个正方形在路径上,只对右下角的正方形进行操作。

对于n<=31的情况,我们可以分两部分求得当前路径上正方形边长为x的情况下的花费

首先为什么不能直接套最大值最小值两个公式呢。。因为想要构造出一个路径边长为x的情况,(设路径上的正方形有m个)此时必定有m-2个边长为x*2的正方形......

我的计算方法是递归计算,边长从n-1开始枚举,一旦构造当前边长的路径的最小值大于等于k或者边长变为1就结束循环

每次分两个部分相加 一部分是g(n)另一部分累加,累加所有边长为2*n的正方形

#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstring>
#define MP make_pair
#define PB push_back
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define rep(i,a,b) for(repType i=(a); i<=(b); ++i)
#define per(i,a,b) for(repType i=(a); i>=(b); --i)
#define ZERO(x) memset(x, 0, sizeof(x))
#define MS(x,y) memset(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define QUICKIO                  \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

using namespace std;
using pi=pair<int,int>;
using repType=int;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
int n,m;
ll f[50];
ll sum[50];//sum[i]表示边被切割了i次的最小次数,输出n-i
void init()
{
    f[1]=1;
    for(int i=2;i<=32;i++)
    {
        f[i]=f[i-1]*4+1;
    }
}

int main()
{
    init();
    ios::sync_with_stdio(0);
    ll n,k;
    int t;
    cin>>t;
    //
    while(t--)
    {
        cin>>n>>k;
        if(n>31)//先分一步,然后分右下角的
        {
            cout<<"YES "<<n-1<<endl;
        }
        else
        {
            ll now=1,i=0,tot=0,tot2=0;
            while(now+tot<=k&&i<n)
            {
                tot+=now;
                now=now*2+1;
                i++;
                tot2+=f[n-i]*(now-2);
            }
            if(tot2+tot>=k)
            {
                cout<<"YES "<<n-i<<endl;
            }
            else
            {
                cout<<"NO"<<endl;
            }
        }
    }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值