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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | seconds will be between 0 and 24*60*60 - 1 = 86399, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
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):
Now we repeat the process, assuming the opposite about P[0]:
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | message will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | Each character in message will be either ‘0‘, ‘1‘, ‘2‘, or ‘3‘. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
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
原文地址:http://blog.csdn.net/charlie_pyc/article/details/38894737