美团

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiongchengluo1129/article/details/82468916

题目一:

在ACM竞赛中,一支队伍由三名队员组成,现在有N+M名学生,其中的N名学生擅长算法,剩下的M名学生擅长编程,这些学生组队打算参加ACM竞赛,
他们的教练要求每支队伍至少有一名擅长算法的学生和一名擅长编程的学生,那么这些学生最多能组成多少支队伍?

代码如下:

int main()
{
//  主要思路:求 N,M,(N+M)/3里面的最小值就可以了
    int n,m ;
    cin >> n >> m;
    int temp = (n + m) / 3;
    int result = n > m ? m : n;
    int resu = result > temp ? temp : result;
    cout << resu << endl;
    return 0;
}

题目二:

考试这题,一共120分钟,做n题,每题可以选择做或者不做或者做一部分,这道题不做的话,得0分,做一部分,花pi分钟得ai分
一道题全部做完花qi分钟,得bi分。求此次做题能得的最高分数。
其中1<=n<=100,1<=pi<=qi<=120,0<=ai<=bi<=1000
输入:
4
20 20 100 60
50 30 80 55
100 60 110 88
5 3 10 6

程序代码:

思路:有点像0-1背包问题
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<unordered_map>
#include<queue>
#include<unordered_set>
#include<map>
using namespace std;
int main() 
{
    int n;
    scanf("%d", &n);
    int p[110], a[110], q[110], b[110];
    for (int i = 0; i < n; ++i) 
    {
        scanf("%d%d%d%d",&p[i],&a[i],&q[i],&b[i]);
    }
    int min_ans[110][150] = {0};
    for (int i = 0; i < 110; ++i) 
    {
        for (int j = 0; j < 150; ++j) 
        {
            min_ans[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) 
    {
        for (int j = 0; j <= 120; ++j) 
        {
            int max = min_ans[i-1][j];
            if (j >= p[i-1]) 
            {
                int now_check = min_ans[i - 1][j - p[i-1]] + a[i-1];
                if (now_check > max) 
                {
                    max = now_check;
                }
            }
            if (j >= q[i-1]) 
            {
                int now_check = min_ans[i - 1][j - q[i-1]] + b[i-1];
                if (now_check > max)
                {
                    max = now_check;
                }
            }
            min_ans[i][j] = max;
        }
    }
    cout << min_ans[n][120];
    return 0;
}

仅做记录,就不做注释了,good night !

猜你喜欢

转载自blog.csdn.net/xiongchengluo1129/article/details/82468916