QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#759253#5137. Towerfrankly6TL 0ms0kbC++171.4kb2024-11-17 23:27:242024-11-17 23:27:25

Judging History

你现在查看的是最新测评结果

  • [2024-11-17 23:27:25]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-17 23:27:24]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int MX=505;
const int inf=0x3fffffff;

int T, N, M;
int ar[MX];
map<int,int> mp;
int read()
{
    int r=0, f=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
    return r*f;
}
int main()
{
    // freopen("testdata.in","r",stdin);
    T=read();
    while(T--)
    {
        mp.clear();
        N=read(); M=read();
        for(int i=1;i<=N;i++) 
        {
            ar[i]=read();
            int tmp=ar[i];
            while(tmp) mp[tmp]=1, tmp/=2;
        }
        int ans=inf;
        for(auto [tar,tag]:mp)
        {
            int br[MX]={}, cr[MX]={}, ta=0;
            memcpy(cr,ar,sizeof(ar));
            for(int i=1;i<=N;i++)
            {
                if(cr[i]<tar) br[i]=tar-cr[i];
                else
                {
                    while(abs(cr[i]/2-tar)>cr[i]-tar-1) cr[i]/=2, br[i]++;
                    br[i]+=abs(cr[i]-tar);
                }
                // cout << "   br[i]=" << br[i] << '\n';
            }
            sort(br+1,br+1+N);
            for(int i=1;i<=N-M;i++)
                ta+=br[i];
            // cout << "tar=" << tar << ", ta=" << ta << '\n';
            ans=min(ans,ta);    
        }
        cout << ans << '\n';
    }
    return (0-0);
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

3
2 0
2 6
5 0
1 2 3 4 5
5 3
1 2 3 4 5

output:


result: