前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UVA-12260-Free Goodies

UVA-12260-Free Goodies

作者头像
f_zyj
发布2018-01-09 10:37:39
4430
发布2018-01-09 10:37:39
举报

ACM模版

描述

描述
描述
描述
描述

题解

image.png
image.png

代码

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_LEN_NAME = 11;
const int MAXN = 1010;

struct Node
{
    int x, y;
    bool operator < (const Node &rhs) const
    {
        if (x != rhs.x)
        {
            return x > rhs.x;
        }
        return y < rhs.y;
    }
} arr[MAXN];

int n;
int dp[MAXN][MAXN >> 1];
int cost[MAXN][MAXN >> 1];
char name[MAX_LEN_NAME];

int main()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        scanf("%d%s", &n, name);

        int sum = 0;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%d", &arr[i].x, &arr[i].y);
            sum += arr[i].x;
        }

        sort(arr + 1, arr + 1 + n);

        memset(dp, 0, sizeof(dp));
        memset(cost, 0, sizeof(cost));

        int cur = 0;
        int i = name[0] == 'P' ? 2 : 1;
        for (; i <= n; ++i)
        {
            ++cur;
            int t = (cur + 1) >> 1;
            for (int j = 1; j <= t; ++j)
            {
                int &ans = dp[i][j] = dp[i - 1][j];
                cost[i][j] = cost[i - 1][j];

                if (j == 1 || dp[i - 1][j - 1])
                {
                    int tmp = dp[i - 1][j - 1] + arr[i].y;
                    if (tmp > ans)
                    {
                        ans = tmp;
                        cost[i][j] = cost[i - 1][j - 1] + arr[i].x;
                    }
                    else if (tmp == ans)
                    {
                        cost[i][j] = min(cost[i][j], cost[i - 1][j - 1] + arr[i].x);
                    }
                }
            }
        }

        printf("%d %d\n", sum - cost[n][(cur + 1) >> 1], dp[n][(cur + 1) >> 1]);
    }

    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年10月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 描述
  • 题解
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档