码迷,mamicode.com
首页 > 其他好文 > 详细

TopCoder SRM 144 DIV 2

时间:2014-08-28 11:27:29      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:topcoder   srm144div2   

200:

Problem Statement

  Computers tend to store dates and times as single numbers which represent the number of seconds or milliseconds since a particular date. Your task in this problem is to write a method whatTime, which takes an int, seconds, representing the number of seconds since midnight on some day, and returns a string formatted as "<H>:<M>:<S>". Here, <H> represents the number of complete hours since midnight, <M> represents the number of complete minutes since the last complete hour ended, and <S> represents the number of seconds since the last complete minute ended. Each of <H>, <M>, and <S> should be an integer, with no extra leading 0‘s. Thus, if seconds is 0, you should return "0:0:0", while if seconds is 3661, you should return "1:1:1".

Definition

 
Class: Time
Method: whatTime
Parameters: int
Returns: string
Method signature: string whatTime(int seconds)
(be sure your method is public)

Limits

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

Constraints

- seconds will be between 0 and 24*60*60 - 1 = 86399, inclusive.

Examples

0)  
 
0
Returns: "0:0:0"
 
1)  
 
3661
Returns: "1:1:1"
 
2)  
 
5436
Returns: "1:30:36"
 
3)  
 
86399
Returns: "23:59:59"
 

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.     

题目大意:给定秒数,转化成【时:分:秒】的形式。

算法讨论:转化成60进制即可。

Code:

#include <string>

using namespace std;

class Time{
	public:
		string whatTime(int seconds){
			int s=seconds%60;
			seconds/=60;
			int m=seconds%60,h=seconds/60;
			string ans;
			int len=0,digit[20];
			for (int i=h;i;i/=10) digit[++len]=i%10;
			for (int i=len;i;--i) ans+=(char)(digit[i]+'0');
			if (!len) ans+='0';
			ans+=':';
			len=0;
			for (int i=m;i;i/=10) digit[++len]=i%10;
			for (int i=len;i;--i) ans+=(char)(digit[i]+'0');
			if (!len) ans+='0';
			ans+=':';
			len=0;
			for (int i=s;i;i/=10) digit[++len]=i%10;
			for (int i=len;i;--i) ans+=(char)(digit[i]+'0');
			if (!len) ans+='0';
			return ans;
		}
};


550:

Problem Statement

 

Let‘s say you have a binary string such as the following:

011100011

One way to encrypt this string is to add to each digit the sum of its adjacent digits. For example, the above string would become:

123210122

In particular, if P is the original string, and Q is the encrypted string, then Q[i] = P[i-1] + P[i] + P[i+1] for all digit positions i. Characters off the left and right edges of the string are treated as zeroes.

An encrypted string given to you in this format can be decoded as follows (using 123210122 as an example):

  1. Assume P[0] = 0.
  2. Because Q[0] = P[0] + P[1] = 0 + P[1] = 1, we know that P[1] = 1.
  3. Because Q[1] = P[0] + P[1] + P[2] = 0 + 1 + P[2] = 2, we know that P[2] = 1.
  4. Because Q[2] = P[1] + P[2] + P[3] = 1 + 1 + P[3] = 3, we know that P[3] = 1.
  5. Repeating these steps gives us P[4] = 0, P[5] = 0, P[6] = 0, P[7] = 1, and P[8] = 1.
  6. We check our work by noting that Q[8] = P[7] + P[8] = 1 + 1 = 2. Since this equation works out, we are finished, and we have recovered one possible original string.

Now we repeat the process, assuming the opposite about P[0]:

  1. Assume P[0] = 1.
  2. Because Q[0] = P[0] + P[1] = 1 + P[1] = 1, we know that P[1] = 0.
  3. Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 + P[2] = 2, we know that P[2] = 1.
  4. Now note that Q[2] = P[1] + P[2] + P[3] = 0 + 1 + P[3] = 3, which leads us to the conclusion that P[3] = 2. However, this violates the fact that each character in the original string must be ‘0‘ or ‘1‘. Therefore, there exists no such original string P where the first digit is ‘1‘.

Note that this algorithm produces at most two decodings for any given encrypted string. There can never be more than one possible way to decode a string once the first binary digit is set.

Given a string message, containing the encrypted string, return a vector <string> with exactly two elements. The first element should contain the decrypted string assuming the first character is ‘0‘; the second element should assume the first character is ‘1‘. If one of the tests fails, return the string "NONE" in its place. For the above example, you should return {"011100011", "NONE"}.

Definition

 
Class: BinaryCode
Method: decode
Parameters: string
Returns: vector <string>
Method signature: vector <string> decode(string message)
(be sure your method is public)

Limits

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

Constraints

- message will contain between 1 and 50 characters, inclusive.
- Each character in message will be either ‘0‘, ‘1‘, ‘2‘, or ‘3‘.

Examples

0)  
 
"123210122"
Returns: { "011100011",  "NONE" }

The example from above.

1)  
 
"11"
Returns: { "01",  "10" }

We know that one of the digits must be ‘1‘, and the other must be ‘0‘. We return both cases.

2)  
 
"22111"
Returns: { "NONE",  "11001" }

Since the first digit of the encrypted string is ‘2‘, the first two digits of the original string must be ‘1‘. Our test fails when we try to assume that P[0] = 0.

3)  
 
"123210120"
Returns: { "NONE",  "NONE" }

This is the same as the first example, but the rightmost digit has been changed to something inconsistent with the rest of the original string. No solutions are possible.

4)  
 
"3"
Returns: { "NONE",  "NONE" }
 
5)  
 
"12221112222221112221111111112221111"
Returns: 
{ "01101001101101001101001001001101001",
  "10110010110110010110010010010110010" }
 

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.     

题目大意:设2个数组P[i],Q[i],Q[i]=P[i-1]+P[i]+P[i+1],给定Q,求P的可能形式,其中P[i]∈{0,1}。

算法讨论:枚举P[0],之后几位递推即可,判断是否合法。

Code:

#include <string>
#include <vector>

using namespace std;

class BinaryCode{
	public:
		vector <string> decode(string message){
			vector<string> ans;
			if (message.length()==1){
				if (message[0]=='0') ans.push_back("0"),ans.push_back("NONE");
				else if (message[0]=='1') ans.push_back("NONE"),ans.push_back("1");
				else ans.push_back("NONE"),ans.push_back("NONE");
				return ans;
			}
			vector<int> a,b;
			for (int i=0;i<message.length();++i) a.push_back(message[i]-'0');
			for (int i=0;i<2;++i){
				b.clear();
				b.push_back(i);
				if (a[0]-i!=0 && a[0]-i!=1){
					ans.push_back("NONE");
					continue;
				}
				b.push_back(a[0]-i);
				bool f=1;
				for (int i=1;i<a.size()-1;++i){
					if (a[i]-b[i-1]-b[i]!=0 && a[i]-b[i-1]-b[i]!=1){
						f=0;
						break;
					}
					b.push_back(a[i]-b[i-1]-b[i]);
				}
				if (!f){
					ans.push_back("NONE");
					continue;
				}
				if (b[b.size()-1]+b[b.size()-2]!=a[a.size()-1]){
					ans.push_back("NONE");
					continue;
				}
				string res;
				for (int i=0;i<b.size();++i) res+=b[i]+'0';
				ans.push_back(res);
			}
			return ans;
		}
};


1100:

Problem Statement

 

You work for an electric company, and the power goes out in a rather large apartment complex with a lot of irate tenants. You isolate the problem to a network of sewers underneath the complex with a step-up transformer at every junction in the maze of ducts. Before the power can be restored, every transformer must be checked for proper operation and fixed if necessary. To make things worse, the sewer ducts are arranged as a tree with the root of the tree at the entrance to the network of sewers. This means that in order to get from one transformer to the next, there will be a lot of backtracking through the long and claustrophobic ducts because there are no shortcuts between junctions. Furthermore, it‘s a Sunday; you only have one available technician on duty to search the sewer network for the bad transformers. Your supervisor wants to know how quickly you can get the power back on; he‘s so impatient that he wants the power back on the moment the technician okays the last transformer, without even waiting for the technician to exit the sewers first.

You will be given three vector <int>‘s: fromJunction, toJunction, and ductLength that represents each sewer duct. Duct i starts at junction (fromJunction[i]) and leads to junction (toJunction[i]). ductlength[i] represents the amount of minutes it takes for the technician to traverse the duct connecting fromJunction[i] and toJunction[i]. Consider the amount of time it takes for your technician to check/repair the transformer to be instantaneous. Your technician will start at junction 0 which is the root of the sewer system. Your goal is to calculate the minimum number of minutes it will take for your technician to check all of the transformers. You will return an int that represents this minimum number of minutes.

Definition

 
Class: PowerOutage
Method: estimateTimeOut
Parameters: vector <int>, vector <int>, vector <int>
Returns: int
Method signature: int estimateTimeOut(vector <int> fromJunction, vector <int> toJunction, vector <int> ductLength)
(be sure your method is public)

Limits

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

Constraints

- fromJunction will contain between 1 and 50 elements, inclusive.
- toJunction will contain between 1 and 50 elements, inclusive.
- ductLength will contain between 1 and 50 elements, inclusive.
- toJunction, fromJunction, and ductLength must all contain the same number of elements.
- Every element of fromJunction will be between 0 and 49 inclusive.
- Every element of toJunction will be between 1 and 49 inclusive.
- fromJunction[i] will be less than toJunction[i] for all valid values of i.
- Every (fromJunction[i],toJunction[i]) pair will be unique for all valid values of i.
- Every element of ductlength will be between 1 and 2000000 inclusive.
- The graph represented by the set of edges (fromJunction[i],toJunction[i]) will never contain a loop, and all junctions can be reached from junction 0.

Examples

0)  
 
{0}
{1}
{10}
Returns: 10
The simplest sewer system possible. Your technician would first check transformer 0, travel to junction 1 and check transformer 1, completing his check. This will take 10 minutes.
1)  
 
{0,1,0}
{1,2,3}
{10,10,10}
Returns: 40
Starting at junction 0, if the technician travels to junction 3 first, then backtracks to 0 and travels to junction 1 and then junction 2, all four transformers can be checked in 40 minutes, which is the minimum.
2)  
 
{0,0,0,1,4}
{1,3,4,2,5}
{10,10,100,10,5}
Returns: 165
Traveling in the order 0-1-2-1-0-3-0-4-5 results in a time of 165 minutes which is the minimum.
3)  
 
{0,0,0,1,4,4,6,7,7,7,20}
{1,3,4,2,5,6,7,20,9,10,31}
{10,10,100,10,5,1,1,100,1,1,5}
Returns: 281
Visiting junctions in the order 0-3-0-1-2-1-0-4-5-4-6-7-9-7-10-7-8-11 is optimal, which takes (10+10+10+10+10+10+100+5+5+1+1+1+1+1+1+100+5) or 281 minutes.
4)  
 
{0,0,0,0,0}
{1,2,3,4,5}
{100,200,300,400,500}
Returns: 2500
 

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.     

题目大意:给一棵有根树,求从根开始遍历所有点的最短路径。

算法讨论:贪心,可以证明答案为sigma(cost[i])*2-longest_path。

Code:

#include <vector>
#include <algorithm>

#define N 50

using namespace std;

int mm,ans,ed[N+10],son[N+10],nex[N+10],cost[N+10],dis[N+10];

inline void add(int x,int y,int z){
	nex[++mm]=son[x],son[x]=mm,ed[mm]=y,cost[mm]=z;
}

void dfs(int x){
	for (int i=son[x];i;i=nex[i]){
		int y=ed[i];
  		dis[y]=dis[x]+cost[i];
		dfs(y);
	}
}

class PowerOutage{
	public:
		int estimateTimeOut(vector <int> fromJunction, vector <int> toJunction, vector <int> ductLength){
			for (int i=0;i<fromJunction.size();++i) add(fromJunction[i],toJunction[i],ductLength[i]),ans+=ductLength[i]*2;
			dfs(0);
			vector<int> res;
			for (int i=0;i<50;++i)
				if (!son[i]) res.push_back(dis[i]);
			sort(res.begin(),res.end());
			ans-=res[res.size()-1];
			return ans;
		}
};

By Charlie

Aug 28,2014

TopCoder SRM 144 DIV 2

标签:topcoder   srm144div2   

原文地址:http://blog.csdn.net/charlie_pyc/article/details/38894737

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!