topcoder srm 315 div1
problem1 link
直接模拟即可。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class DigitsSum { public int lastDigit(int n) {
while(n>=10) {
int s=0;
while(n!=0) {
s+=n%10;
n/=10;
}
n=s;
}
return n;
}
}
problem2 link
暴力搜索,记忆搜过的状态。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class SillySudoku { Map<Long,Integer> map=null; long gethash(Integer[][] a) {
long x=0;
for(int i=0;i<4;++i) {
for(int j=0;j<4;++j) {
x=x*10+a[i][j];
}
}
return x;
} boolean check(Integer[][] a,int x,int y) {
for(int i=0;i<4;++i) {
if(i!=y&&a[x][i]==a[x][y]) {
return false;
}
if(i!=x&&a[i][y]==a[x][y]) {
return false;
}
}
for(int i=x/2*2;i<x/2*2+2;++i) {
for(int j=y/2*2;j<y/2*2+2;++j) {
if(i==x&&j==y) continue;
if(a[x][y]==a[i][j]) {
return false;
}
}
}
return true;
} int dfs(Integer[][] a,int x,int y) {
if(y==4) {
++x;
y=0;
}
if(x==4) {
return 1;
}
long h=gethash(a);
if(map.containsKey(h)) {
return map.get(h);
}
if(a[x][y]!=0) {
int result=check(a,x,y)?dfs(a,x,y+1):0;
map.put(h,result);
return result;
} int result=0; for(int i=1;i<=4;++i) {
a[x][y]=i;
if(check(a,x,y)) {
result+=dfs(a,x,y+1);
}
a[x][y]=0;
}
map.put(h,result);
return result;
} public int countWays(String[] board) {
map=new HashMap<>(); Integer[][] a=new Integer[4][4];
for(int i=0;i<4;++i) {
for(int j=0;j<4;++j) {
if(board[i].charAt(j)=='-') {
a[i][j]=0;
}
else {
a[i][j]=(int)(board[i].charAt(j)-'0');
}
}
} return dfs(a,0,0);
}
}
problem3 link
三个矩形的位置有六种情况:
(1)右侧一个,左侧上下两个;
(2)左侧一个,右侧上下两个;
(3)左中右三个;
(4)上面左右两个,下面一个;
(5)上面一个,下面左右两个;
(6)上中下三个。
所以预处理并保存以(i,j)为右下角,左下角,右上角,左上角的区域内可选出的最优矩形,这样就能轻松地解决(1)(2)(4)(5)四种情况。对于第(3)(6)两种情况,首先枚举两条分界线,分界线中间的这一块不能直接得到结果,只能暴力。所以最大的复杂度是$n^{4}$
import java.util.*;
import java.math.*;
import java.util.regex.Matcher; import static java.lang.Math.*; public class ThreeMines { int n,m;
int[][] a=null;
int[][] b=null; int[][] rb=null;
int[][] lb=null;
int[][] lu=null;
int[][] ru=null; int max(int ...a) {
int ans=Integer.MIN_VALUE;
for(int i=0;i<a.length;++i) {
ans=Math.max(ans,a[i]);
}
return ans;
} int calsum(int x0,int y0,int x1,int y1) {
return b[x1][y1]-b[x1][y0-1]-b[x0-1][y1]+b[x0-1][y0-1];
} void calrb() {
rb=new int[n+1][m+1];
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j) {
rb[i][j]=Integer.MIN_VALUE;
for(int x=1;x<=i;++x) {
for(int y=1;y<=j;++y) {
rb[i][j]=max(rb[i][j],calsum(x,y,i,j));
}
}
if(i>1) rb[i][j]=max(rb[i][j],rb[i-1][j]);
if(j>1) rb[i][j]=max(rb[i][j],rb[i][j-1]);
}
}
} void callb() {
lb=new int[n+1][m+1];
for(int i=1;i<=n;++i) {
for(int j=m;j>=1;--j) {
lb[i][j]=Integer.MIN_VALUE;
for(int x=1;x<=i;++x) {
for(int y=m;y>=j;--y) {
lb[i][j]=max(lb[i][j],calsum(x,j,i,y));
}
}
if(i>1) lb[i][j]=max(lb[i][j],lb[i-1][j]);
if(j<m) lb[i][j]=max(lb[i][j],lb[i][j+1]);
}
}
}
void callu() {
lu=new int[n+1][m+1];
for(int i=n;i>=1;--i) {
for(int j=m;j>=1;--j) {
lu[i][j]=Integer.MIN_VALUE;
for(int x=n;x>=i;--x) {
for(int y=m;y>=j;--y) {
lu[i][j]=max(lu[i][j],calsum(i,j,x,y));
}
}
if(i<n) lu[i][j]=max(lu[i][j],lu[i+1][j]);
if(j<m) lu[i][j]=max(lu[i][j],lu[i][j+1]);
}
}
} void calru() {
ru=new int[n+1][m+1];
for(int i=n;i>=1;--i) {
for(int j=1;j<=m;++j) {
ru[i][j]=Integer.MIN_VALUE;
for(int x=n;x>=i;--x) {
for(int y=1;y<=j;++y) {
ru[i][j]=max(ru[i][j],calsum(i,y,x,j));
}
}
if(i<n) ru[i][j]=max(ru[i][j],ru[i+1][j]);
if(j>1) ru[i][j]=max(ru[i][j],ru[i][j-1]);
}
}
} int cal1() {
int ans=Integer.MIN_VALUE;
for(int j=1;j<m;++j) {
for(int i=1;i<n;++i) {
ans=max(ans,rb[i][j]+ru[i+1][j]+lb[n][j+1]);
}
}
return ans;
}
int cal2() {
int ans=Integer.MIN_VALUE;
for(int j=1;j<m;++j) {
for(int i=1;i<n;++i) {
ans=max(ans,rb[n][j]+lb[i][j+1]+lu[i+1][j+1]);
}
}
return ans;
}
int cal3() {
int ans=Integer.MIN_VALUE;
for(int i=1;i<m;++i) {
for(int j=i+1;j<m;++j) {
int premax=Integer.MIN_VALUE;
for(int k=1;k<=n;++k) {
for(int x=1;x<=k;++x) {
premax=max(premax,calsum(x,i+1,k,j));
}
}
ans=max(ans,rb[n][i]+premax+lb[n][j+1]);
}
}
return ans;
}
int cal4() {
int ans=Integer.MIN_VALUE;
for(int i=1;i<n;++i) {
for(int j=1;j<m;++j) {
ans=max(ans,rb[i][j]+lb[i][j+1]+ru[i+1][m]);
}
} return ans;
}
int cal5() {
int ans=Integer.MIN_VALUE;
for(int i=1;i<n;++i) {
for(int j=1;j<m;++j) {
ans=max(ans,rb[i][m]+ru[i+1][j]+lu[i+1][j+1]);
}
}
return ans;
} int cal6() {
int ans=Integer.MIN_VALUE;
for(int i=1;i<n;++i) {
for(int j=i+1;j<n;++j) {
int premax=Integer.MIN_VALUE;
for(int k=1;k<=m;++k) {
for(int y=1;y<=k;++y) {
premax=max(premax,calsum(i+1,y,j,k));
}
}
ans=max(ans,rb[i][m]+premax+ru[j+1][m]);
}
}
return ans;
} public int maximumProfit(String[] field) {
n=field.length;
m=field[0].length();
a=new int[n+1][m+1];
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
char c=field[i].charAt(j);
if('a'<=c&&c<='z') {
a[i+1][j+1]=(int)(c-'a');
}
else {
a[i+1][j+1]=(int)('A'-c);
}
}
}
b=new int[n+1][m+1];
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j) {
b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+a[i][j];
}
}
calrb();
callb();
callu();
calru();
int ans=Integer.MIN_VALUE;
ans=max(ans,cal1());
ans=max(ans,cal2());
ans=max(ans,cal3());
ans=max(ans,cal4());
ans=max(ans,cal5());
ans=max(ans,cal6());
return ans;
}
}
topcoder srm 315 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)其 ...
随机推荐
- teragen/terasort_简化版
1, 关闭Hadoop安全模式 进入hdfs用户 su – hdfs Cd /opt/cloudera/parcels/CDH-5.12.1-1.cdh5.12.1.p0.3/bin hdfs dfs ...
- InstallShield :cannot rename directory ...
InstallShield项目编译的生成目录文件夹需要关闭.
- python爬虫-基础入门-爬取整个网站《1》
python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...
- 018-AJAX异步请求XMLHttpRequest
创建XMLHttpRequest对象 一.先来创建XMLHttpRequest对象在IE.Firefox.safari和Opera中创建该对象的JavaScript代码为: var xhr = new ...
- 超简单系列:ubuntu 13.04 安装 apache2.2+mod_wsgi+Django
1,Ubuntu更新系统 sudo apt-get update sudo apt-get upgrade 2,安装apache,mod_wsgi,Django sudo apt-get instal ...
- hdu5302 构造
题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在,Demon将图分成两部分,一部分包含所有的黑边,另一部分包括所有的白边,给你白边图中度为0的点的数量w ...
- SoapUI link
1. Groovy language: http://docs.groovy-lang.org/latest/html/documentation/index.html http://doc ...
- sqlmap的使用
安全测试===sqlmap(壹)转载 六.优化 这些参数可以优化Sqlmap的性能. 1.一键优化 参数:-o 添加此参数相当于同时添加下列三个优化参数: --keep-alive --null- ...
- isIos
function IsIOS() { if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { return true } else { r ...
- qt creator翻译流程
一,下载qt creator软件包,最好是5.3, 本人是Win7,64位电脑,下载的是qt-opensource-windows-x86-mingw482_opengl-5.3.0.ex ...