topcoder srm 315 div1

时间:2023-01-09 21:37:45

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&lt&semi;peter&lowbar;pan&gt&semi;

    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&lpar;枚举&rpar;

    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 &colon; 字符串

    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. python requests 基础学习

    首先,Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API 不友好.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来 ...

  2. 移植u-boot-2012&period;04&period;01到JZ2440

    开发环境:Ubuntu 12.04 开发板:JZ2440  256M NandFlash  64M SDRAM 交叉编译器:arm-linux-gcc-4.3.2 u-boot:u-boot-2012 ...

  3. Parhaps you are running on a JRE rather than a JDK&quest;

    maven项目启动时报错 解决方案: 第一步:在启动项目上右击 第二步:修改JRE为JDK,双击划线部分 第三步:如果没有配置JDK,进行以下操作 第四步:从本地添加JDK 第五步:应用JDK 选择好 ...

  4. hibernate 解决诡异的mysql存入中文乱码

    使用hibernate查询mysql,通过bean的get方法拿到字符串再写入mysql中的字段会中文乱码,需要String string = xxx.get(),把get方法拿到的值传入到新的str ...

  5. 使用zip&period;js压缩文件和解压文件

    zip.js官方网站为:https://stuk.github.io/jszip/ 在此说明,下面的例子基本上来自官方示例,大家可以做参考,官方示例地址为:https://stuk.github.io ...

  6. Git超实用总结

    Git 是什么? Git 是一个分布式的代码管理容器,本地和远端都保有一份相同的代码. Git 仓库主要是由是三部分组成:本地代码,缓存区,提交历史,这几乎是所有操作的本质,但是为了文章更加简单易懂, ...

  7. 【BZOJ4566】【HAOI2016】找相同字符

    后缀自动姬好,好写好调好ac 原题: 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. 1 <=n1, n2< ...

  8. Xiuno 开发手册正式发布。

    下载地址:http://bbs.xiuno.com/down/xiuno.chm.tar.gz

  9. hadoop 之Hadoop生态系统

    1.Hadoop生态系统概况 Hadoop是一个能够对大量数据进行分布式处理的软件框架.具有可靠.高效.可伸缩的特点. Hadoop的核心是HDFS和Mapreduce,hadoop2.0还包括YAR ...

  10. WWW&period;LoadFromCacheOrDownload

    [WWW.LoadFromCacheOrDownload] static WWWLoadFromCacheOrDownload(string url, int version, uint crc = ...