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的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  4. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  5. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  6. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  7. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  8. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  9. 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)其 ...

随机推荐

  1. InstallShield2015制作安装包----------安装后实现电脑开机自启动

    开机自启动有两个方法: 一 .把程序的快捷方式放在”开始---启动“目录下. 二.把程序的安装目录放在注册表”“. 实现方法一: 1.编写bat脚本.执行bat启动exe. a)核心:cmd命令  : ...

  2. ES6学习之 解构赋值

    最近看了一个vue的项目,发现作者大量使用了ES6的语法,包括async, Promise, Set, Map还有一些解构赋值, 才发现自己对于ES6的语法缺乏了总结和运用,看得有点艰难,所以重新学习 ...

  3. 笔记 : WampServe加装PHP版本(7.2.3)为例

    1.由于正在学习Laravel框架,服务器wamp,Composer已搭建完成,但在安装laravel installer之后使用laravel new blog,报错为"This pack ...

  4. ubuntu安装mysql,redis,python-mysqldb

    sudo apt-get install mysql-server sudo apt-get install redis-server sudo apt-get install python-redi ...

  5. wxpython(python3.5)安装

    安装步骤: http://blog.csdn.net/xiaodong193/article/details/51920283 注意:安装软件前需要阅读其中的README.txt,可快速知道安装方法, ...

  6. 【2017-03-02】C#集合,结构体,枚举

    集合 集合与数组的区别 数组:同一类型,固定长度 集合:不同类型,不固定长度 使用集合前需要:     引用命名空间:using System.Collections; 1.普通集合 定义: Arra ...

  7. Thread(26)

    1.进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 2.线程:线程是进程中的一个执行单元,负责当前进程中程序的执行, ...

  8. 自动添加菜品,加入运行中遇到的异常,生成日志文件...<工作中场景...>

    """ 很弱智的小脚本,记录下.也许以后看到会笑,因为太幼稚或者证明曾经也努力过.so... """ """ ...

  9. 区块链公链分片技术(sharding)方案,配思维导图

    区块链公链分片技术(sharding)方案,配思维导图 分片技术(sharding)方案 以太坊分片思路 其基本思想是,将网络中的节点分成不同的碎片,各分片可以并行处理不同交易,这样可以并行处理相互之 ...

  10. python GIL 全局锁,多核cpu下的多线程性能究竟如何?

    python GIL 全局锁,多核cpu下的多线程性能究竟如何?GIL全称Global Interpreter Lock GIL是什么? 首先需要明确的一点是GIL并不是Python的特性,它是在实现 ...