topcoder srm 360 div1
problem1 link
(1)$n \neq m$时,假设$n<m$,那么同一行中的$m$个数字必定都相等。
(2)$n=m$时,要满足任意的$i_{1},i_{2},j_{1},j_{2},A[i_{1}][j_{1}]+A[i_{2}][j_{2}]=A[i_{1}][j_{2}]+A[i_{2}][j_{1}]$
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class SumOfSelectedCells { public String hypothesis(String[] table) {
final String YES="CORRECT";
final String NO="INCORRECT";
final int n=table.length;
int m=-1;
int[][] g=new int[22][22];
for(int i=0;i<n;++i) {
String[] t=table[i].split("\\W+");
if(m==-1) {
m=t.length;
}
else {
assert m==t.length;
}
for(int j=0;j<m;++j) {
g[i][j]=Integer.valueOf(t[j]);
}
}
if(n!=m) {
if(n<m) {
for(int i=0;i<n;++i) {
for(int j=1;j<m;++j) {
if(g[i][0]!=g[i][j]) {
return NO;
}
}
}
}
else {
for(int j=0;j<m;++j) {
for(int i=1;i<n;++i) {
if(g[0][j]!=g[i][j]) {
return NO;
}
}
}
}
}
else {
for(int i1=0;i1<n;++i1) {
for(int i2=i1+1;i2<n;++i2) {
for(int j1=0;j1<m;++j1) {
for(int j2=j1+1;j2<m;++j2) {
if(g[i1][j1]+g[i2][j2]!=g[i1][j2]+g[i2][j1]) {
return NO;
}
}
}
}
}
}
return YES;
}
}
problem2 link
仅当相邻时无解。有解时答案最大为4.因为可以将其中的一个围住。那么只需要检测小于4时是否可以。这时,可以暴力枚举然后判断连通性。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class PrinceOfPersia { static class pair {
public int first;
public int second; public pair() {}
public pair(int first,int second) {
this.first=first;
this.second=second;
}
} int[][] visit=null;
int index=0;
int sx=-1,sy=-1,ex=-1,ey=-1; String[] maze=null; final int[] dx={0,0,1,-1};
final int[] dy={1,-1,0,0};
int n,m; List<pair> empty=null; boolean dfs(int x,int y) {
if(x==ex&&y==ey) {
return true;
}
if(x<0||x>=n||y<0||y>=m||maze[x].charAt(y)=='#') {
return false;
}
if(index==visit[x][y]) {
return false;
}
visit[x][y]=index;
for(int i=0;i<4;++i) {
if(dfs(x+dx[i],y+dy[i])) {
return true;
}
}
return false;
} int cal() {
++index;
if(!dfs(sx,sy)) {
return 0;
}
for(int i=0;i<empty.size();++i) {
++index;
visit[empty.get(i).first][empty.get(i).second]=index;
if(!dfs(sx,sy)) {
return 1;
}
} for(int i=0;i<empty.size();++i) {
for(int j=i+1;j<empty.size();++j) {
++index;
visit[empty.get(i).first][empty.get(i).second]=index;
visit[empty.get(j).first][empty.get(j).second]=index;
if(!dfs(sx,sy)) {
return 2;
}
}
} for(int i=0;i<empty.size();++i) {
for(int j=i+1;j<empty.size();++j) {
for(int k=j+1;k<empty.size();++k) {
++index;
visit[empty.get(i).first][empty.get(i).second]=index;
visit[empty.get(j).first][empty.get(j).second]=index;
visit[empty.get(k).first][empty.get(k).second]=index;
if(!dfs(sx,sy)) {
return 3;
}
}
}
}
return 4;
} public int minObstacles(String[] maze) { this.maze=maze;
n=maze.length;
m=maze[0].length();
visit=new int[n][m];
empty=new ArrayList<>();
for(int i=0;i<n;++i){
for(int j=0;j<m;++j) {
if(maze[i].charAt(j)=='P') {
if(sx==-1) {
sx=i;
sy=j;
}
else {
ex=i;
ey=j;
}
}
else if(maze[i].charAt(j)=='.') {
empty.add(new pair(i,j));
}
}
}
if(sx==ex&&Math.abs(sy-ey)==1||sy==ey&&Math.abs(sx-ex)==1) {
return -1;
}
return cal();
}
}
problem3 link
设$m$ 为给出的数字个数.对于一个凸包,假设顶点大于3,那么去掉任意一个顶点后仍是凸包。所以:
(1)$m\geq 6$时,若为奇数只需要添加一个数字(这时可以暴力),为偶数不需要添加;
(2)$m<6$时,需要添加$6-m$个数字。$m=2,3,4,5$时可以暴力;$m=0$时答案为$1,1,1,2,2,1$,$m=1$时,若给出的数字为1则答案为$1,1,2,2,1$,否则答案为$1,1,1,1,2$
那么,剩下的问题就是判断凸包,只需判断:(1)任意三个点角度小于180;(2)任意两条边不相交(这个只需要判断$y$连续严格增大的顶点段的个数以及$y$严格减小的连续顶点段的个数,为凸包时这些段数最大为3)
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class ConvexArray { static class point {
public long x;
public long y; public point() {}
public point(long x,long y) {
this.x=x;
this.y=y;
}
public point add(point a) {
return new point(x+a.x,y+a.y);
}
public point substract(point a) {
return new point(x-a.x,y-a.y);
}
public long crossMultiply(point a) {
return x*a.y-y*a.x;
} } boolean check(List<Integer> list) {
final int n=list.size()>>1;
List<point> g=new ArrayList<>();
for(int i=0;i<n;++i) {
g.add(new point(list.get(i<<1),list.get(i<<1|1)));
}
long a=0;
for(int i=0;i<n;++i) {
a+=g.get(i).crossMultiply(g.get((i+1)%n));
}
if(a==0) {
return false;
}
if(a<0) {
int ll=0,rr=n-1;
while(ll<rr) {
point p=g.get(ll);
point q=g.get(rr);
g.set(ll,q);
g.set(rr,p);
++ll;
--rr;
}
}
for(int i=0;i<n;++i) {
point p=g.get(i);
point q=g.get((i+1)%n);
point r=g.get((i+2)%n);
if(q.substract(p).crossMultiply(r.substract(p))<=0) {
return false;
}
}
int num=0;
for(int i=0;i<n;) {
if(g.get(i).y<g.get((i+1)%n).y) {
++num;
while(g.get(i).y<g.get((i+1)%n).y) {
++i;
if(i==n) {
break;
}
}
}
else if(g.get(i).y>g.get((i+1)%n).y) {
++num;
while(g.get(i).y>g.get((i+1)%n).y) {
++i;
if(i==n) {
break;
}
}
}
else {
++i;
}
}
if(num>3) {
return false;
}
return true;
} public int[] getEnding(int[] beginning) {
final int n=beginning.length;
if(n>=6) {
List<Integer> list=new ArrayList<>();
for(int i=0;i<n;++i) {
list.add(beginning[i]);
}
if(n%2==1) {
list.add(0);
for(int i=1;i<=50;++i){
list.set(list.size()-1,i);
if(check(list)) {
return new int[]{i};
}
}
return new int[]{-1};
}
else {
if(check(list)) {
return new int[0];
}
else {
return new int[]{-1};
}
}
}
if(n==0) {
return new int[]{1,1,1,2,2,1};
}
if(n==1) {
final int x=beginning[0];
if(x==1) {
return new int[]{1,1,2,2,1};
}
return new int[]{1,1,1,1,2};
}
List<Integer> list=new ArrayList<>();
for(int i=0;i<n;++i) {
list.add(beginning[i]);
}
while(list.size()<6) {
list.add(0);
}
if(dfs(n,list)) {
int[] result=new int[6-n];
for(int i=n;i<6;++i) {
result[i-n]=list.get(i);
}
return result;
}
return new int[]{-1};
} boolean dfs(int t,List<Integer> list) {
if(t==6) {
return check(list);
}
for(int i=1;i<=50;++i) {
list.set(t,i);
if(dfs(t+1,list)) {
return true;
}
}
return false;
}
}
topcoder srm 360 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 515 div1
problem1 link 暴力枚举即可. problem2 link 一共有24小时,所以最多有24个顾客.设$f[x][y][z]$表示还剩下$x$把刀,现在时间是$y$,以及来过的顾客集合为$z ...
- 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 ...
随机推荐
- PXC搭建
一,安装依赖包 rpm -ivh libev-4.04-2.el6.x86_64.rpm =====> ftp://rpmfind.net/linux/atrpms/el6-x86_64/ ...
- Hadoop.之.入门部署
一.课程目标 ->大数据是什么?大数据能做什么? ->什么是Hadoop?Hadoop的设计思想? ->Hadoop如何解决大数据的问题?(什么是hdfs与yarn.MapReduc ...
- nginx空白图片(empty_gif模块)
用过百度统计的兄弟有没有注意到百度使用1x1的空白图片传递统计参数,自己做异步统计的兄弟是否使用静态文件来传递参数.为什么使用空白图片呢,而不是自己存放一张小图呢,nginx里面的空白图片是保存在内存 ...
- Beta冲刺阶段5.0
1. 提供当天站立式会议照片一张 2. 每个人的工作 (有work item 的ID) 成员 昨天已完成的工作 今天计划完成的工作 工作中遇到的困难 具体贡献 郑晓丽 首页活动详情界面的美化 实现首页 ...
- SQLGetStmtAttr
SQLGetStmtAttr 函数定义: SQLRETURN SQLGetStmtAttr( SQLHSTMT StatementHandle, SQLINTEGER Attribut ...
- while练习题
# 1. 使用while循环输出1 2 3 4 5 6 8 9 10count = 1while count <= 10: if count == 7: count += 1 continue ...
- 使用Groovy+Spock轻松写出更简洁的单测
当无法避免做一件事时,那就让它变得更简单. 概述 单测是规范的软件开发流程中的必不可少的环节之一.再伟大的程序员也难以避免自己不犯错,不写出有BUG的程序.单测就是用来检测BUG的.Java阵营中,J ...
- 【转】Apache Kylin 2.0为大数据带来交互式的BI
本文转载自:[技术帖]Apache Kylin 2.0为大数据带来交互式的BI 编者注:Kyligence的联合创始人兼CEO Luke Han在上做题为“”的演讲. 基于Hadoop的SQL一直在被 ...
- php CI框架中URL特殊字符处理与SQL注入隐患
php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...
- leaflet:调用arcgis切片地图服务
var mymap = L.map('mapid').setView([31.59, 120.29], 7); L.tileLayer('http://map.geoq.cn/ArcGIS/rest/ ...