topcoder srm 420 div1
problem1 link
暴力即可。因为即便所有数字的和是50,50所有的不同的划分数只有204226中。所以最长的循环也就这么大。
problem2 link
令$f[i][j]$表示有$i$个红色和$j$个黑色时最大的期望,那么:
(1)当$j=0$时,$f[i][0]=f[i-1][0]+1$;
(2)当$j>0$但是$i=0$时,$f[i][j]=0$;
(3)当$j>0$且$i>0$时,$f[i][j]=max(0,(f[i-1][j]+1)*\frac{i}{i+j}+(f[i][j-1]-1)*\frac{j}{i+j})$
problem3 link
设$B$为$outputValues$中的最大值。
当$inputValue \geq B*(B-1)$时,在将$inputValue$置换后的输出中一定有$B$。否则,将有大于等于$B$个小于等于$B-1$的数字。那么这大于等于$B$个数字中,一定有某些数字的和是$B$的倍数($x_{1},x_{1}+x_{2},,,,,x_{1}+x_{2}+..+x_{n}$中要么存在$B$倍数的数字,要么一定存在两个数字模$B$的值相等,它们的差就是$B$的倍数)。这时将其拿掉换成都是$B$得到的数字个数更小。
这样的话,只需要解决小于$B*(B-1)$的部分(大于等于$B*(B-1)$的部分都可以直接换成若干$B$)。这里可以使用动态规划。记录置换$i$需要的最少数量以及在这种置换中用到的最大的是$outputValues$中哪一种。
这里需要解决的是,当$i$是$outputValues$中的某个数字时,一定要将$i$替换成至少两个数字之和。可以令$bestPay[i]$表示将$i$置换所需要的最小的个数(可以是一个数字),$bestChange[i]$表示将$i$置换所需要的最小的个数(至少两个数字),而$bestPayCoin[i],bestChangeCoin[i]$分别表示两种情况下最大的是$outputValues$中哪一个。
有了这些,可以推导出小于$B*(B-1)$的$inputValue$被置换成了:
$t_{1}=bestChangeCoin[inputValue]$
$t_{2}=bestPayCoin[inputValue-t_{1}]$
$t3=bestPayCoin[inputValue-t_{1}-t_{2}]$
$...$
最后是对于最终答案的计算。可以从大到小依次置换每一种$outputValues$。
code for problem1
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class SolitaireSimulation { public int periodLength(int[] heaps) { List<Integer> list=new ArrayList<>();
for(int x:heaps) {
list.add(x);
}
List<Integer> list1=next(list);
while(!list.equals(list1)) {
list=next(list);
list1=next(next(list1));
}
int step=1;
list=next(list);
while(!list.equals(list1)){
++step;
list=next(list);
}
return step;
} List<Integer> next(List<Integer> list) {
List<Integer> list1=new ArrayList<>();
for(int i=0;i<list.size();++i) {
if(list.get(i)>1) {
list1.add(list.get(i)-1);
}
}
list1.add(list.size());
Collections.sort(list1);
return list1;
}
}
code for problem2
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class RedIsGood { public double getProfit(int R, int B) { double[][] f=new double[2][B+1];
for(int i=0;i<=B;++i) {
f[0][i]=0;
} int pre=0,cur=1; for(int i=1;i<=R;++i) {
for(int j=0;j<=B;++j) { if(j==0) {
f[cur][j]=i;
continue;
} double p=1.0*i/(i+j);
f[cur][j]=p*(f[pre][j]+1)+(1-p)*(f[cur][j-1]-1);
if(f[cur][j]<0) {
f[cur][j]=0;
}
}
pre^=1;
cur^=1;
}
return f[pre][B];
}
}
code for problem3
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class ChangeOMatic { public long howManyRounds(int[] outputValues,long inputValue) { if(outputValues.length==1) {
return 1;
} final int N=outputValues.length;
final int B=outputValues[N-1];
final int MAX=B*B+B+47;
int[] bestPay=new int[MAX];
int[] bestPayCoin=new int[MAX];
int[] bestChange=new int[MAX];
int[] bestChangeCoin=new int[MAX];
for(int i=0;i<MAX;++i) {
bestPay[i]=bestChange[i]=i;
}
for(int c=1;c<N;++c) {
for(int i=outputValues[c];i<MAX;++i) {
if(bestPay[i]>=bestPay[i-outputValues[c]]+1) {
bestPay[i]=bestPay[i-outputValues[c]]+1;
bestPayCoin[i]=c;
}
}
for(int i=outputValues[c]+1;i<MAX;++i) {
if(bestChange[i]>=bestPay[i-outputValues[c]]+1) {
bestChange[i]=bestPay[i-outputValues[c]]+1;
bestChangeCoin[i]=c;
}
}
} long[] coinCounts=new long[N]; if(inputValue>=MAX) {
coinCounts[N-1]=(inputValue-(MAX-1))/B;
inputValue-=coinCounts[N-1]*B;
if(inputValue>=MAX) {
inputValue-=B;
++coinCounts[N-1];
}
while(inputValue>0) {
++coinCounts[bestPayCoin[(int)inputValue]];
inputValue-=outputValues[bestPayCoin[(int)inputValue]];
}
}
else {
++coinCounts[bestChangeCoin[(int)inputValue]];
inputValue-=outputValues[bestChangeCoin[(int)inputValue]];
while(inputValue>0) {
++coinCounts[bestPayCoin[(int)inputValue]];
inputValue-=outputValues[bestPayCoin[(int)inputValue]];
}
} long result=1;
for(int q=N-1;q>0;--q) {
result+=coinCounts[q];
int remains=outputValues[q];
coinCounts[bestChangeCoin[remains]]+=coinCounts[q];
remains-=outputValues[ bestChangeCoin[remains ]];
while(remains>0) {
coinCounts[bestPayCoin[remains]]+=coinCounts[q];
remains-=outputValues[bestPayCoin[remains]];
}
}
return result;
}
}
topcoder srm 420 div1的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- topcoder srm 575 div1
problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...
随机推荐
- 简易C# socket
服务器 using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Thread ...
- C#项目中关于多个程序集下App.config文件的问题
在项目中我们会经常用到App.config文件,有的是自动生成的,比如引用webservice.wcf服务时生成:也有手动建立的配置文件直接默认名就为app.config.这些配置有的保存当前程序集用 ...
- oracle中实现自增id
在一些数据库(例如mysql)中,实现自增id只要在建表的时候指定一下即可, 但是在oracle中要借助sequence来实现自增id, 要用上自增id,有几种方式: 1.直接在insert语句中使用 ...
- django 网站的搭建(2)
这里使用nginx+uwsgi的方法来搭建生产环境 1,pip3.5 install uwsgi 下载uwsgi ,这里就不做测试了,一般不会出错 2,将django与uwsgi连接在一起 毕竟ru ...
- 11.match
(我对部分段落进行翻译) A match statement is used to branch execution of a program. It’s the equivalent of the ...
- jQuery安装
http://www.runoob.com/jquery/jquery-install.html 网页中添加jQuery: 方法一:可以从http://jquery.com/download/ 下载j ...
- 20155228 2016-2017-2 《Java程序设计》第4周学习总结
20155228 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 继承与多态 继承:在Java中,子类只能继承一个父类,关键字为extends,子类和父类之间 ...
- shell 编程每日100行
xiluhua@localhost ~/tscripts $ echo "hello world !" -bash: !": event not found xiluhu ...
- Hadoop常用命令总结
一.前述 分享一篇hadoop的常用命令的总结,将常用的Hadoop命令总结如下. 二.具体 1.启动hadoop所有进程start-all.sh等价于start-dfs.sh + start-yar ...
- PNG文件格式详解
源文件地址:https://blog.mythsman.com/2015/12/08/1/ 最近在看隐写术的时候经常需要研究图片文件的二进制文档格式,那么这就很有必要了解我们的图片文件究竟是如何保存的 ...