• 【t057】任务分配


    Time Limit: 1 second
    Memory Limit: 128 MB

    【问题描述】

    现有n个任务,要交给A和B完成。每个任务给A或给B完成,所需的时间分别为ai和bi。问他们完成所有的任务至少要多少时间。
    【输入格式】

    第一行一个正整数n,表示有n个任务。
    接下来有n行,每行两个正整数ai,bi。

    【输出格式】

    一个数,他们完成所有的任务至少要的时间。
    【输入输出样例解释】
    A完成任务1和任务2,时间为11。B完成任务3,时间为12。
    或者 A完成任务1和任务3,时间为12。B完成任务2,时间为11。
    【限制】
    30%的数据满足:1 <= n <= 20
    100%的数据满足:1 <= n <= 200 , 1 <= ai,bi <=200
    Sample Input

    3
    5 10
    6 11
    7 12

    Sample Output

    12

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t057

    【题解】

    设f[i][j]表示前i个任务,a机器花了j时间,b机器花的时间的最小值;
    即f[i][j]表示的是B机器花了多少时间;

    f[i][j] = min(f[i-1][j]+b[i],f[i-1][j-a[i]]);
    其中前一个表示第i个任务用b机器搞,后一个表示用a机器搞;
    比较奇葩的状态转移方程.

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    void rel(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void rei(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    const int MAXN = 200+10;
    const int INF = 0x3f3f3f3f;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int f[MAXN][MAXN*MAXN];
    int n;
    int a[MAXN],b[MAXN];
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        rep1(i,1,n)
            rei(a[i]),rei(b[i]);
        memset(f,INF,sizeof f);
        f[0][0] = 0;
        int now = 0;
        rep1(i,1,n)
        {
            now += a[i];
            rep1(j,0,now)
            {
                if (f[i-1][j]+b[i]<f[i][j])
                    f[i][j] = f[i-1][j]+b[i];
                if (j>=a[i])
                    if (f[i][j]>f[i-1][j-a[i]])
                        f[i][j] = f[i-1][j-a[i]];
            }
        }
        int ans = INF;
        rep1(i,0,now)
            ans = min(ans,max(i,f[n][i]));
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    使用活字格搭建企业级web应用--办公用品管理系统
    怪兽级性能,用代码玩转Excel!葡萄城强势发布Spread表格组件
    无需编码,轻松搭建企业采购管理软件
    NHibernate变的简单
    Easyui + jQuery表单提交 给 Controller patr1
    Easyui datagrid 批量编辑和提交
    C# 网络编程 Part.1
    提高你开发效率的十五个Visual Studio 2010使用技巧
    System.IO
    C#路径,文件,目录,I/O常见操作
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626860.html
Copyright © 2020-2023  润新知