Topcoder-SRM-#712-Div2
250-RepeatNumberCompare
Problem Statement
For any two positive integers x and k we can make a new number denoted repeat(x, k) by concatenating k copies of x written in decimal. For example, repeat(1234,3) = 123412341234 and repeat(70,4) = 70707070.
You are given the ints x1, k1, x2, and k2. Let v1 = repeat(x1, k1) and v2 = repeat(x2, k2). Please compare the numbers v1 and v2 and return a string that describes the result of the comparison. In particular:
Return "Less" if v1 is less than v2.
Return "Greater" if v1 is greater than v2.
Return "Equal" if v1 and v2 are equal.
题意
将x写k次后组成的新数字,比较两个的大小
代码
class RepeatNumberCompare
{
public:
string compare(int x1, int k1, int x2, int k2)
{
int k=x1;
int a[100];
int b[100];
int len1=0;
while (k)
{
len1++;
a[len1]=k%10;
k/=10;
}
k=x2;
int len2=0;
while (k)
{
len2++;
b[len2]=k%10;
k/=10;
}
if (len1*k1<len2*k2) return "Less";
if (len1*k1>len2*k2) return "Greater";
string sk1;sk1.clear();
string sk2;sk2.clear();
for (int o=1;o<=k1;o++)
for (int i=len1;i>0;i--) sk1.push_back(a[i]+'0');
for (int o=1;o<=k2;o++)
for (int i=len2;i>0;i--) sk2.push_back(b[i]+'0');
if (sk1<sk2) return "Less";
if (sk1==sk2) return "Equal";
if (sk1>sk2) return "Greater";
}
}t;
int main()
{
cout<<t.compare(1010,3,101010,2);
}
500-MakePalindrome
Problem Statement
You have some cards. Each card contains a single lowercase letter. You are given these letters as the characters of the string card.
A palindrome is a string that reads the same forwards and backwards. Examples of palindromes: "eve", "abba", "aaaaaa", and "racecar".
Use the cards you have to spell some palindromes. In particular:
- Each card must be used in exactly one of the palindromes.
- The total number of palindromes must be as small as possible.
Return a vector containing the palindromes you built. (Each element of the return value should be one of the palindromes.)
A solution always exists. If there are multiple optimal solutions, you may choose and output any one of them.
题意
用他给的字符串的所有字母组成最少的回文串
代码
class MakePalindrome
{
public:
vector <string> constructMinimal(string card)
{
vector <string> ret;ret.clear();
int a[200];memset(a,0,sizeof(a));
for (int i=0;i<card.size();i++) a[card[i]]++;
queue<char> q;while (!q.empty()) q.pop();
queue<char> sig;while (!sig.empty()) sig.pop();
stack<char> s;while (!s.empty()) s.pop();
for (int i='a';i<='z';i++)
{
while (a[i]>=2)
{
a[i]-=2;
q.push(i);
}
if (a[i]) sig.push(i);
}
string pus;pus.clear();
while (!q.empty())
{
s.push(q.front());
pus.push_back(q.front());
q.pop();
}
if (!sig.empty())
{
pus.push_back(sig.front());sig.pop();
}
while (!s.empty())
{
pus.push_back(s.top());
s.pop();
}
ret.push_back(pus);
while (!sig.empty())
{
pus.clear();
pus.push_back(sig.front());
sig.pop();
ret.push_back(pus);
}
return ret;
}
};
1000-AverageVarianceSubset
Problem Statement
In probability theory and statistics, variance is the expectation of the squared deviation of a random variable from its mean. As a special case, we can compute the variance of a nonempty finite set X = { x_1, ..., x_n } as follows:
- Let mu = (x_1 + ... + x_n) / n be the mean of the set.
- Let y_i = (x_i - mu)^2 be the square of the difference between x_i and the mean.
- The variance of X, denoted var(X), can now be computed as the average of all y_i. (In other words, as the sum of all y_i, divided by n.)
For example, if X = { 0, 1 }, we have mu = 1/2, then y_1 = y_2 = 1/4, and finally var(X) = (1/4 + 1/4) / 2 = 1/4.
The range of a nonempty finite set is the difference between its maximum and its minimum. For example, the range of the set { 40, 51, 67, 70 } is 70 - 40 = 30.
You are given a vector s that contains a set of distinct positive integers. You are also given an int R.
Consider all nonempty subsets of s with range less than or equal to R. Alice computed the variance of each of those subsets. Bob took all Alice's results and computed their average. Compute and return the number computed by Bob
题意
取所有非空子集,去除最大值与最小值相差超过R的集合,计算集合内的方差,求所有方差的平均数
代码(不过hack)
int cmp(int q,int w)
{
return q<w;
}
class AverageVarianceSubset
{
public:
int cnt;
int RR;
vector<int> q;
int top;
int a[100];
double sumit()
{
cnt++;
double sum=0;
double mean=0;
for (int i=0;i<top;i++) sum+=a[i];
mean=sum/top;
sum=0;
for (int i=0;i<top;i++) sum+=(a[i]-mean)*(a[i]-mean);
return sum/top;
}
double dfs(int k)
{
double ret=0;
if (k>=q.size() || (top && q[k]-a[0]>RR))
{
if (top) return sumit();
}
else
{
if (q[k]-a[0]<=RR || !top) a[top++]=q[k];
ret+=dfs(k+1);
top--;
ret+=dfs(k+1);
return ret;
}
return 0;
}
double average(vector <int> s, int R)
{
RR=R;
sort(s.begin(),s.end(),cmp);
q=s;
return dfs(0)/cnt;
}
};
赛后总结
250打错变量
500打错括号
烦躁
Topcoder-SRM-#712-Div2的更多相关文章
- Topcoder Srm 673 Div2 1000 BearPermutations2
\(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...
- Topcoder Srm 671 Div2 1000 BearDestroysDiv2
\(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- Topcoder srm 632 div2
脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...
- topcoder SRM 628 DIV2 BracketExpressions
先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...
- topcoder SRM 628 DIV2 BishopMove
题目比较简单. 注意看测试用例2,给的提示 Please note that this is the largest possible return value: whenever there is ...
- Topcoder SRM 683 Div2 B
贪心的题,从左向右推过去即可 #include <vector> #include <list> #include <map> #include <set&g ...
- Topcoder SRM 683 Div2 - C
树形Dp的题,根据题意建树. DP[i][0] 表示以i为根节点的树的包含i的时候的所有状态点数的总和 Dp[i][1] 表示包含i结点的状态数目 对于一个子节点v Dp[i][0] = (Dp[v] ...
- Topcoder SRM 626 DIV2 SumOfPower
本题就是求所有连续子数列的和 开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列 循环遍历即可 int findSum(vector <int> array) { ; ; ...
- Topcoder SRM 626 DIV2 FixedDiceGameDiv2
典型的条件概率题目. 事件A在另外一个事件B已经发生条件下的发生概率.条件概率表示为P(A|B),读作“在B条件下A的概率”. 若只有两个事件A,B,那么, P(A|B)=P(AB)/P(B) 本题的 ...
随机推荐
- JS编程题练习
JS编程题练习 1. 两个数组合并成一个数组排序返回 先依次比较两个数组,按照小的就传入新的数组.当这次比较完之后可能有一个数组的长度很长,留下一些数组,然后在新数组的末尾插入即可. function ...
- PHP系统编程--PHP进程信号处理(转)
原地址:https://www.cnblogs.com/linzhenjie/p/5485436.html PHP的pcntl扩展提供了信号处理的功能,利用它可以让PHP来接管信号的处理,在开发服务器 ...
- sqljdbc4.jar的安装
自己项目环境(idea+jdk1.8+tomcat8),在搭建maven项目时,由于本地数据库是使用了sqlserver,所以需要项目与sqlserver之间建立连接,但是网上查的资料都说微软不允许以 ...
- css让内层div自动撑开外层div
.clear{clear:both;height:0px;font-size: 1px;line-height: 0px;} <div class="audi_items"& ...
- 基于vue的悬浮碰撞窗口(用于打广告的)组件
由于项目需要改写了一个悬浮碰撞弹窗组件 <template> <div class="floatLayer"> <a class="clos ...
- 安装nginx流程
1.下载nginx压缩包: 下载nginx:http://nginx.org/en/download.html 本教程下载 nginx-1.14.0.zip(http://nginx.org/down ...
- c# dev treelist 总结
1:去掉左侧顺序号列 2: EnableAppearanceFocusedCell 允许/否获得焦点的单格使用外观 设置TreeList的OptionsSelection属性: 3:设置TreeLis ...
- git 分支的创建和切换
每次提交,GIT 都会将他们串成一个时间线,截止到目前,只有一个时间线,GIT里叫这个分支为主分支,叫master,HEAD指向master,master指向提交,HEAD指向当前的分支. 一开始的时 ...
- SSH原理与运用(一):远程登录(转)
作者: 阮一峰 日期: 2011年12月21日 SSH是每一台Linux电脑的标准配置. 随着Linux设备从电脑逐渐扩展到手机.外设和家用电器,SSH的使用范围也越来越广.不仅程序员离不开它,很 ...
- Delphi中静态方法重载还是覆盖的讨论
Delphi中静态方法重载还是覆盖的讨论 新人学习Delphi的时候,容易搞不懂的一个问题,当子类方法和基类方法同名,并且参数也一样的时候,叫做什么呢?是覆盖,还是重载呢? 答案是隐藏父类方法. 一般 ...