牛客练习赛20

A题  可以dp两次  分别是两种价格  也可以把n m种商品合在一块 

#include 
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define sz size()
#define cl clear()
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164
typedef long long LL;
typedef pair pr;
const int inf = 99999999;
const double eps = 1e-8;
const int dir4[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
const int dir8[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int maxn = 2e5 + 10;
//const int mod = 1e9 + 7;
 
int dp[1005];
int v[2005];
int main()
{
    int t;
    int n, m, k, mod;
    cin >> t;
    while(t --)
    {
        //cout << endl;
        mset(dp,0);
        dp[0] = 1; 
        cin >> n >> m >> k >> mod;
         
        for(int i = 1;i <= n;i ++)
        {
            v[i] = 1;
        }
        for(int i = n + 1;i <= n + m;i ++)
        {
            v[i] = 2;
        }
         
         
        for(int i = 1;i <= n + m;i ++)
        {
            for(int j = v[i];j <= k;j ++)
            {
                dp[j] += dp[j - v[i]];
                dp[j] %= mod;
            }  
        }
        cout << dp[k] % mod << endl;
    }
    return 0;
}


E题  模拟

#include 
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define sz size()
#define cl clear()
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164
typedef long long LL;
typedef pair pr;
const int inf = 99999999;
const double eps = 1e-8;
const int dir4[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
const int dir8[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;

int a[100005];
int my[3];
	
int main() 
{
    int n;
    cin >> n;
    for(int i = 0;i < n;i ++)
    {
    	cin >> a[i];
	}
	
	bool flag = true;
	
	for(int i = 0;i < n;i ++)
    {
    	if(a[i] == 25)
    	{
    		my[0] ++;
		}
		else if(a[i] == 50)
		{
			my[1] ++;
			my[0] --;
			
		}
		else
		{
			if(my[1] > 0)
			{
				my[1] --;
				my[0] --;
				my[2] ++;
			}
			else
			{
				my[0] -= 2;
			}
		}
		if(my[0] < 0 || my[1] < 0)
		{
			flag = false;
			break;
		}
	}
	
	if(flag)
	cout << "YES" << endl;
	else 
	cout << "NO" << endl;
    return 0;
}


F题 模拟   注意可能存在的情况是  金币会有剩余  所有要从数字大的开始遍历  

#include 
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define sz size()
#define cl clear()
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164
typedef long long LL;
typedef pair pr;
const int inf = 99999999;
const double eps = 1e-8;
const int dir4[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
const int dir8[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int maxn = 2e5 + 10;
//const int mod = 1e9 + 7;

int a[10];
int n;

int main() 
{
    cin >> n;
    
    int tm = inf;
    
	for(int i = 1;i <= 9;i ++)
    {
    	cin >> a[i];
    	tm = min(tm,a[i]);
	}
	
	if(n < tm) 
	{
		cout<< -1;
		return 0;
	}
	
	int len = n / tm, r = n % tm;
	
	for(int i = 1;i <= len;i ++)
	{
		for(int j = 9;j >= 1;j --)
		{
			if(a[j] - tm <= r)
			{
				cout << j;
				r -= a[j] - tm;
				break;
			}
		}
	}
    return 0;
}


你可能感兴趣的:(牛客网)