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)其 ...
随机推荐
- QT 添加 lib库
扒自网友文章: 一.添加第三方的头文件 首先,添加头文件 #include "ControlCAN.h" 然后,再将这个头文件放到工程的目录下 二.添加.lib文件 首先,将.l ...
- LeetCode168.Excel表列名称
给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...
- MySQL练习题之参考答案
1.创建表结构和数据 /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Sou ...
- sql server得到某个数据库的所有表和所有字段
select b.name tablename,a.name as columnnamefrom sys.columns a,sys.objects b,sys.types cwhere a.obje ...
- 【Linux学习四】正则表达式
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.grep:显示匹配行v:反显示e:使用扩展正则表达式E:不使用正则 ...
- Linux基础命令---文本统计wc
wc 统计文件的字节数.单词数.行数. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 wc [选项] f ...
- 转:控制ComboBox下拉框的下拉部分宽度,使内容能够显示完全
一般的情况下,如果下拉框的选项的文字太长,下拉框ComboBox的Width宽度属性我们又不想要改变(默认不变),下拉选项的文字内容就会被截剪,如下图所示: 解决办法: 1.自动判断下拉选项的文字长度 ...
- 获取当前exe的路径
1.Assembly.GetExecutingAssembly().Location得到exe的全路径,Path.GetDirectoryName得到目录路径,不要用Directory.GetCurr ...
- QT中添加 动态库(.so) 和 静态库 (.a) 的方法
在QT 的Makefile文件中: 1 添加动态库,如lipcap.so 则,在LIBS一行中添加“-L/usr/local/lib -lpcap”,依据自己的情况修改libpcap.so的路径 2 ...
- [转载] 关于出现“使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式”错误的可能原因
1. 对于该问题确实存在UNION前后SELECT语句中的列数不同导致:2. 以下为个人遇到的一种可能:在项目开发中由于有张表是动态的,即有个基础表,其他的表按年月根据基础表来生成动态表,动态表结构和 ...