跳到正文

题解

题解:P12712 [KOI 2021 Round 1] 超矩形

ZBH2015约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年11月16日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 10 分钟
题目
P12712

版本

  1. v1当前

最优解前来报到

一眼是个贪心。

思路:本题选取的卡片顺序无关,且 UU 都是正整数,要从大往小选,而每次从四种候选卡片中选贡献最大的那个,如果选的不是贡献最大的那个,就不是最优解。

注:下文的 a0a_0、b0b_0、c0c_0、d0d_0 指每个变量的当前值,初始时等于输入给出的 A0A_0、B0B_0、C0C_0、D0D_0,选中某张牌后累加进去。

每次贪心比较四个式子的大小来更新:

(a0+a1)×b0×c0×d0(a_0+a_1) \times b_0 \times c_0 \times d_0 a0×(b0+b1)×c0×d0a_0 \times (b_0+b_1) \times c_0 \times d_0 a0×b0×(c0+c1)×d0a_0 \times b_0 \times (c_0+c_1) \times d_0 a0×b0×c0×(d0+d1)a_0 \times b_0 \times c_0 \times (d_0+d_1)

一定爆 long long,考虑优化。

我们将所有式子除以 a0×b0×c0×d0a_0 \times b_0 \times c_0 \times d_0,得到:

1+a1/a01+a_1/a_0 1+b1/b01+b_1/b_0 1+c1/c01+c_1/c_0 1+d1/d01+d_1/d_0

四个式子都带一个公共的 11,同时消掉它,就只需要比较:

a1/a0a_1/a_0 b1/b0b_1/b_0 c1/c0c_1/c_0 d1/d0d_1/d_0

防止精度被卡,用 long double。

记得开 long long。

复杂度:O(nlog⁡n)O(n \log n),能过。

最后放个代码:

C++
//#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define ot float
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define in __int128
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=1e6+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
ll n,k,m;
vector<ll> num[4];
ll a0,b0,c0,d0;
ll posa,posb,posc,posd;
vector<char> ans1;
vector<ll> ans2;
inline void pre(){
    
}
inline void input(){
    n=read(),k=read();m=k;
    a0=read(),b0=read(),c0=read(),d0=read();
    F(i,1,n,1){
        string tmp=rs();
        ll x=read();
        num[tmp[0]-'A'].push_back(x);
    }
    F(i,0,3,1) sort(num[i].begin(),num[i].end(),greater<ll>());
    vector<pair<ld,ll>> now;
    W(m--){
        now.clear();
        ll a1,b1,c1,d1;
        if(posa<num[0].size()) a1=num[0][posa];
        if(posb<num[1].size()) b1=num[1][posb];
        if(posc<num[2].size()) c1=num[2][posc];
        if(posd<num[3].size()) d1=num[3][posd];
        if(posa<num[0].size()) now.push_back({(ld)(a1)/(ld)(a0),0});
        if(posb<num[1].size()) now.push_back({(ld)(b1)/(ld)(b0),1});
        if(posc<num[2].size()) now.push_back({(ld)(c1)/(ld)(c0),2});
        if(posd<num[3].size()) now.push_back({(ld)(d1)/(ld)(d0),3});
        sort(now.begin(),now.end(),greater<pair<ld,ll>>());
        pair<ld,ll> tmp=now[0];
        if(tmp.second==0){
            ans1.push_back('A');
            ans2.push_back(a1);
            a0+=a1;
            posa++;
        }else if(tmp.second==1){
            ans1.push_back('B');
            ans2.push_back(b1);
            b0+=b1;
            posb++;
        }else if(tmp.second==2){
            ans1.push_back('C');
            ans2.push_back(c1);
            c0+=c1;
            posc++;
        }else{
            ans1.push_back('D');
            ans2.push_back(d1);
            d0+=d1;
            posd++;
        }
    }
    F(i,0,k-1,1){
        pc(ans1[i]);
        pc(' ');
        write(ans2[i]);
        pc('\n');
    }
}
inline void output(){
    
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    //ios::sync_with_stdio(0);
    //cin.tie(0);cin.tie(0);
    int _=1;//_=read();
    while(_--) solve();
    flush();
    return 0;
}
原文