TopCoder SRM 144 DIV 2

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

时间: 2024-08-12 22:12:24

TopCoder SRM 144 DIV 2的相关文章

Topcoder SRM 144 DIV 1

BinaryCode 模拟 题意是:定义串P,Q,其中Q[i]=P[i-1]+P[i]+P[i+1],边界取0,并且P必须是01串.现在给你Q,让你求出P. 做法是:枚举第一位是1还是0,然后就可以推到出P[i]=Q[i-1]-P[i-1]-P[i-2],需要注意一下边界就好. Lottery 组合数学 题意是:给你四种买彩票,将他们的中奖概率排序,这四种彩票都是从1到a中取b个数字,第一种是随便取,第二种是选取的必须是有序的,第三种是选取的必须是不同的,第四种是选取的必须是有序且不同的. 做法

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization &amp; Codeforces 839 E

传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相加,含有n个不同变量的式子的最大值. 另外限制了每一个变量的最大最小值R[i]和L[i]和所有变量之和的最大值Max. n<=13 题外话: 刚开始做这道题的时候,感觉意外眼熟? codeforces 839 E(此题的退化版):http://codeforces.com/contest/839/pro

TopCoder Practice SRM 144 Div 1

1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <sstream> 5 #include <algorithm> 6 7 class Lottery 8 { 9 public: 10 std::vector<std::string> sortByOdds(std::vector<std::string> rules) 11 {

[topcoder]SRM 633 DIV 2

第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #include <vector> #include <algorithm> using namespace std; class Target { public: vector <string> draw(int n) { vector<string> result(n,

[topcoder]SRM 646 DIV 2

第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs.com/lautsie/p/4245242.html BFS和DFS都可以,注意的是,写的时候,可以往que里几个东西一起扔,就不用建立对象了.也可以直接用二维矩阵记录blocked和visited. 剪枝什么的,最基本的是发现其实在步数限制的情况下,棋盘就是有界的了. 第三题:http://ap

Topcoder SRM 648 (div.2)

第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map&

TopCoder SRM 596 DIV 1 250

body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } Problem Statement      You have an array with N elements. Initially, each element is 0. You can perform the following operations: Increment operation: