topcoder SRM 625 DIV2 B

题目

Problem Statement

You have a vector <int> A with N elements.

Your          goal is to change it into a vector <int> that contains each number          from 1 to N exactly once. The change will consist of zero or more          steps. In each step, you may pick an arbitrary element of A          and increment its value by k. You may pick the same element          multiple times. Note that you are not allowed to decrement the value          of any element.

You are given the int k and the vector          <int> A. Return "POSSIBLE" if it is possible to achieve          your goal. Return "IMPOSSIBLE" otherwise.

Definition

Class:IncrementingSequence
Method:canItBeDone
Parameters:int, vector <int>
Returns:string
Method signature:string canItBeDone(int k, vector <int> A)
(be sure your method is public)

Limits

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

Notes

-Return value is case-sensitive. For example, you can't return          "Possible" or "possible" instead of "POSSIBLE".

Constraints

-k will be between 1 and 10, inclusive.
-A will contain between 1 and 50 elements, inclusive.
-Each element of A will be between 1 and 50, inclusive.

Examples

0)
3
                   
{1,2,4,3}
                   
Returns: "POSSIBLE"
             
This sequence of length 4 already contains all numbers                      from 1 to 4 exactly once. Note that their order does not                      matter.
1)
5
                   
{2,2}
                   
Returns: "IMPOSSIBLE"
             
2)
1
                   
{1,1,1,1,1,1,1,1}
                   
Returns: "POSSIBLE"
             
There are many ways to achieve the goal. For example, it                      is possible to obtain the sequence {1,2,3,4,5,6,7,8}. To                      do this, just increment the element at each position one                      by one until it reaches the required value.
3)
2
                   
{5,3,3,2,1}
                   
Returns: "IMPOSSIBLE"
             
We want to have the values {1,2,3,4,5}, in any order.                      Currently, we are missing the 4. As k=2, the only                      way to produce a 4 is by incrementing a 2. But if we                      increment our only 2, we will have no way of producing                      another 2.
4)
9
                   
{1,2,3,1,4,5,6,7,9,8}
                   
Returns: "POSSIBLE"
             
5)
2
                   
{1,1,1,1,1,1,2,2,2,2,2,2}
                   
Returns: "POSSIBLE"
             
6)
1
                   
{1}
                   
Returns: "POSSIBLE"
             

This problem statement is the exclusive and proprietary property of      TopCoder, Inc. Any unauthorized use or reproduction of this information      without the prior written consent of TopCoder, Inc. is strictly      prohibited. (c)2003, TopCoder, Inc. All rights reserved.

分析

数据量较小使用模拟过程直接求解。

代码

/**
 * @brief code for tp
 * @author xiyan
 * @date 2014/06/20
 */
//typedef unsigned int size_t;
//typedef signed int ssize_t;
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
#define MAXN 200          /*maxvak + k*/
#define CLR(p) memset(p, 0, sizeof(p))
#define TRUESTR   "POSSIBLE"
#define FALSESTR  "IMPOSSIBLE"
class IncrementingSequence{
public:
string canItBeDone(ssize_t k, vector<int> vec){
    CLR(ansTable);
    string ans = TRUESTR;
    for(size_t pos = 0; pos < vec.size(); pos++){
            ansTable[vec[pos]]++;
    }
    for(size_t val = 1; val <= vec.size(); val++){
             if(!ansTable[val]){            /*fail*/
                    ans = FALSESTR;
                    break;
             }
             ssize_t leftNums = ansTable[val] - 1;
             ansTable[val + k] += leftNums; /*store left nums*/
    }
    return ans;
}
private:
    ssize_t ansTable[MAXN];
};



转载于:https://my.oschina.net/u/572632/blog/282574

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值