题解:P16480 [GKS 2014 #A] Cut Tiles

· · 题解

link

Solution

首先贪心一下,显然先选大的更优。

所以一种方法就是把 M \times M 的大正方形分成二的幂次的正方形,设现在有 cnt_i 个 2^i \times 2^i 的正方形。

为了满足先选大的的贪心,加入一块的时候要从大到小加,所以当 i<30 时,cnt_i \gets cnt_i + \lfloor \dfrac{m}{2^i} \rfloor^2 - 4 \times \lfloor \dfrac{m}{2^{i+1}} \rfloor^2,因为要先加大的。

如果要一个大小为 2^i \times 2^i 的正方形,可以找第一个不比它小的的正方形 2^j \times 2^j,如果没有,就再加一个 m \times m 的正方形,总数加一。如果有,那么就从 2^j \times 2^j 的正方形里挖出一个 2^i \times 2^i 的,变成 \sum\limits_{k=i}^{j-1} 3 \times 2^k \times 2^k,直接修改 cnt 就可以了。

Code

:::success[code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=501,maxm=32;
const i128 Inf=1e35;
ll t,s[maxn],n,m,cnt[maxm],ans;
void work(ll tc)
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>s[i];
    sort(s+1,s+n+1,greater<ll>());
    ans=0;
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++)
    {
        ll now=s[i];
        while(now<=30&&cnt[now]==0) now++;
        if(now<=30)
        {
            cnt[now]--;
            for(int j=s[i];j<now;j++) cnt[j]+=3;
        }
        else
        {
            ans++;
            for(int i=0;i<=30;i++) cnt[i]+=(m>>i)*(m>>i)-4*(i<30?(m>>(i+1))*(m>>(i+1)):0);
            cnt[s[i]]--;
        }
    }
    cout<<"Case #"<<tc<<": "<<ans<<"\n";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    for(int i=1;i<=t;i++) work(i);
}

:::