Hackerrank Equal(这特么也是dp???)

作者在军训期间通过编程来打发时间,分享了一段C++代码实现,该代码用于解决一个具体的数学问题,通过预处理和动态规划技巧来优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

咸鱼的等待着军训,哎呀真的很烦,没有事情可做.
随便玩玩咯.
印度人很恶心,喜欢各种trick.

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long  LL;
typedef unsigned long long ULL;
const int MAXN = 10000+17;
const int MAXM = 20;
const int INF = 0x7fffffff;
const int MOD = 1e9+7;
LL a[MAXN],dp[1000+17];
int main()
{
    #ifndef ONLINE_JUDGE 
    FFF
    #endif
    int t;
    cin>>t;
    while(t--)
    {
        memset(dp, 0, sizeof(dp));
        LL n,mn=INF;
        cin>>n;
        for (int i = 0; i < n; ++i)
        {
            scanf("%lld",a+i);
            mn = min(a[i],mn);
        }
        LL h[10] = {0};
        for (int i = 0; i < n; ++i)
        {
            a[i]-=mn;
            int temp = a[i]/5;
            h[5]+=temp;
            a[i]-=temp*5;
            h[a[i]]++;
        }
        dp[mn] = h[1]+h[2]+2*h[3]+2*h[4]+h[5];
        LL ans = min(dp[mn],1LL*INF);
        LL have = h[5];
        for (int i = mn-1; i > mn-10; --i)
        {
            have += h[4];
            int temp = h[0];
            h[0] = h[4];
            h[4] = h[3];
            h[3] = h[2];
            h[2] = h[1];
            h[1] = temp;
            LL zz = (h[1]+h[2]+2*h[3]+2*h[4])+have;
            ans = min(ans,zz);
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值