465DIV2-1000 将军的时间

7 篇文章 0 订阅


Problem Statement
    
The celebrated general Archibald Waving took charge of the fourth army in the occidental front. After losing the first three armies, Waving has become obsessed with effective synchronization of the army. Whenever Waving needs to write the time at which military operations will be performed, he writes only the minute part of the time (the time also has an hour part, which Waving does not write down). This strategy prevents the enemy from figuring out the exact times of the operations even if he obtains the schedule written by Archibald Waving. Of course, Waving's soldiers are very smart, so they are able to reconstruct the entire schedule from the minute parts alone.
 
A particular time can be written as AB:CD, where AB represents the hour part, and CD represents the minute part. The schedule is a list of times within the same day, where time can lie in the range from 00:00 to 23:59. Moreover, the times appear in chronological order, i.e. the time of the operation performed earlier should be listed before the time of the operation performed later during the day. Also, no two operations can be performed at the same time.
 
Waving wants to know if the enemy will be able to figure out anything from the schedule he has written. You are given a vector <int> minuteValues representing minute parts of the times as written by Waving. An hour assignment can be represented by a vector <int> whose i-th element is the hour part corresponding to minuteValues[i]. The assignment is considered valid if and only if the schedule obtained from the assignment follows the rules mentioned above. Help Waving by returning the K-th (1-indexed) lexicographically smallest valid hour assignment. If there are less than K valid hour assignments, return a vector <int> containing single element -1 instead.
Definition
    
Class:
WeirdTimes
Method:
hourValues
Parameters:
vector <int>, int
Returns:
vector <int>
Method signature:
vector <int> hourValues(vector <int> minuteValues, int K)
(be sure your method is public)
    


Notes
-
vector <int> X is lexicographically smaller than vector <int> Y of the same size iff there exists an index i such that X[j] = Y[j], j < i, and X[i] < Y[i].
Constraints
-
minuteValues will contain between 1 and 50 elements, inclusive.
-
Each element of minuteValues will be between 0 and 59, inclusive.
-
K will be between 1 and 1,000,000,000, inclusive.
Examples
0)


    
{22, 11, 33}
3
Returns: {0, 1, 3 }
The three lexicographically smallest valid hour assignments and the corresponding schedules obtained from them are:
{0, 1, 1} ---> {00:22, 01:11, 01:33}
{0, 1, 2} ---> {00:22, 01:11, 02:33}
{0, 1, 3} ---> {00:22, 01:11, 03:33}
1)


    
{10}
2
Returns: {1 }
All possible schedules in lexicographically sorted order are
00:10, 01:10, 02:10, 03:10, 04:10, 05:10, 06:10, 07:10,
08:10, 09:10, 10:10, 11:10, 12:10, 13:10, 14:10, 15:10, 
16:10, 17:10, 18:10, 19:10, 20:10, 21:10, 22:10, 23:10.
2)


    
{2, 1}
20
Returns: {0, 20 }


3)


    
{1, 2}
20
Returns: {0, 19 }


4)


    
{25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 15, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
1
Returns: {-1 }
In the list of minute parts, each next element is less than or equal to the previous one. Operations must follow in chronological order and no two operations can be performed at the same time, so no two listed operations can happen within the same hour. There are 25 elements in the list, but a single day consists of just 24 hours, therefore there are no valid hour assignments.
5)


    
{45, 12, 0, 3, 2, 7, 4, 9, 23, 6, 17, 33}
12345
Returns: {0, 1, 2, 2, 3, 3, 4, 5, 12, 13, 18, 18 }


6)


    
{43, 58}
318
Returns: {-1 }

class WeirdTimes
{
public:
	vector<int> hourValues( vector<int> minuteValues, int K )
	{
		return hourValuesRecur( minuteValues, K);
	}
private:
	vector<int> hourValuesRecur( vector<int> minuteValues, int K)
	{
		if( K == 1 )
		{
			return findMinimum( minuteValues, vector<int>() );
		}
		else
		{
			vector<int> lastResult = hourValuesRecur( minuteValues, K - 1 );
			if( lastResult[0] == -1 )
			{
				 return lastResult;
			}
			int idx = lastResult.size() - 1;
			while( idx >= 0 && lastResult[idx] == 23 ) idx--;
			if( idx >= 0 )
			{
				vector<int>::const_iterator first = lastResult.begin();
				vector<int>::const_iterator last = lastResult.begin() + idx + 1;
				vector<int> newVec(first, last);
				newVec[ idx ] += 1;	
				return findMinimum( minuteValues, newVec );
			}			
			return vector<int>(1, -1);
		}
	}
	vector<int> findMinimum(  vector<int> minuteValues, vector<int> input )
	{
		int idx = 1;
		vector<int> result;
		if( input.size() == 0 )
		{
			result.push_back( 0 );
		}
		else
		{
			result = input;
			idx = input.size();
		}
		while( idx < minuteValues.size() )
		{
			if( minuteValues[idx] > minuteValues[idx-1] )
			{
				result.push_back( result[idx - 1] );
			}
			else
			{
				if( result[idx-1] < 23 )
				{
					result.push_back( result[idx-1] + 1 );
				}
				else
				{
					return vector<int>(1, -1);
				}
			}
			idx++;
		}
		return result;
	}


	vector<int> hourValues( vector<int> minuteValues, int K )
	{
		vector< vector<int> > currentList, lastList;
		
		for( int k = 0; k <= K && k < 24 && currentList.size() < K; k++ )
		{
			vector<int> temp(1, k);
			currentList.push_back( temp);
		}
		for( int idx = 1; idx < minuteValues.size(); idx++ )
		{
			lastList = currentList;
			currentList.clear();
			if( minuteValues[idx] < minuteValues[idx-1] )
			{	
				for( int j = 0; j < K && currentList.size() < K; j++ )
				{
					for( int k = lastList[j].back() + 1; k <= K && currentList.size() < K; k++ )
					{
						vector<int> temp;
						temp = lastList[j];
						temp.push_back( k );
						currentList.push_back( temp );
					}
				}
				if( currentList.size() < K )
				{
					return vector<int>(1,-1);
				}
			}
			else 
			{
				for( int j = 0; j < K && currentList.size() < K; j++ )
				{
					for( int k = lastList[j].back(); k <= K && currentList.size() < K; k++ )
					{
						vector<int> temp;
						temp = lastList[j];
						temp.push_back( k );
						currentList.push_back( temp );
					}
				}
			}
		}		
		return currentList.back();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值