sgu-256 Balloons

题目大意:

有M(M<=100)个气球,有N(N<=10)个人可以吹气球,但是只有一个工具,对于每个人i,有两个参数:

Ai(Ai<=10)?一分钟可以吹Ai个气球,Bi(Bi<=4)?吹完一分钟后需要休息Bi分钟才能继续吹。

问吹完M个气球最少要多少分钟。

解题思路:

观察发现Bi<=4,那么显然我们可以开一个F[sum][a1][a2][a3][a4]表示还有sum个气球,前一分钟是a1在吹,前两分钟是a2在吹,以此类推。

然后就很好做了。直接记忆化搜索就可以AC了。

AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;

int F[102][11][11][11][11]={{{{{0}}}}};

int N,M;
int A[11]={0};
int B[11]={0};

int done(int sum,int a1,int a2,int a3,int a4)
{
    if(sum<=0) sum=0;
    if(F[sum][a1][a2][a3][a4]!=0x3f3f3f3f) return F[sum][a1][a2][a3][a4];
    if(sum==0)
        F[sum][a1][a2][a3][a4]=0;
    else
    {
        int flag=0;
        for(int i=1;i<=N;i++)
        {
            if(a1==i && B[i]>0) continue;
            if(a2==i && B[i]>1) continue;
            if(a3==i && B[i]>2) continue;
            if(a4==i && B[i]>3) continue;
            flag=1;
            int gg=done(sum-A[i],i,a1,a2,a3);
            F[sum][a1][a2][a3][a4]=min(F[sum][a1][a2][a3][a4],gg+1);
        }
        if(flag==0)
        {
            int gg=done(sum,0,a1,a2,a3);
            F[sum][a1][a2][a3][a4]=min(F[sum][a1][a2][a3][a4],gg+1);
        }
    }
    return F[sum][a1][a2][a3][a4];
}

int main()
{
    scanf("%d%d",&M,&N);
    memset(F,0x3f3f3f3f,sizeof(F));
    for(int i=1;i<=N;i++)
        scanf("%d%d",&A[i],&B[i]);
    printf("%d",done(M,0,0,0,0));
    return 0;
}
时间: 2024-10-20 16:43:30

sgu-256 Balloons的相关文章

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

ACM: SGU 101 Domino- 欧拉回路-并查集

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl

LeetCode OJ:Burst Balloons(击破气球)

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r

053第256题

256.Sales details are being stored on a daily basis in the SALES_2007 table. A large amount of data is added to the table daily. To save disk space, you issued the following command: ALTER TABLE sales_2007 COMPRESS FOR ALL OPERATIONS; What would be t

SGU 116 Index of super-prime 数论+完全背包+输出方案

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=116 题意好晦涩 给你一个不超过一万的数 问它最少可以用多少个“超级素数”来表示 使“超级素数”之和等于它 如果无法这样表示 输出0 否则 按非降序形式输出方案 数论部分就模板的问题 没什么说的 完全背包方面也很常规 说说[输出方案] 背包九讲的伪码给的是二维dp[]的方法 实际上稍加改动就可以用在一维数组上 用一个rec[]记录dp[]的当前状态是从哪个状态转移而来(即上一个状态) 通过

SGU 乱搞日志

SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注意自环,连通 1 //sgu101 2 #include<iostream> 3 #include<cstdio> 4 #include <math.h> 5 #include<algorithm> 6 #include<string.h> 7 #i

SGU 275 To xor or not to xor (高斯消元)

题目地址:SGU 275 首先,贪心的思想,每一二进制位上要尽量是1,而能不能是1用高斯消元来解决.当该位有一个可以使之为1的变元时,就说明这位可以为1,而且令该变元控制该位,然后向低位消元. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

SGU 221.Big Bishops(DP)

题意: 给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数. Solution : 同SGU 220,加个高精度就好了. code #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> using namespace std; string f[2][250][250], ans;

SGU 193.Chinese Girls&#39; Amusement

/* 实际上就是求一个k,满足k<=n/2,且gcd(n,k)=1 如果n为奇数,k为[n/2] 如果n为偶数,k=n/2-1-(n/2)%2 */ #include <iostream> using namespace std; string s; void div2() { string t; int l = s.size() - 1, tem = s[0] - '0'; if (tem > 1) t += '0' + tem / 2; tem &= 1; for (i

sgu 495 Kids and Prizes

计算出每个人得到礼物的概率,然后加起来即可 1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 #include<stdio.h> 5 using namespace std; 6 double dp[101010]; 7 int main(){ 8 int n,m; 9 while(cin>>n>>m){ 10 dp[1]=1; 11 for(int i