HDOJ 5143 NPY and arithmetic progression

本文探讨了如何将不同数量的数字划分成多个长度至少为3的等差数列的问题。通过使用递推方法和四维数组进行预处理,文章提供了一个有效的解决方案,并附带AC代码。

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

题意:有a1个1,a2个2,a3个3,a4个4,问能否不重不漏的划分成一些长度大于等于3的等差数列(一个数列可以出现多次)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143

思路:四个数字用一个四维数组保存,利用递推对数组预处理,并且当a1,a2,a3,a4全都大于等于三时,等差数列一定存在,缩小数据范围。

注意点:无。


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
124933212014-12-14 00:15:56Accepted5143218MS1192K3918 BG++luminous11
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;

bool f[11][11][11][11];
int main()
{
    ios::sync_with_stdio( false );
    int t;
    int a1,a2,a3,a4;
    f[0][0][0][0] = true;
    for ( a1 = 0; a1 <= 10; a1 ++ )
    {
        for ( a2 = 0; a2 <= 10; a2 ++ )
        {
            for ( a3 = 0; a3 <= 10; a3 ++ )
            {
                for ( a4 = 0; a4 <= 10; a4 ++ )
                {
                    if ( a1 != 0 || a2 != 0 || a3 != 0 || a4 != 0 )
                    {
                        f[a1][a2][a3][a4] = false;
                        for ( int i = a1 - 3; i >= 0; i -- )
                        {
                            if ( f[i][a2][a3][a4] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                        for ( int i = a2 - 3; i >= 0; i -- )
                        {
                            if ( f[a1][i][a3][a4] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                        for ( int i = a3 - 3; i >= 0; i -- )
                        {
                            if ( f[a1][a2][i][a4] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                        for ( int i = a4 - 3; i >= 0; i -- )
                        {
                            if ( f[a1][a2][a3][i] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                        if ( a1 > 0 && a2 > 0 && a3 > 0 )
                        {
                            if ( f[a1-1][a2-1][a3-1][a4] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                        if ( a2 > 0 && a3 > 0 && a4 > 0)
                        {
                            if ( f[a1][a2-1][a3-1][a4-1] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                        if ( a1 > 0 && a2 > 0 && a3 > 0 && a4 > 0 )
                        {
                            if ( f[a1-1][a2-1][a3-1][a4-1] )
                            {
                                f[a1][a2][a3][a4] = true;
                                continue;
                            }
                        }
                    }
                }
            }
        }
    }
    cin >> t;
    while ( t -- )
    {
        cin >> a1 >> a2 >> a3 >> a4;
        if ( a1 > 10 ) a1 = 10;
        if ( a2 > 10 ) a2 = 10;
        if ( a3 > 10 ) a3 = 10;
        if ( a4 > 10 ) a4 = 10;
        if ( f[a1][a2][a3][a4] )
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值