2018 TCO Algorithm-Round 1B 600-points题解报告

一、题目

Problem Statement

Consider the set of integers between 1 and n, inclusive, and two positive integers c and k. You want to build an ordered list of k pairs (x1, y1), (x2, y2), … (xk, yk) such that the following conditions are met.
1 ≤ xi < yi ≤ n for all i between 1 and k, inclusive.
yi < xi+1 for all i between 1 and k-1, inclusive.
(xi+1 + yi+1) - (xi + yi) = c for all i between 1 and k-1, inclusive.
If a list of pairs satisfies all of the above conditions, the list is said to be stable. For any fixed n, c, and k, a stable list of k pairs is said to be maximal if its sum of elements (the sum of all 2k integers it contains) is the largest among all stable lists of k pairs.
For instance, consider n=5, c=4, and k=2. There are two stable lists of pairs: one is [(1, 2), (3, 4)] and the other is [(2, 3), (4, 5)]. The latter is the only maximal stable list of pairs in this example as its sum is (2+3+4+5) = 14.

Given n, c, and k, find one maximal stable list of pairs, and return a vector that describes the list. Specifically, if (x1, y1), (x2, y2), …, (xk, yk) is your maximal stable list of pairs, you must return { x1, y1, x2, y2, …, xk, yk }.
If there are many maximal stable lists of pairs, you may return any one of them. If no stable list of pairs with the desired properties exists, you must return an empty vector .

Definition

Class: StablePairsDiv1
Method: findMaxStablePairs
Parameters: int, int, int
Returns: vector
Method signature:vector findMaxStablePairs(int n, int c, int k)
(be sure your method is public)

Limits

Time limit (s): 2.000
Memory limit (MB): 256

Constraints

  • n will be between 1 and 100, inclusive.
  • c will be between 1 and 100, inclusive.
  • k will be between 1 and 100, inclusive.

Examples

0)
5
4
2
Returns: {2, 3, 4, 5 }
This example was described in the problem statement.

1)
4
4
2
Returns: {1, 2, 3, 4 }

2)
1
100
1
Returns: { }
When n=1, regardless of c and k, there is no way to build a pair.

3)
3
100
1
Returns: {2, 3 }
With k=1 we are looking for stable lists that only contain a single pair of numbers. There are three stable lists: [(1, 2)], [(1, 3)], and [(2, 3)]. Obviously, the last one is the only maximal one in this case.

4)
10
6
3
Returns: {2, 5, 6, 7, 9, 10 }

5)
12
7
3
Returns: {4, 5, 6, 10, 11, 12 }

二、分析

本题比较简单,按从大到小的顺序把数字放到vector中,这样可以保证和最大。最后再把vector的元素顺序反转即可

三、程序

#include <iostream>
#include <vector>
#include<algorithm>

using namespace std;

class StablePairsDiv1
{
public:
    vector<int> findMaxStablePairs(int n, int c, int k)
    {
        vector<int> res;
        if(1 == k)
        {
            res.push_back(n - 1);
            res.push_back(n);

            if(1 == n)
            {
                res.clear();
            }

            return res;
        }

        if(c < 4)
        {
            // 结果必然为空,比如n=4,c=3,k=2
            return res;
        }

        res.push_back(n);
        res.push_back(n - 1);

        for(int i = 0; i < k - 1; ++i)
        {
            int sum = res.back() + res[res.size() - 2] - c;
            int x = sum / 2;
            int y = sum - x;

            while(x == y)
            {
                --x;
                ++y;
            }

            res.push_back(y);
            res.push_back(x);
        }

        if(res.back() <= 0)
        {
            res.clear();
        }

        reverse(res.begin(), res.end());
        return res;
    }
};

int main()
{
    StablePairsDiv1 s;
    vector<int> ans = s.findMaxStablePairs(4, 3, 2);
    vector<int>::iterator it;
    for(it = ans.begin(); it != ans.end(); it++)
    {
        cout << *it;
    }

    return 0;
}


TopCoder & Codeforces & AtCoder交流QQ群:648202993
更多内容请关注微信公众号
wechat_public_header.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值