topcoder srm 325 div1
problem1 link
$g[i]$表示解决前$i$个的代价,那么$g[i]$是所有$g[j]+cost(j+1,i)$的最小值。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class FenceRepairing { public double calculateCost(String[] boards) {
StringBuilder builder=new StringBuilder();
for(int i=0;i<boards.length;++i) {
builder.append(boards[i]);
} final String s=builder.toString();
final int n=s.length();
int[] f=new int[n+1];
f[0]=0;
for(int i=1;i<=n;++i) {
f[i]=f[i-1];
if(s.charAt(i-1)=='X') {
++f[i];
}
}
double[] g=new double[n+1];
g[0]=0;
for(int i=1;i<=n;++i) {
g[i]=g[i-1];
if(f[i]-f[i-1]!=0) {
g[i]+=1;
}
for(int j=0;j<i;++j) {
double t=g[j];
if(f[i]-f[j]>0) {
t+=Math.sqrt(i-j);
}
if(t<g[i]) {
g[i]=t;
}
}
}
return g[n];
}
}
problem2 link
分别讨论$X$的取值区间即可。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class ModularInequality { public int countSolutions(int[] A, int P) {
Arrays.sort(A);
int result=0;
final int n=A.length;
long sum=0;
for(int x:A) {
sum+=x;
} if(sum>=P) {
long k=(sum-P+n-1)/n;
if(k<A[0]) {
result+=A[0]-k;
}
}
else {
long k=(sum-P)/n;
if(k<A[0]) {
result+=A[0]-k;
}
} if(P+sum>=0) {
long k=(P+sum)/n;
if(A[n-1]<=k) {
result+=k-A[n-1]+1;
}
}
else {
long k=(P+sum-(n-1))/n;
if(A[n-1]<=k) {
result+=k-A[n-1]+1;
}
} long pre=0;
for(int i=1;i<n;++i) {
pre+=A[i-1];
sum-=A[i-1]; if(A[i]==A[i-1]) {
continue;
} long aa=P-(sum-pre);
long bb=i+i-n; if(bb==0) {
if(aa>=0) {
result+=A[i]-A[i-1];
}
}
else if(bb<0) {
long k=-1;
if(aa<0) {
k=aa/bb;
if(aa%bb!=0) {
++k;
}
}
else if(aa==0) {
k=0;
}
else {
k=aa/bb;
}
if(k<A[i]) {
result+=A[i]-Math.max(A[i-1],k);
}
}
else {
long k=-1;
if(aa<0) {
k=aa/bb;
if(aa%bb!=0) {
--k;
}
}
else if(aa==0) {
k=0;
}
else {
k=aa/bb;
}
if(A[i-1]<=k) {
result+=Math.min(k,A[i]-1)-A[i-1]+1;
}
} }
return result;
}
}
problem3 link
从小到大依次枚举每个币种的面值。假设要求的答案为$f(n,K)$。当枚举第二种面值的时候,假设是2,那么后面所有的面值都是2的倍数,所以此时$f(n,K)=n$%$2+f(\frac{n}{2},K-1)$。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class NewMoneySystem { public long chooseBanknotes(String N,int K) {
map=new HashMap<>();
return dfs(Long.valueOf(N),K);
} static Map<Long,Map<Integer,Long>> map=null; long dfs(long n,int k) {
if(k==1) {
return n;
}
if(n==0) {
return 0;
}
Map<Integer,Long> t=map.get(n);
if(t==null) {
t=new HashMap<>();
map.put(n,t);
}
if(t.get(k)!=null) {
return t.get(k);
}
long result=-1;
for(int i=2;i<=5;++i) {
long tmp=n%i+dfs(n/i,k-1);
if(result==-1||result>tmp) {
result=tmp;
}
}
t.put(k,result);
return result;
} }
topcoder srm 325 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)其 ...
随机推荐
- node.js初识05
小项目,需求,通过url来查询学生学号和老师的编号 05.js var http = require("http"); var server = http.createServer ...
- Spring框架第一天
## 今天课程:Spring框架第一天 ## ---------- **Spring框架的学习路线** 1. Spring第一天:Spring的IOC容器之XML的方式,Spring框架与Web项目整 ...
- “0x00,0x08”两个十六进制字符串,转换为整形
int m_length=0;char buf[2]=={0x00,0x08};memcpy(&m_length,&buf[0],2); m_length=m_length<&l ...
- sublime text3配置及相关小技巧
1.下载&安装: 官方地址:http://www.sublimetext.com/,sublime text3又更新了,支持不依赖插件进行侧边栏颜色的更改,同时自带的皮肤颜色也有四种,十分方便 ...
- I Count Tow Three
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#inclu ...
- LinkedList 底层实现原理
LinkedList的底层实现原理 LinkedList 底层数据结构为双向链表,链表结构,基于一个个链表节点Node 1,Inner Class 内部类 private static class N ...
- 用django统计代码行数+注释行数
实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...
- mybatis源码解析3---XMLConfigBuilder解析
1.XMLConfigBuilder XMLConfigBuilder类位于Mybatis包的org.apache.ibatis.builder.xml目录下,继承于BaseBuilder类,关于Ba ...
- react系列笔记1 用npx npm命令创建react app
react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...
- TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow
TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow 只需在cmd中输入pip ...