prompt
string | response
string |
|---|---|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
#define MX 101
using namespace std;
char mp[MX][MX];
short sx[MX][MX], sy[MX][MX];
short f[MX][MX][MX][MX];
short n, m, ex, ey;
void chmax(short& a, const short& b) {if(b > a) a = b;}
int main()
{
scanf("%hd%hd", &n, &m);
for(short i=1; i<=n; i++)
{
scanf("%s", mp[i]+1);
for(short j=1; j<=m; j++)
{
if(mp[i][j] == 'o')
sx[i][j] = sx[i-1][j] + 1,
sy[i][j] = sy[i][j-1] + 1;
else
sx[i][j] = sx[i-1][j],
sy[i][j] = sy[i][j-1];
if(mp[i][j] == 'E') ex = i, ey = j;
}
}
short ans = 0;
f[ex][ex][ey][ey] = 0;
for(short l=ey; l>=1; l--)
for(short r=ey; r<=m; r++)
for(short u=ex; u>=1; u--)
for(short d=ex; d<=n; d++)
{
short dd = min(d, (short)(n-(ex-u)));
short du = max(u, (short)(d-ex+1));
short dl = max(l, (short)(r-ey+1));
short dr = min(r, (short)(m-(ey-l)));
if(dd>=du && l-1>=(r-ey+1)) chmax(f[l-1][r][u][d], f[l][r][u][d] + sx[dd][l-1] - sx[du-1][l-1]);
if(dd>=du && r+1<=(m-(ey-l))) chmax(f[l][r+1][u][d], f[l][r][u][d] + sx[dd][r+1] - sx[du-1][r+1]);
if(dr>=dl && u-1>=(d-ex+1)) chmax(f[l][r][u-1][d], f[l][r][u][d] + sy[u-1][dr] - sy[u-1][dl-1]);
if(dr>=dl && d+1<=(n-(ex-u))) chmax(f[l][r][u][d+1], f[l][r][u][d] + sy[d+1][dr] - sy[d+1][dl-1]);
chmax(ans, f[l][r][u][d]);
}
printf("%hd\n", ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
int n,m,sx,sy,pre_ver[111][111],pre_hor[111][111];
char s[111][111];
int dp[111][111][111],ndp[111][111][111];
int getver(int i,int j,int jj){return pre_ver[i][jj]-pre_ver[i][j-1];}
int gethor(int j,int i,int ii){return pre_hor[ii][j]-pre_hor[i-1][j];}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
{
pre_ver[i][j]=pre_ver[i][j-1]+(s[i][j]=='o');
pre_hor[i][j]=pre_hor[i-1][j]+(s[i][j]=='o');
if(s[i][j]=='E')
{
sx=i;
sy=j;
}
}
int ans=0;
dp[0][0][0]=0;
for(int l=0;l<=sy-1;l++)
{
memset(ndp,0,sizeof(ndp));
for(int r=0;r<=m-sy;r++)for(int u=0;u<=sx-1;u++)for(int d=0;d<=n-sx;d++)
{
ans=max(ans,dp[r][u][d]);
int nl=max(sy-l,r+1),nr=min(sy+r,m-l),nu=max(sx-u,d+1),nd=min(sx+d,n-u);
if(sy-l>r+1)ndp[r][u][d]=max(ndp[r][u][d],dp[r][u][d]+gethor(sy-l-1,nu,nd));
if(sy+r<m-l)dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+gethor(sy+r+1,nu,nd));
if(sx-u>d+1)dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+getver(sx-u-1,nl,nr));
if(sx+d<n-u)dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+getver(sx+d+1,nl,nr));
}
memcpy(dp,ndp,sizeof(dp));
}
printf("%d\n",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<iostream>
#include<cstdio>
#include<cstring>
namespace wohaocaia
{
inline void check_max(int a,int &b){if(a>b)b=a;}
const int N=105;
int s[N][N],f[N][N][N][N];
int n,m,sx,sy;
int tl,tr,tu,td;
int got(){for(char c=getchar();;c=getchar())if(c=='o' || c=='.' || c=='E')return c;}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
void initialize()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int x=got();
if(x=='o')s[i][j]=1;
else if(x=='.')s[i][j]=0;
else sx=i,sy=j;
s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
}
}
int bdy(int a,int b,int c,int d)
{
if(a>c || b>d)return 0;
// printf("(%d,%d) -- (%d,%d) : %d\n",a,b,c,d,s[c][d]-s[c][b-1]-s[a-1][d]+s[a-1][b-1]);
return s[c][d]-s[c][b-1]-s[a-1][d]+s[a-1][b-1];
}
void dp()
{
tl=sy-1,tr=m-sy,tu=sx-1,td=n-sx;
// printf("L:%d , R:%d , U:%d , D:%d \n",tl,tr,tu,td);
// printf("%d , %d \n",sx,sy);
f[0][0][0][0]=0;
for(int l=0;l<=tl;l++)
for(int r=0;r<=tr;r++)
for(int u=0;u<=tu;u++)
for(int d=0;d<=td;d++)
{
int v=f[l][r][u][d];
check_max(v+bdy(max(sx-u,d+1),sy-l-1,min(sx+d,n-u),sy-l-1)*(sy-l-1>r),f[l+1][r][u][d]);
check_max(v+bdy(max(sx-u,d+1),sy+r+1,min(sx+d,n-u),sy+r+1)*(sy+r+1<=m-l),f[l][r+1][u][d]);
check_max(v+bdy(sx-u-1,max(sy-l,r+1),sx-u-1,min(sy+r,m-l))*(sx-u-1>d),f[l][r][u+1][d]);
check_max(v+bdy(sx+d+1,max(sy-l,r+1),sx+d+1,min(sy+r,m-l))*(sx+d+1<=n-u),f[l][r][u][d+1]);
// printf("f[%d][%d][%d][%d] = %d\n",l,r,u,d,v);
}
}
void orz()
{
initialize();
dp();
printf("%d\n",f[tl][tr][tu][td]);
}
}
int main()
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
wohaocaia::orz();
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
int dp[2][101][101][101];
int H,W;
string bd[100];
int ei,ej;
int cs[2][101][101];
int main() {
cin >> H >> W;
for (int i=0;i<H;i++) cin >> bd[i];
for (int i=0;i<H;i++) for (int j=0;j<W;j++) {
if (bd[i][j]=='E') {
ei=i;
ej=j;
}
else if (bd[i][j]=='o') {
cs[0][i][j+1]++;
cs[1][i+1][j]++;
}
cs[0][i][j+1]+=cs[0][i][j];
cs[1][i+1][j]+=cs[1][i][j];
}
int p=0;
for (int i=H;i>=0;i--,p=1-p) for (int j=H-i;j>=0;j--)
for (int k=W;k>=0;k--) for (int l=W-k;l>=0;l--) {
if (i+j==H || k+l==W) {
dp[p][j][k][l]=0;
continue;
}
int &r=dp[p][j][k][l];
int t=0;
if (ei+i+1<H-j) {
int lb=max(k,ej-l),ub=min(W-l,ej+k+1);
if (lb<ub) t+=cs[0][ei+i+1][ub]-cs[0][ei+i+1][lb];
}
t+=dp[1-p][j][k][l];
r=t;
t=0;
if (ei-j-1>=i) {
int lb=max(k,ej-l),ub=min(W-l,ej+k+1);
if (lb<ub) t+=cs[0][ei-j-1][ub]-cs[0][ei-j-1][lb];
}
t+=dp[p][j+1][k][l];
if (t>r) r=t;
t=0;
if (ej+k+1<W-l) {
int lb=max(i,ei-j),ub=min(H-j,ei+i+1);
if (lb<ub) t+=cs[1][ub][ej+k+1]-cs[1][lb][ej+k+1];
}
t+=dp[p][j][k+1][l];
if (t>r) r=t;
t=0;
if (ej-l-1>=k) for (int c=max(i,ei-j);c<min(H-j,ei+i+1);c++)
if (bd[c][ej-l-1]=='o') t++;
t+=dp[p][j][k][l+1];
if (t>r) r=t;
}
cout << dp[1-p][0][0][0] << endl;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in JAVA):
|
import java.io.*;
import java.util.*;
public class Main {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
void solve() throws IOException {
int h = nextInt();
int w = nextInt();
char[][] f = new char[h][];
int ex = -1, ey = -1;
for (int i = 0; i < h; i++) {
f[i] = nextToken().toCharArray();
for (int j = 0; j < w; j++) {
if (f[i][j] == 'E') {
ex = i;
ey = j;
}
}
}
int[][] cumX = new int[h][w + 1];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cumX[i][j + 1] = cumX[i][j] + (f[i][j] == 'o' ? 1 : 0);
}
}
int[][] cumY = new int[w][h + 1];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
cumY[i][j + 1] = cumY[i][j] + (f[j][i] == 'o' ? 1 : 0);
}
}
int segmH = h * (h + 1) / 2;
int segmW = w * (w + 1) / 2;
int[] hFrom = new int[segmH];
int[] hTo = new int[segmH];
int[][] hSegmId = new int[h][h];
int ptr = 0;
for (int len = 1; len <= h; len++)
for (int i = 0; i + len <= h; i++) {
hFrom[ptr] = i;
hTo[ptr] = i + len - 1;
hSegmId[i][i + len - 1] = ptr;
ptr++;
}
int[] wFrom = new int[segmW];
int[] wTo = new int[segmW];
int[][] wSegmId = new int[w][w];
ptr = 0;
for (int len = 1; len <= w; len++)
for (int i = 0; i + len <= w; i++) {
wFrom[ptr] = i;
wTo[ptr] = i + len - 1;
wSegmId[i][i + len - 1] = ptr;
ptr++;
}
int[][] dp = new int[segmH][segmW];
for (int i = 0; i < segmH; i++) {
int x1 = hFrom[i];
int x2 = hTo[i];
for (int j = 0; j < segmW; j++) {
int val = 0;
int y1 = wFrom[j];
int y2 = wTo[j];
int xTo = Math.min(ex + x1, x2);
int yTo = Math.min(ey + y1, y2);
int xFrom = Math.max(ex + (x2 - x1) - (h - 1) + x1, x1);
int yFrom = Math.max(ey + (y2 - y1) - (w - 1) + y1, y1);
// kill x1
int bonus = 0;
if (x1 >= xFrom && x1 <= xTo && yFrom <= yTo) {
bonus = cumX[x1][yTo + 1] - cumX[x1][yFrom];
}
val = Math.max(val, bonus + (x1 == x2 ? 0 : dp[hSegmId[x1 + 1][x2]][j]));
// kill y1
bonus = 0;
if (y1 >= yFrom && y1 <= yTo && xFrom <= xTo) {
bonus = cumY[y1][xTo + 1] - cumY[y1][xFrom];
}
val = Math.max(val, bonus + (y1 == y2 ? 0 : dp[i][wSegmId[y1 + 1][y2]]));
// kill x2
bonus = 0;
if (x2 >= xFrom && x2 <= xTo && yFrom <= yTo) {
bonus = cumX[x2][yTo + 1] - cumX[x2][yFrom];
}
val = Math.max(val, bonus + (x1 == x2 ? 0 : dp[hSegmId[x1][x2 - 1]][j]));
// kill y2
bonus = 0;
if (y2 >= yFrom && y2 <= yTo && xFrom <= xTo) {
bonus = cumY[y2][xTo + 1] - cumY[y2][xFrom];
}
val = Math.max(val, bonus + (y1 == y2 ? 0 : dp[i][wSegmId[y1][y2 - 1]]));
// System.err.println(x1 + " " + y1 + " " + x2 + " " + y2 + " " + val);
dp[i][j] = val;
}
}
out.println(dp[segmH - 1][segmW - 1]);
}
Main() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new Main();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100
using namespace std;
char Map[MAXN+5][MAXN+5];
int n,m,ex,ey;
int sumr[MAXN+5][MAXN+5],sumc[MAXN+5][MAXN+5];
int dp[MAXN+5][MAXN+5][MAXN+5];
void Init()
{
memset(dp,-1,sizeof(dp));
}
int main()
{
Init();
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",Map[i]+1);
for(int j=1;j<=m;j++)
{
sumr[i][j]=sumr[i][j-1];
sumc[i][j]=sumc[i-1][j];
if(Map[i][j]=='E')
ex=i,ey=j;
else if(Map[i][j]=='o')
{
sumr[i][j]++;
sumc[i][j]++;
}
}
}
int L,R,U,D,ans=0;
L=ey-1;
R=m-ey;
U=ex-1;
D=n-ex;
dp[0][0][0]=0;
for(int l=0;l<=L;l++)
for(int r=0;r<=R;r++)
for(int u=0;u<=U;u++)
for(int d=0;d<=D;d++)
{
if(dp[r][u][d]==-1)
continue;
ans=max(dp[r][u][d],ans);
int up=max(ex-u,d+1);
int down=min(ex+d,n-u);
int left=max(ey-l,r+1);
int right=min(ey+r,m-l);
if(up>down||left>right)
continue;
int add;
if(u+d<U)
{
add=sumr[ex-u-1][right]-sumr[ex-u-1][left-1];
dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+add);
}
if(u+d<D)
{
add=sumr[ex+d+1][right]-sumr[ex+d+1][left-1];
dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+add);
}
if(l+r<R)
{
add=sumc[down][ey+r+1]-sumc[up-1][ey+r+1];
dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+add);
}
if(l+r<L)
{
add=sumc[down][ey-l-1]-sumc[up-1][ey-l-1];
dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+add);
}
}
printf("%d\n",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
int gi() {
int w=0;bool q=1;char c=getchar();
while ((c<'0'||c>'9') && c!='-') c=getchar();
if (c=='-') q=0,c=getchar();
while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
return q? w:-w;
}
const int N=110;
int h[N][N],u[N][N];
int f[N][N][N],g[N][N][N];
inline void upd(int &x,int y) { x<y?x=y:0; }
int main()
{
int n=gi(),m=gi(),i,j,x,y,lx,rx,ly,ry,t;
char c;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++) {
h[i][j]=h[i][j-1];u[i][j]=u[i-1][j];
while ((c=getchar())!='.'&&c!='E'&&c!='o');
if (c=='E') x=i,y=j;
else if (c=='o') h[i][j]++,u[i][j]++;
}
for (lx=0;lx<x;lx++) {
memcpy(g,f,sizeof(g));
memset(f,0,sizeof(f));
for (rx=0;rx<=n-x;rx++)
for (ly=0;ly<y;ly++)
for (ry=0;ry<=m-y;ry++) {
t=g[rx][ly][ry];
upd(f[rx][ly][ry],t+(x-lx-1>rx?h[x-lx-1][min(y+ry,m-ly)]-h[x-lx-1][max(y-ly-1,ry)]:0));
upd(g[rx+1][ly][ry],t+(x+rx+1<=n-lx?h[x+rx+1][min(y+ry,m-ly)]-h[x+rx+1][max(y-ly-1,ry)]:0));
upd(g[rx][ly+1][ry],t+(y-ly-1>ry?u[min(x+rx,n-lx)][y-ly-1]-u[max(x-lx-1,rx)][y-ly-1]:0));
upd(g[rx][ly][ry+1],t+(y+ry+1<=m-ly?u[min(x+rx,n-lx)][y+ry+1]-u[max(x-lx-1,rx)][y+ry+1]:0));
}
}
printf("%d\n",g[n-x][y-1][m-y]);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
inline void upmax(short &x,const short &y){if(y>x) x=y;}
const int N=105;
short f[N][N][N][N];
short sum1[N][N],sum2[N][N];
int n,m,tx,ty;
void gao()
{
int limu=tx-1,limd=n-tx,liml=ty-1,limr=m-ty;
for(int u=0;u<=limu;u++)
for(int d=0;d<=limd;d++)
for(int l=0;l<=liml;l++)
for(int r=0;r<=limr;r++)
{
int L=max(ty-l,r+1),R=min(ty+r,m-l);
if(L<=R)
{
upmax(f[u+1][d][l][r],f[u][d][l][r]+(tx-u-1>=d+1?sum1[tx-u-1][R]-sum1[tx-u-1][L-1]:0));
upmax(f[u][d+1][l][r],f[u][d][l][r]+(tx+d+1<=n-u?sum1[tx+d+1][R]-sum1[tx+d+1][L-1]:0));
}
L=max(tx-u,d+1),R=min(tx+d,n-u);
if(L<=R)
{
upmax(f[u][d][l+1][r],f[u][d][l][r]+(ty-l-1>=r+1?sum2[R][ty-l-1]-sum2[L-1][ty-l-1]:0));
upmax(f[u][d][l][r+1],f[u][d][l][r]+(ty+r+1<=m-l?sum2[R][ty+r+1]-sum2[L-1][ty+r+1]:0));
}
}
printf("%d\n",f[limu][limd][liml][limr]);
}
int main()
{
static char s[105];
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
for(int j=1;j<=m;j++)
{
if(s[j]=='E') tx=i,ty=j;
sum1[i][j]=sum1[i][j-1]+(s[j]=='o');
sum2[i][j]=sum2[i-1][j]+(s[j]=='o');
}
}
gao();
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,i,j,k,l,x,y;
short f[105][105][105][105],ans,s1[105][105],s2[105][105],z;
char s[105][105];
int main()
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)
{
scanf("%s",s[i]+1);
for(j=1;j<=m;++j)
{
s1[i][j]=s1[i][j-1]+(s[i][j]=='o'?1:0);
if(s[i][j]=='E')
x=i,y=j;
}
}
for(i=1;i<=m;++i)
for(j=1;j<=n;++j)
s2[i][j]=s2[i][j-1]+(s[j][i]=='o'?1:0);
f[0][0][0][0]=0;
for(i=0;i<x;++i)
for(j=0;j<=n-x;++j)
for(k=0;k<y;++k)
for(l=0;l<=m-y;++l)
{
ans=max(ans,f[i][j][k][l]);
int lx=j+1,rx=n-i,ly=l+1,ry=m-k;
//printf("%d %d %d %d %d %d %d %d %d\n",i,j,k,l,f[i][j][k][l],lx,rx,ly,ry);
if(i+1<x)
f[i+1][j][k][l]=max((int)f[i+1][j][k][l],f[i][j][k][l]+(x-i-1>=lx&&x-i-1<=rx?s1[x-i-1][min(y+l,ry)]-s1[x-i-1][max(y-k,ly)-1]:0));
if(j+1<=n-x)
f[i][j+1][k][l]=max((int)f[i][j+1][k][l],f[i][j][k][l]+(x+j+1>=lx&&x+j+1<=rx?s1[x+j+1][min(y+l,ry)]-s1[x+j+1][max(y-k,ly)-1]:0));
if(k+1<y)
f[i][j][k+1][l]=max((int)f[i][j][k+1][l],f[i][j][k][l]+(y-k-1>=ly&&y-k-1<=ry?s2[y-k-1][min(x+j,rx)]-s2[y-k-1][max(x-i,lx)-1]:0));
if(l+1<=m-y)
f[i][j][k][l+1]=max((int)f[i][j][k][l+1],f[i][j][k][l]+(y+l+1>=ly&&y+l+1<=ry?s2[y+l+1][min(x+j,rx)]-s2[y+l+1][max(x-i,lx)-1]:0));
}
printf("%hd",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nn 102
#define lf double
char buf[nn][nn];
int dp[2][nn][nn][nn];
int n,m;
int sum[nn][nn];
int get(int x1,int y1,int x2,int y2)
{
if(x1<1) x1=1;if(y1<1) y1=1;
if(x2>n) x2=n;if(y2>m) y2=m;
return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
}
void upd(int &x,int y)
{
x=max(x,y);
}
int ex,ey;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",buf[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+(buf[i][j]=='o');
if(buf[i][j]=='E') ex=i,ey=j;
}
int now=0;int ans=0;
memset(dp,0x80,sizeof(dp));
dp[0][0][0][0]=0;
for(int up=0;up<=n;up++)
{
for(int down=0;down<=n;down++)
{
for(int left=0;left<=m;left++)
{
for(int right=0;right<=m;right++)
{
// if(ex+down<=n-up and ex-up>down and ey+right<=m-left and ey-left>right) ;
// else continue;
if(ey-left<1 or ex-up<1 or ex+down>n or ey+right>m) continue;
int ct=dp[now][down][left][right];
ans=max(ans,ct);int l,r;
// if(ct==4) cout<<up<<" "<<down<<" "<<left<<" "<<right<<"\n";
l=max(ey-left,ey+right-ey+1),r=min(ey+right,ey-left+m-ey);
if(ex-up-1>down)
upd(dp[!now][down][left][right],
ct+get(ex-up-1,l,ex-up-1,r));
if(ex+down+1<=n-up)
upd(dp[now][down+1][left][right],
ct+get(ex+down+1,l,ex+down+1,r));
l=max(ex-up,ex+down-ex+1),r=min(ex+down,ex-up+n-ex);
if(ey-left-1>right)
upd(dp[now][down][left+1][right],
ct+get(l,ey-left-1,r,ey-left-1));
if(ey+right+1<=m-left)
upd(dp[now][down][left][right+1],
ct+get(l,ey+right+1,r,ey+right+1));
}
}
}
memset(dp[now],0x80,sizeof(dp[now]));
now^=1;
}
cout<<ans;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
template<typename T> inline void chkmin(T &a, const T &b) { a = a < b ? a : b; }
template<typename T> inline void chkmax(T &a, const T &b) { a = a > b ? a : b; }
const int MAXN = 105;
int f[MAXN][MAXN][MAXN], row[MAXN][MAXN], col[MAXN][MAXN], n, m, ex, ey, ans;
char mp[MAXN][MAXN];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", mp[i] + 1);
for (int j = 1; j <= m; j++) {
row[i][j] = row[i][j - 1] + (mp[i][j] == 'o');
col[j][i] = col[j][i - 1] + (mp[i][j] == 'o');
if (mp[i][j] == 'E') ex = i, ey = j;
}
}
for (int i = ex; i > 0; i--)
for (int j = ex; j <= n; j++)
for (int k = ey; k > 0; k--)
for (int l = ey; l <= m; l++) {
int cur = f[j][k][l];
chkmax(ans, cur);
if (i > 1 && i - 1 > j - ex)
chkmax(f[j][k][l], cur + row[i - 1][min(l, m - ey + k)] - row[i - 1][max(k - 1, l - ey)]);
if (j < n && ex - i < n - j)
chkmax(f[j + 1][k][l], cur + row[j + 1][min(l, m - ey + k)] - row[j + 1][max(k - 1, l - ey)]);
if (k > 1 && k - 1 > l - ey)
chkmax(f[j][k - 1][l], cur + col[k - 1][min(j, n - ex + i)] - col[k - 1][max(i - 1, j - ex)]);
if (l < m && ey - k < m - l)
chkmax(f[j][k][l + 1], cur + col[l + 1][min(j, n - ex + i)] - col[l + 1][max(i - 1, j - ex)]);
}
printf("%d\n", ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 110;
int f[2][Maxn][Maxn][Maxn];
int n, m;
int stx, sty;
char s[Maxn][Maxn];
int a[Maxn][Maxn], row[Maxn][Maxn], col[Maxn][Maxn];
void up(int &x, int y) { if(x < y) x = y; }
int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
int getr(int x, int l, int r) {
int L = _max(sty-l, r+1), R = _min(sty+r, m-l);
return L > R ? 0 : row[x][R] - row[x][L-1];
}
int getc(int y, int u, int d) {
int U = _max(stx-u, d+1), D = _min(stx+d, n-u);
return U > D ? 0 : col[D][y] - col[U-1][y];
}
int main() {
int i, j, k;
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i++) scanf("%s", s[i]+1);
for(i = 1; i <= n; i++) for(j = 1; j <= m; j++){
if(s[i][j] == 'o') a[i][j] = 1;
if(s[i][j] == 'E') stx = i, sty = j;
}
for(i = 1; i <= n; i++){
for(j = 1; j <= m; j++) row[i][j] = row[i][j-1]+a[i][j], col[i][j] = col[i-1][j]+a[i][j];
}
int st = 0, ans = 0;;
for(int t = 1; t <= n+m; t++){
st ^= 1;
for(i = 0; i < stx; i++){
for(j = 0; j <= n-stx; j++){
for(k = 0; k < sty; k++){
up(ans, f[st^1][i][j][k]);
f[st][i][j][k] = 0;
}
}
}
for(i = 0; i < stx; i++){
for(j = 0; j <= n-stx; j++){
for(k = 0; k < sty; k++){
int x = t-1-i-j-k;
if(x < 0 || x > m-sty) continue;
if(stx-i-1 > j) up(f[st][i+1][j][k], f[st^1][i][j][k]+getr(stx-i-1, k, x));
if(stx+j+1 <= n-i) up(f[st][i][j+1][k], f[st^1][i][j][k]+getr(stx+j+1, k, x));
if(sty-k-1 > x) up(f[st][i][j][k+1], f[st^1][i][j][k]+getc(sty-k-1, i, j));
if(sty+x+1 <= m-k) up(f[st][i][j][k], f[st^1][i][j][k]+getc(sty+x+1, i, j));
}
}
}
}
for(i = 0; i < stx; i++){
for(j = 0; j <= n-stx; j++){
for(k = 0; k < sty; k++){
up(ans, f[st][i][j][k]);
}
}
}
printf("%d\n", ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
template<class I> I chmax(I &x, I y) { return x < y ? x = y : x; }
template<class I> I chmin(I &x, I y) { return x > y ? x = y : x; }
char s[103];
short F[103][103][103][103];
short s1[103][103], s2[103][103];
int main()
{
int n, m, tx = 0, ty = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
{
scanf("%s", s + 1);
for(int j = 1; j <= m; j++)
{
s1[i][j] = s1[i][j - 1] + (s[j] == 'o');
s2[i][j] = s2[i - 1][j] + (s[j] == 'o');
if(s[j] == 'E') tx = i, ty = j;
}
}
int limu = tx - 1, limd = n - tx;
int liml = ty - 1, limr = m - ty;
for(int u = 0; u <= limu; u++)
for(int d = 0; d <= limd; d++)
for(int l = 0; l <= liml; l++)
for(int r = 0; r <= limr; r++)
{
int L = max(ty - l, r + 1), R = min(ty + r, m - l);
if(L <= R)
{
chmax(F[u + 1][d][l][r], (short) (F[u][d][l][r] + (tx - u - 1 >= d + 1 ? s1[tx - u - 1][R] - s1[tx - u - 1][L - 1] : (short) 0)));
chmax(F[u][d + 1][l][r], (short) (F[u][d][l][r] + (tx + d + 1 <= n - u ? s1[tx + d + 1][R] - s1[tx + d + 1][L - 1] : (short) 0)));
}
L = max(tx - u, d + 1), R = min(tx + d, n - u);
if(L <= R)
{
chmax(F[u][d][l + 1][r], (short) (F[u][d][l][r] + (ty - l - 1 >= r + 1 ? s2[R][ty - l - 1] - s2[L - 1][ty - l - 1] : (short) 0)));
chmax(F[u][d][l][r + 1], (short) (F[u][d][l][r] + (ty + r + 1 <= m - l ? s2[R][ty + r + 1] - s2[L - 1][ty + r + 1] : (short) 0)));
}
}
printf("%d\n", F[limu][limd][liml][limr]);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, ex, ey;
short dp[N][N][N][N], s1[N][N], s2[N][N];
char s[N][N];
inline void up(short& x, short y) { x = (x < y ? y : x); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i][j] == 'E') ex = i, ey = j;
s1[i][j] = s1[i][j - 1] + (s[i][j] == 'o');
s2[i][j] = s2[i - 1][j] + (s[i][j] == 'o');
}
}
int I = ex - 1, J = n - ex, K = ey - 1, L = m - ey;
for (int i = 0; i <= I; i++)
for (int j = 0; j <= J; j++)
for (int k = 0; k <= K; k++)
for (int l = 0; l <= L; l++) {
int L = max(ey - k, l + 1), R = min(ey + l, m - k);
if (L <= R) {
up(dp[i + 1][j][k][l], dp[i][j][k][l] + (ex - i - 1 >= j + 1? s1[ex - i - 1][R] - s1[ex - i - 1][L - 1]: 0));
up(dp[i][j + 1][k][l], dp[i][j][k][l] + (ex + j + 1 <= n - i? s1[ex + j + 1][R] - s1[ex + j + 1][L - 1] : 0));
}
L = max(ex - i, j + 1), R = min(ex + j, n - i);
if (L <= R) {
up(dp[i][j][k + 1][l], dp[i][j][k][l] + (ey - k - 1 >= l + 1? s2[R][ey - k - 1] - s2[L - 1][ey - k - 1] : 0));
up(dp[i][j][k][l + 1], dp[i][j][k][l] + (ey + l + 1 <= m - k? s2[R][ey + l + 1] - s2[L - 1][ey + l + 1] : 0));
}
}
printf("%d", dp[I][J][K][L]);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll ;
#define rep(i, a, b) for (int i = a ; i <= b; ++ i)
const int N = 101, inf = 1e4 ;
using namespace std ;
int n, m ;
char a[N][N] ;
short f[N][N][N][N], sum[2][N][N] ;
void upd(short &x, short y) { if (y > x) x = y ; }
int main() {
scanf("%d%d", &n, &m) ;
rep(i, 1, n) scanf("%s", a[i] + 1) ;
int x, y ;
rep(i, 1, n) rep(j, 1, m) {
sum[0][i][j] = sum[1][i][j] = a[i][j] == 'o' ;
if (a[i][j] == 'E') x = i, y = j ;
sum[0][i][j] += sum[0][i - 1][j], sum[1][i][j] += sum[1][i][j - 1] ;
}
rep(u, 0, x - 1) rep(d, 0, n - x) rep(l, 0, y - 1) rep(r, 0, m - y) f[u][d][l][r] = - inf ;
f[0][0][0][0] = 0 ;
rep(u, 0, x - 1) rep(d, 0, n - x) rep(l, 0, y - 1) rep(r, 0, m - y) {
upd(f[u + 1][d][l][r], f[u][d][l][r] + (x - u - 1 > d ? max(sum[1][x - u - 1][min(m - l, y + r)] - sum[1][x - u - 1][max(r, y - l - 1)], 0) : 0)) ;
upd(f[u][d + 1][l][r], f[u][d][l][r] + (x + d + 1 <= n - u ? max(sum[1][x + d + 1][min(m - l, y + r)] - sum[1][x + d + 1][max(r, y - l - 1)], 0) : 0)) ;
upd(f[u][d][l + 1][r], f[u][d][l][r] + (y - l - 1 > r ? max(sum[0][min(n - u, x + d)][y - l - 1] - sum[0][max(d, x - u - 1)][y - l - 1], 0) : 0)) ;
upd(f[u][d][l][r + 1], f[u][d][l][r] + (y + r + 1 <= m - l ? max(sum[0][min(n - u, x + d)][y + r + 1] - sum[0][max(d, x - u - 1)][y + r + 1], 0) : 0)) ;
}
printf("%d\n", f[x - 1][n - x][y - 1][m - y]) ;
return 0 ;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=100+10;
int f[maxn][maxn][maxn],sum[maxn][maxn];
int i,j,k,l,r,u,d,t,n,m,ex,ey,ans,up,down,left,right;
char ch;
char get(){
char ch=getchar();
while (ch!='.'&&ch!='o'&&ch!='E') ch=getchar();
return ch;
}
int getsum(int a,int b,int x,int y){
return sum[x][y]-sum[a-1][y]-sum[x][b-1]+sum[a-1][b-1];
}
int main(){
scanf("%d%d",&n,&m);
fo(i,1,n)
fo(j,1,m){
ch=get();
if (ch=='E'){
ex=i;
ey=j;
}
else if (ch=='o') sum[i][j]++;
}
fo(i,1,n)
fo(j,1,m)
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
fo(i,0,n)
fo(j,0,m)
fo(k,0,m)
f[i][j][k]=-100000000;
f[0][0][0]=0;
fo(u,0,n){
if (ex-u<1) break;
fo(d,0,n){
if (ex+d>n) break;
up=max(ex-u,1+d);
down=min(ex+d,n-u);
if (up>down) continue;
fo(l,0,m){
if (ey-l<1) break;
fo(r,0,m){
if (ey+r>m) break;
left=max(ey-l,1+r);
right=min(ey+r,m-l);
if (left>right) continue;
ans=max(ans,f[d][l][r]);
if (u+d<n-ex) f[d+1][l][r]=max(f[d+1][l][r],f[d][l][r]+getsum(ex+d+1,left,ex+d+1,right));
if (l+r<ey-1) f[d][l+1][r]=max(f[d][l+1][r],f[d][l][r]+getsum(up,ey-l-1,down,ey-l-1));
if (l+r<m-ey) f[d][l][r+1]=max(f[d][l][r+1],f[d][l][r]+getsum(up,ey+r+1,down,ey+r+1));
if (u+d<ex-1) f[d][l][r]=max(f[d][l][r],f[d][l][r]+getsum(ex-u-1,left,ex-u-1,right));
}
}
}
}
printf("%d\n",ans);
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <iostream>
#define ri register int
using namespace std;
typedef long long LL;
const int N = 102;
char ch[N][N];
short le[N][N], up[N][N], dp[N][N][N][N];
inline void upd(short &x, short y) {
if (x < y) x = y;
}
int main() {
int n, m, ex, ey;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++) {
scanf("%s", ch[i] + 1);
for (int j = 1; j <= m; j ++) {
le[i][j] = le[i][j - 1];
up[i][j] = up[i - 1][j];
if (ch[i][j] == 'o') le[i][j] ++, up[i][j] ++;
if (ch[i][j] == 'E') ex = i, ey = j;
}
}
short t, ans = 0;
for (ri U = ex; U; U --)
for (ri D = ex; D <= n; D ++)
for (ri L = ey; L; L --)
for (ri R = ey; R <= m; R ++) {
t = dp[U][D][L][R];
ans = max(ans, t);
if (U - 1 > D - ex) upd(dp[U - 1][D][L][R], t + le[U - 1][min(R, m - (ey - L))] - le[U - 1][max(L - 1, R - ey)]);
if (n - D > ex - U) upd(dp[U][D + 1][L][R], t + le[D + 1][min(R, m - (ey - L))] - le[D + 1][max(L - 1, R - ey)]);
if (L - 1 > R - ey) upd(dp[U][D][L - 1][R], t + up[min(D, n - (ex - U))][L - 1] - up[max(U - 1, D - ex)][L - 1]);
if (m - R > ey - L) upd(dp[U][D][L][R + 1], t + up[min(D, n - (ex - U))][R + 1] - up[max(U - 1, D - ex)][R + 1]);
}
cout << ans << endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
inline void upmax(short &x,const short &y){if(y>x) x=y;}
const int N=105;short f[N][N][N][N],sum1[N][N],sum2[N][N];
int n,m,tx,ty;
void gao(){
int limu=tx-1,limd=n-tx,liml=ty-1,limr=m-ty;
for(int u=0;u<=limu;u++)for(int d=0;d<=limd;d++)for(int l=0;l<=liml;l++)for(int r=0;r<=limr;r++)
{int L=max(ty-l,r+1),R=min(ty+r,m-l);
if(L<=R){upmax(f[u+1][d][l][r],f[u][d][l][r]+(tx-u-1>=d+1?sum1[tx-u-1][R]-sum1[tx-u-1][L-1]:0));
upmax(f[u][d+1][l][r],f[u][d][l][r]+(tx+d+1<=n-u?sum1[tx+d+1][R]-sum1[tx+d+1][L-1]:0));}
L=max(tx-u,d+1),R=min(tx+d,n-u);
if(L<=R){upmax(f[u][d][l+1][r],f[u][d][l][r]+(ty-l-1>=r+1?sum2[R][ty-l-1]-sum2[L-1][ty-l-1]:0));
upmax(f[u][d][l][r+1],f[u][d][l][r]+(ty+r+1<=m-l?sum2[R][ty+r+1]-sum2[L-1][ty+r+1]:0));}
}printf("%d\n",f[limu][limd][liml][limr]);
}
int main(){
static char s[105];scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){scanf("%s",s+1);for(int j=1;j<=m;j++)
{if(s[j]=='E') tx=i,ty=j;sum1[i][j]=sum1[i][j-1]+(s[j]=='o');sum2[i][j]=sum2[i-1][j]+(s[j]=='o');}}
gao();return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
int h, w;
char a[100][101];
int ex, ey;
int c[101][101];
int dp[100][100][100][100];
int box(int x, int y, int z, int w){
return c[z][w] - c[z][y] - c[x][w] + c[x][y];
}
int main(){
cin >> h >> w;
rep(i, h){
cin >> a[i];
rep(j, w){
if(a[i][j] == 'E'){
ex = i;
ey = j;
}
else if(a[i][j] == 'o'){
c[i + 1][j + 1] = 1;
}
}
}
rep(i, h + 1){
for(int j = 1; j <= w; ++j){
c[i][j] += c[i][j - 1];
}
}
for(int i = 1; i <= h; ++i){
rep(j, w + 1){
c[i][j] += c[i - 1][j];
}
}
int ans = 0;
rep(i, ex + 1){
rep(j, h - ex){
rep(k, ey + 1){
rep(l, w - ey){
ans = max(dp[i][j][k][l], ans);
if(l < w - k && j < h - i){
int p = max(ex - i, j);
int q = min(ex + j + 1, h - i);
int r = max(ey - k, l);
int s = min(ey + l + 1, w - k);
if(r <= s && p <= q){
if(j < p){
dp[i + 1][j][k][l] = max(dp[i + 1][j][k][l], box(p - 1, r, p, s) + dp[i][j][k][l]);
}
if(q < h - i){
dp[i][j + 1][k][l] = max(dp[i][j + 1][k][l], box(q, r, q + 1, s) + dp[i][j][k][l]);
}
if(l < r){
dp[i][j][k + 1][l] = max(dp[i][j][k + 1][l], box(p, r - 1, q, r) + dp[i][j][k][l]);
}
if(s < w - k){
dp[i][j][k][l + 1] = max(dp[i][j][k][l + 1], box(p, s, q, s + 1) + dp[i][j][k][l]);
}
}
}
}
}
}
}
cout << ans << endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
# include <iostream>
# include <string>
# include <algorithm>
# include <vector>
# include <cstring>
# include <stdio.h>
# include <map>
# include <queue>
# define ll long long
# define pii pair<int,int>
# define FOR(a,b) for(int a=1; a<=b; a++)
# define REP(a,b) for(int a=0; a<b; a++)
# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
using namespace std;
int R, C, sum[105][105], sr, sc;
int dp[2][105][105][105];
int rangeSum(int t, int l, int b, int r){
return sum[b][r] - sum[t-1][r] - sum[b][l-1] + sum[t-1][l-1];
}
int main(){
ios :: sync_with_stdio(false);
cin >> R >> C;
FOR(i,R){
FOR(j,C){
char ch;
cin >> ch;
if(ch == 'E') sr=i, sc=j;
sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + (ch == 'o');
}
}
bool cur = 0;
int ans = 0;
REP(u, sr){
REP(d, R-sr+1){
REP(l, sc){
REP(r, C-sc+1){
int res = dp[cur][d][l][r];
int top = max(d+1, sr-u);
int bot = min(sr+d, R-u);
int lft = max(r+1, sc-l);
int rgt = min(sc+r, C-l);
if(u>0 && sr-u-d>0) res = max(res, dp[cur^1][d][l][r] + rangeSum(sr-u, lft, sr-u, rgt));
if(d>0 && sr+d+u<=R) res = max(res, dp[cur][d-1][l][r] + rangeSum(sr+d, lft, sr+d, rgt));
if(l>0 && sc-l-r>0) res = max(res, dp[cur][d][l-1][r] + rangeSum(top, sc-l, bot, sc-l));
if(r>0 && sc+l+r<=C) res = max(res, dp[cur][d][l][r-1] + rangeSum(top, sc+r, bot, sc+r));
dp[cur][d][l][r] = res;
ans = max(ans, res);
}
}
}
cur ^= 1;
}
cout << ans << endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=102;
int i,j,k,n,m,ch,x,y,ans;
int a[N][N],A[N][N],B[N][N];
char s[N];
short f[N][N][N][N];
int main() {
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) {
scanf("%s",s+1);
for (j=1;j<=m;j++) {
if (s[j]=='o') a[i][j]=1;
if (s[j]=='E') x=i,y=j;
}
}
for (i=1;i<=n;i++)
for (j=1;j<=m;j++) A[i][j]=a[i][j]+A[i][j-1];
for (j=1;j<=m;j++)
for (i=1;i<=n;i++) B[i][j]=a[i][j]+B[i-1][j];
for (int u=0;u<=n;u++)
for (int d=0;u+d<n;d++)
for (int l=0;l<=m;l++)
for (int r=0;l+r<m;r++) {
if (x-d<=u+1 && u+1<=x+u) f[u+1][d][l][r]=max(f[u+1][d][l][r],(short) (f[u][d][l][r]+max(0,A[u+1][min(y+l,m-r)]-A[u+1][max(y-r,l+1)-1])));
if (x-d<=n-d && n-d<=x+u) f[u][d+1][l][r]=max(f[u][d+1][l][r],(short) (f[u][d][l][r]+max(0,A[n-d][min(y+l,m-r)]-A[n-d][max(y-r,l+1)-1])));
if (y-r<=l+1 && l+1<=y+l) f[u][d][l+1][r]=max(f[u][d][l+1][r],(short) (f[u][d][l][r]+max(0,B[min(x+u,n-d)][l+1]-B[max(x-d,u+1)-1][l+1])));
if (y-r<=m-r && m-r<=y+l) f[u][d][l][r+1]=max(f[u][d][l][r+1],(short) (f[u][d][l][r]+max(0,B[min(x+u,n-d)][m-r]-B[max(x-d,u+1)-1][m-r])));
}
for (int u=0;u<=n;u++)
for (int d=0;u+d<=n;d++)
for (int l=0;l<=m;l++)
for (int r=0;l+r<=m;r++) ans=max(ans,(int) f[u][d][l][r]);
printf("%d\n",ans);
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=101;
char a[N][N];
int row[N][N],col[N][N],f[N][N][N][N];
int n,m,x,y;
inline void chk(int &a,int b){a=max(a,b);}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i) scanf("%s",a[i]+1);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
row[i][j]=row[i][j-1]+(a[i][j]=='o');
col[i][j]=col[i-1][j]+(a[i][j]=='o');
if(a[i][j]=='E') x=i,y=j;
};
int ans=0;
for(int l=0;l<=y-1;++l)
for(int r=0;r<=m-y;++r)
for(int u=0;u<=x-1;++u)
for(int d=0;d<=n-x;++d)
{
ans=max(ans,f[l][r][u][d]);
int ub=max(x-u,1+d),db=min(x+d,n-u),lb=max(y-l,1+r),rb=min(y+r,m-l);
int nx,ny;
ny=y-l-1;
if(1+r<=ny&&ny<=m-l) chk(f[l+1][r][u][d],f[l][r][u][d]+(db>=ub?(col[db][ny]-col[ub-1][ny]):0));
ny=y+r+1;
if(1+r<=ny&&ny<=m-l) chk(f[l][r+1][u][d],f[l][r][u][d]+(db>=ub?(col[db][ny]-col[ub-1][ny]):0));
nx=x-u-1;
if(1+d<=nx&&nx<=n-u) chk(f[l][r][u+1][d],f[l][r][u][d]+(rb>=lb?(row[nx][rb]-row[nx][lb-1]):0));
nx=x+d+1;
if(1+d<=nx&&nx<=n-u) chk(f[l][r][u][d+1],f[l][r][u][d]+(rb>=lb?(row[nx][rb]-row[nx][lb-1]):0));
};
printf("%d\n",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <algorithm>
const int maxn = 105;
using namespace std;
int n, m, ex, ey; char str[maxn][maxn];
short f[maxn][maxn][maxn][maxn], p[maxn][maxn], q[maxn][maxn];
short max(short x, int y){
if (x < y) return y;
return x;
}
int main(){
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", str[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
p[i][j] = p[i][j - 1]; q[i][j] = q[i - 1][j];
if (str[i][j] == 'E') ex = i, ey = j;
else if (str[i][j] == 'o') p[i][j]++, q[i][j]++;
}
for (int i = 0; i <= ex - 1; i++)
for (int j = 0; j <= ey - 1; j++)
for (int x = 0; x <= n - ex; x++)
for (int y = 0; y <= m - ey; y++){
int L = max(ex - i, x + 1), R = min(n - i, ex + x);
if (L <= R){
f[i][j + 1][x][y] = max(f[i][j + 1][x][y], f[i][j][x][y] + (ey - j - 1 >= y + 1) * (q[R][ey - j - 1] - q[L - 1][ey - j - 1]));
f[i][j][x][y + 1] = max(f[i][j][x][y + 1], f[i][j][x][y] + (m - j >= ey + y + 1) * (q[R][ey + y + 1] - q[L - 1][ey + y + 1]));
}
L = max(ey - j, y + 1), R = min(m - j, ey + y);
if (L <= R){
f[i + 1][j][x][y] = max(f[i + 1][j][x][y], f[i][j][x][y] + (ex - i - 1 >= x + 1) * (p[ex - i - 1][R] - p[ex - i - 1][L - 1]));
f[i][j][x + 1][y] = max(f[i][j][x + 1][y], f[i][j][x][y] + (n - i >= ex + x + 1) * (p[ex + x + 1][R] - p[ex + x + 1][L - 1]));
}
}
printf("%d\n", f[ex - 1][ey - 1][n - ex][m - ey]);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
typedef short sh;
#define N 105
int n,m,sum[N][N];
char mp[N][N];
sh f[N][N][N][N];
inline void upd(sh &x,sh y) {x=x>y?x:y;}
int getsm(int x,int y,int z,int w) {
return sum[z][w]-sum[x-1][w]-sum[z][y-1]+sum[x-1][y-1];
}
int main() {
scanf("%d%d",&n,&m);
int i,j,sx=0,sy=0;
for(i=1;i<=n;i++) scanf("%s",mp[i]+1);
for(i=1;i<=n;i++) {
for(j=1;j<=m;j++) {
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
if(mp[i][j]=='E') sx=i,sy=j;
else if(mp[i][j]=='o') sum[i][j]++;
}
}
int k,l;
for(i=0;i<=n;i++)for(j=0;j<=n;j++)for(k=0;k<=m;k++)for(l=0;l<=m;l++)f[i][j][k][l]=-10000;
f[0][0][0][0]=0;
for(i=0;i<=sx-1;i++) {
for(j=0;j<=n-sx;j++) {
for(k=0;k<=sy-1;k++) {
for(l=0;l<=m-sy;l++) {
sh t=f[i][j][k][l];
if(t<0) continue;
//UP
if(sx-i>1) upd(f[i+1][j][k][l],t+ (sx-i-1>=j+1?getsm(sx-i-1,max(l+1,sy-k),sx-i-1,min(m-k,sy+l)):0) );
//DOWN
if(sx+j<n) upd(f[i][j+1][k][l],t+ (sx+j+1<=n-i?getsm(sx+j+1,max(l+1,sy-k),sx+j+1,min(m-k,sy+l)):0) );
//LEFT
if(sy-k>1) upd(f[i][j][k+1][l],t+ (sy-k-1>=l+1?getsm(max(j+1,sx-i),sy-k-1,min(n-i,sx+j),sy-k-1):0) );
//RIGHT
if(sy+l<m) upd(f[i][j][k][l+1],t+ (sy+l+1<=m-k?getsm(max(j+1,sx-i),sy+l+1,min(n-i,sx+j),sy+l+1):0) );
}
}
}
}
sh ans=0;
for(i=0;i<=n;i++)for(j=0;j<=n;j++)for(k=0;k<=m;k++)for(l=0;l<=m;l++)ans=max(ans,f[i][j][k][l]);
printf("%d\n",int(ans));
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
const int MAXN = 110;
const int NINF = 0xcfcfcfcf;
char buf[MAXN];
int H, W, dp[2][MAXN][MAXN][MAXN];
int pre[MAXN][MAXN];
void getmax(int & x, int y) { x < y ? x = y : 0; }
struct M {
int l, r, u, d;
M(int a1, int a2, int a3, int a4) {
l = a1, r = a2, u = a3, d = a4;
}
} ;
inline int gm(int x) { return x < 0 ? 0 : x; }
inline int in(M a, M b) {
a.l = std::max(a.l, b.l);
a.u = std::max(a.u, b.u);
a.r = std::min(a.r, b.r);
a.d = std::min(a.d, b.d);
if (a.l > a.r || a.u > a.d) return 0;
return pre[a.d][a.r] - pre[a.d][a.l - 1] - pre[a.u - 1][a.r] + pre[a.u - 1][a.l - 1];
}
int main() {
std::ios_base::sync_with_stdio(false), std::cin.tie(0);
std::cin >> H >> W;
int X, Y;
for (int i = 1; i <= H; ++i) {
std::cin >> buf + 1;
for (int j = 1; j <= W; ++j) {
if (buf[j] == 'E') Y = i, X = j;
else if (buf[j] == 'o') pre[i][j] = 1;
pre[i][j] += pre[i][j - 1];
}
}
for (int i = 1; i <= H; ++i)
for (int j = 1; j <= W; ++j)
pre[i][j] += pre[i - 1][j];
int ans = 0;
int now = 1, lst = 0;
memset(dp, 0xcf, sizeof dp);
dp[now][0][0][0] = 0;
for (int u = 0; u < Y; ++u) {
std::swap(lst, now);
for (int d = 0; d <= H - Y; ++d)
for (int l = 0; l < X; ++l)
for (int r = 0; r <= W - X; ++r) {
int & f = dp[now][d][l][r] = 0, t;
M lim(r + 1, W - l, d + 1, H - u);
if (u) {
t = in(M(X - l, X + r, Y - u, Y - u), lim);
getmax(f, dp[lst][d][l][r] + t);
}
if (d) {
t = in(M(X - l, X + r, Y + d, Y + d), lim);
getmax(f, dp[now][d - 1][l][r] + t);
}
if (l) {
t = in(M(X - l, X - l, Y - u, Y + d), lim);
getmax(f, dp[now][d][l - 1][r] + t);
}
if (r) {
t = in(M(X + r, X + r, Y - u, Y + d), lim);
getmax(f, dp[now][d][l][r - 1] + t);
}
getmax(ans, f);
}
}
std::cout << ans << std::endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
const int N = 102;
const short INF = 30000;
short dp[N][N][N][N],n,m,mat[N][N],px,py,ans;
char s[N];
short query(int u,int d,int l,int r) {
if (u > d || l > r) return 0;
return mat[d][r] - mat[u-1][r] - mat[d][l-1] + mat[u-1][l-1];
}
short calc(int u,int d,int l,int r,int u1,int d1,int l1,int r1) {
return query(u,d,l,r) - query(max(u,u1),min(d,d1),max(l,l1),min(r,r1));
}
int main() {
cin >> n >> m;
for (int i = 1 ; i <= n ; ++ i) {
scanf("%s",s+1);
for (int j = 1 ; j <= m ; ++ j) {
mat[i][j] = s[j] == 'o';
if (s[j] == 'E') px = i, py = j;
}
}
for (int i = 1 ; i <= n ; ++ i)
for (int j = 1 ; j <= m ; ++ j)
mat[i][j] += mat[i][j-1];
for (int i = 1 ; i <= n ; ++ i)
for (int j = 1 ; j <= m ; ++ j)
mat[i][j] += mat[i-1][j];
ans = mat[n][m];
for (int s1 = 1 ; s1 <= n ; ++ s1)
for (int s2 = 1 ; s2 <= m ; ++ s2) {
int l = s2 - m + py;
int r = py;
int u = s1 - n + px;
int d = px;
for (int i = 1, j = s1 ; j <= n ; ++ i, ++ j)
for (int a = 1, b = s2 ; b <= m ; ++ a, ++ b) {
dp[i][j][a][b] = INF;
dp[i][j][a][b] = min(dp[i][j][a][b],(short)(dp[i][j-1][a][b]+calc(j,j,a,b,i+u-1,i+d-1,a+l-1,a+r-1)));
dp[i][j][a][b] = min(dp[i][j][a][b],(short)(dp[i+1][j][a][b]+calc(i,i,a,b,i+u-1,i+d-1,a+l-1,a+r-1)));
dp[i][j][a][b] = min(dp[i][j][a][b],(short)(dp[i][j][a+1][b]+calc(i,j,a,a,i+u-1,i+d-1,a+l-1,a+r-1)));
dp[i][j][a][b] = min(dp[i][j][a][b],(short)(dp[i][j][a][b-1]+calc(i,j,b,b,i+u-1,i+d-1,a+l-1,a+r-1)));
}
}
ans -= dp[1][n][1][m];
cout << ans << endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
void chmax(int& a, int b){
a = max(a, b);
}
int main(){
int H, W;
cin >> H >> W;
string S[101];
S[0] = string('!', W+1);
int ER, EC;
for(int i=1; i<=H; i++){
cin >> S[i];
S[i].insert(0, "!");
if(S[i].find('E') != -1){
ER = i;
EC = S[i].find('E');
}
}
int RS[101][101] = {0}, CS[101][101] = {0};
for(int i=1; i<=H; i++) for(int j=1; j<=W; j++){
RS[i][j] = RS[i][j-1];
CS[i][j] = CS[i-1][j];
if(S[i][j] == 'o'){
RS[i][j]++;
CS[i][j]++;
}
}
static int dp[101][101][101][101];
for(int u=0; ER-u>0; u++) for(int d=0; ER+d<=H; d++){
for(int l=0; EC-l>0; l++) for(int r=0; EC+r<=W; r++){
int ub = max(ER-u, d+1), db = min(ER+d, H-u);
int lb = max(EC-l, r+1), rb = min(EC+r, W-l);
if(ER-u-1 > 0){
int add = 0;
if(ER-u-1 >= d+1) chmax(add, RS[ER-u-1][rb] - RS[ER-u-1][lb-1]);
chmax(dp[u+1][d][l][r], dp[u][d][l][r] + add);
}
if(EC-l-1 > 0){
int add = 0;
if(EC-l-1 >= r+1) chmax(add, CS[db][EC-l-1] - CS[ub-1][EC-l-1]);
chmax(dp[u][d][l+1][r], dp[u][d][l][r] + add);
}
if(ER+d+1 <= H){
int add = 0;
if(ER+d+1 <= H-u) chmax(add, RS[ER+d+1][rb] - RS[ER+d+1][lb-1]);
chmax(dp[u][d+1][l][r], dp[u][d][l][r] + add);
}
if(EC+r+1 <= W){
int add = 0;
if(EC+r+1 <= W-l) chmax(add, CS[db][EC+r+1] - CS[ub-1][EC+r+1]);
chmax(dp[u][d][l][r+1], dp[u][d][l][r] + add);
}
}
}
cout << dp[ER-1][H-ER][EC-1][W-EC] << endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
// Do you knOW what it feels like?
// To be TorTured by your own MinD?
// I don't wanna feel the PAIN.
// I BeG you to KILL me, pleASE...
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Os")
#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=1e2+10, lg=22, mod=1e9+7, inf=1e18;
ll n,m,S[maxn][maxn],dp[maxn][maxn],ans;
bool a[maxn][maxn];
ll query(ll x,ll y,ll z,ll w){
return S[z][w]+S[x][y]-S[x][w]-S[z][y];
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
char ch;
cin>>n>>m;
ll X,Y;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
cin>>ch;
if(ch=='o') a[i][j]=1;
if(ch=='E') X=i, Y=j;
}
if(X-1>n-X){
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(i>n+1-i) swap(a[i][j],a[n+1-i][j]);
X=n+1-X;
}
if(Y-1>m-Y){
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(j>m+1-j) swap(a[i][j],a[i][m+1-j]);
Y=m+1-Y;
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) S[i][j]=a[i][j];
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) S[i][j]+=S[i][j-1];
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) S[i][j]+=S[i-1][j];
ll _n=n-(X-1)-(X-1), _m=m-(Y-1)-(Y-1);
for(int x=0;x<X;x++)for(int y=0;y<Y;y++){
dp[0][1]=query(x,y,X+x-1,Y+y);
dp[1][0]=query(x,y,X+x,Y+y-1);
for(int i=1;i<=_n;i++)for(int j=1;j<=_m;j++) dp[i][j]=max(query(i-2+X+x,j-1+y,i-1+X+x,j-1+Y+y)+dp[i-1][j],query(i-1+x,j-2+Y+y,i-1+X+x,j-1+Y+y)+dp[i][j-1]);
ans=max(ans,dp[_n][_m]);
}
cout<<ans;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
int line[110][110],col[110][110];
short dp[101][101][101][101];
int main()
{
int n,m,x,y;scanf("%d%d\n",&n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
line[i][j]=line[i][j-1];col[i][j]=col[i-1][j];
char ch=getchar();
if (ch=='E') {x=i;y=j;}
else if (ch=='o') {line[i][j]++;col[i][j]++;}
if (j==m) scanf("\n");
}
int all=0;
for (int i=x;i>=1;i--)
for (int j=y;j>=1;j--)
for (int k=x;k<=n;k++)
for (int l=y;l<=m;l++)
{
all=max(all,(int)dp[i][j][k][l]);
if ((1<i)&&(k+1<x+i)) dp[i-1][j][k][l]=max((int)dp[i-1][j][k][l],dp[i][j][k][l]+line[i-1][min(l,m-y+j)]-line[i-1][max(j-1,l-y)]);
if ((k<n)&&(x+k<n+i)) dp[i][j][k+1][l]=max((int)dp[i][j][k+1][l],dp[i][j][k][l]+line[k+1][min(l,m-y+j)]-line[k+1][max(j-1,l-y)]);
if ((1<j)&&(l+1<y+j)) dp[i][j-1][k][l]=max((int)dp[i][j-1][k][l],dp[i][j][k][l]+col[min(k,n-x+i)][j-1]-col[max(i-1,k-x)][j-1]);
if ((l<m)&&(y+l<m+j)) dp[i][j][k][l+1]=max((int)dp[i][j][k][l+1],dp[i][j][k][l]+col[min(k,n-x+i)][l+1]-col[max(i-1,k-x)][l+1]);
}
cout<<all<<endl;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
char s[maxn][maxn];
int r[maxn][maxn], c[maxn][maxn];
int f[maxn * maxn >> 2][maxn * maxn >> 2];
int h, w, X, Y;
inline int encode(int l, int r, int M) {
return r * M + l;
}
inline void chkmax(int& x, int v) {
x = max(x, v);
}
int dfs(int lX, int rX, int lY, int rY) {
int cX = encode(lX, rX, X), cY = encode(lY, rY, Y);
if (f[cX][cY] >= 0) return f[cX][cY]; f[cX][cY] = 0;
if (lX < X - 1) {
chkmax(f[cX][cY], dfs(lX + 1, rX, lY, rY) + (rX + 1 <= X - lX - 1 ? max(0, r[X - lX - 1][min(Y + rY, w - lY)] - r[X - lX - 1][max(Y - lY, rY + 1) - 1]) : 0));
}
if (rX < h - X) {
chkmax(f[cX][cY], dfs(lX, rX + 1, lY, rY) + (X + rX + 1 <= h - lX ? max(0, r[X + rX + 1][min(Y + rY, w - lY)] - r[X + rX + 1][max(Y - lY, rY + 1) - 1]) : 0));
}
if (lY < Y - 1) {
chkmax(f[cX][cY], dfs(lX, rX, lY + 1, rY) + (rY + 1 <= Y - lY - 1 ? max(0, c[min(X + rX, h - lX)][Y - lY - 1] - c[max(X - lX, rX + 1) - 1][Y - lY - 1]) : 0));
}
if (rY < w - Y) {
chkmax(f[cX][cY], dfs(lX, rX, lY, rY + 1) + (Y + rY + 1 <= w - lY ? max(0, c[min(X + rX, h - lX)][Y + rY + 1] - c[max(X - lX, rX + 1) - 1][Y + rY + 1]) : 0));
}
return f[cX][cY];
}
int main() {
scanf("%d%d", &h, &w);
for (int i = 1; i <= h; ++i) scanf("%s", s[i] + 1);
for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j)
if (s[i][j] == 'E') X = i, Y = j;
for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j) {
r[i][j] = r[i][j - 1] + (s[i][j] == 'o');
c[i][j] = c[i - 1][j] + (s[i][j] == 'o');
}
memset(f, -1, sizeof(f));
return printf("%d\n", dfs(0, 0, 0, 0)), 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <algorithm>
#define Rep(i, n) for (int i = 1; i <= n; i ++)
#define Rep0(i, n) for (int i = 0; i <= n; i ++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
using namespace std;
const int N = 101;
char g[N][N];
short s[N][N];
short f[N][N][N][N];
void inc(short &a, short b) { if (b > a) a = b;}
short get(int x1, int y1, int x2, int y2)
{
if (x1 > x2 || y1 > y2) return -10000;
x1 --, y1 --;
return s[x2][y2] - s[x2][y1] - s[x1][y2] + s[x1][y1];
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int ex, ey;
Rep(i, n) {
scanf("%s", g[i] + 1);
Rep(j, m) {
s[i][j] = s[i][j - 1];
if (g[i][j] == 'o') s[i][j] ++;
else if (g[i][j] == 'E') ex = i, ey = j;
}
}
Rep(i, n) Rep(j, m) s[i][j] += s[i - 1][j];
short ans = 0;
Rep0(i, ex - 1) Rep0(j, ey - 1) Rep0(k, n - ex) Rep0(h, m - ey){
//printf("%d %d %d %d %d\n", i, j, k, h, (int)f[i][j][k][h]);
int lx = max(1 + k, ex - i), rx = min(n - i, ex + k);
int ly = max(1 + h, ey - j), ry = min(m - j, ey + h);
short tmp = f[i][j][k][h];
if (1 + k <= ex - i - 1 && ex - i - 1 <= n - i)
inc(f[i + 1][j][k][h], tmp + get(ex - i - 1, ly, ex - i - 1, ry));
if (1 + h <= ey - j - 1 && ey - j - 1 <= m - j)
inc(f[i][j + 1][k][h], tmp + get(lx, ey - j - 1, rx, ey - j - 1));
if (1 + k <= ex + k + 1 && ex + k + 1 <= n - i)
inc(f[i][j][k + 1][h], tmp + get(ex + k + 1, ly, ex + k + 1, ry));
if (1 + h <= ey + h + 1 && ey + h + 1 <= m - j)
inc(f[i][j][k][h + 1], tmp + get(lx, ey + h + 1, rx, ey + h + 1));
inc(ans, tmp);
}
printf("%d\n", (int)ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
using namespace std;
const int maxn = 105;
int f[maxn][maxn][maxn], s[maxn][maxn], n, m, sx, sy, ans;
char str[maxn];
inline void upd(int &x, int y) {
if (y > x) x = y;
}
inline int getr(int x, int l, int r) {
return s[x][r] - s[x][l-1] - s[x-1][r] + s[x-1][l-1];
}
inline int getc(int y, int u, int d) {
return s[d][y] - s[u-1][y] - s[d][y-1] + s[u-1][y-1];
}
int main() {
scanf("%d%d", &n, &m);
rep (i, 1, n) {
scanf("%s", str + 1);
rep (j, 1, m) {
if (str[j] == 'E') sx = i, sy = j;
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1];
if (str[j] == 'o') s[i][j]++;
}
}
memset(f, 0xc0, sizeof f); f[0][0][0] = 0;
rep (u, 0, sx-1) rep (d, 0, n-sx)
rep (l, 0, sy-1) rep (r, 0, m-sy) {
if (sx + d + 1 <= n - u)
upd(f[d+1][l][r], f[d][l][r] + getr(sx + d + 1, max(r+1, sy-l), min(m-l, sy+r)));
if (sy - l - 1 >= r + 1)
upd(f[d][l+1][r], f[d][l][r] + getc(sy - l - 1, max(d+1, sx-u), min(n-u, sx+d)));
if (sy + r + 1 <= m - l)
upd(f[d][l][r+1], f[d][l][r] + getc(sy + r + 1, max(d+1, sx-u), min(n-u, sx+d)));
if (sx - u - 1 >= d + 1)
upd(f[d][l][r], f[d][l][r] + getr(sx - u - 1, max(r+1, sy-l), min(m-l, sy+r)));
upd(ans, f[d][l][r]);
// printf("%d %d %d %d %d\n", u, d, l, r, f[d][l][r]);
}
printf("%d\n", ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef short int i16;
typedef unsigned short int u16;
const int MAXN = 100 + 5;
int answer;
int N, M;
i16 ex, ey;
i16 row[MAXN][MAXN], col[MAXN][MAXN];
i16 dp[MAXN][MAXN][MAXN][MAXN];
char mp[MAXN][MAXN];
int main()
{
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i++)
scanf("%s", mp[i] + 1);
for (i16 i = 1; i <= N; i++)
for (i16 j = 1; j <= M; j++)
{
row[i][j] = row[i][j - 1] + (mp[i][j] == 'o'), col[i][j] =
col[i - 1][j] + (mp[i][j] == 'o');
if (mp[i][j] == 'E')
ex = i, ey = j;
}
for (i16 i = ex; i >= 1; i--)
for (i16 j = ey; j >= 1; j--)
for (i16 k = ex; k <= N; k++)
for (i16 l = ey; l <= M; l++)
{
i16 bl, bh;
bl = max((int)j - 1, l - ey), bh = min((int)l, M - ey + j);
if (i > 1 && k - ex < i - 1)
dp[i - 1][j][k][l] = max((int)dp[i - 1][j][k][l],
dp[i][j][k][l] + row[i - 1][bh]
- row[i - 1][bl]);
if (k < N && ex + k < N + i)
dp[i][j][k + 1][l] = max((int)dp[i][j][k + 1][l],
dp[i][j][k][l] + row[k + 1][bh]
- row[k + 1][bl]);
bl = max((int)i - 1, k - ex), bh = min((int)k, N - ex + i);
if (j > 1 && l - ey < j - 1)
dp[i][j - 1][k][l] = max((int)dp[i][j - 1][k][l],
dp[i][j][k][l] + col[bh][j - 1]
- col[bl][j - 1]);
if (l < M && ey + l < M + j)
dp[i][j][k][l + 1] = max((int)dp[i][j][k][l + 1],
dp[i][j][k][l] + col[bh][l + 1]
- col[bl][l + 1]);
answer = max(answer, (int) max(
{ dp[i - 1][j][k][l], dp[i][j - 1][k][l],
dp[i][j][k + 1][l], dp[i][j][k][l + 1] }));
}
printf("%d\n", answer);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
using namespace std;
const int N=105;
char mp[N][N];
int n,m,ex,ey,ans;
int sr[N][N],sc[N][N];
int dp[N][N][N];
void mx(int &x,int y){
x<y?x=y:0;
}
int main(){
scanf("%d%d",&n,&m);
memset(dp,233,sizeof(dp));
For(i,1,n) scanf("%s",mp[i]+1);
For(i,1,n) For(j,1,m){
if (mp[i][j]=='o') sr[i][j]=sc[i][j]=1;
else if (mp[i][j]=='E') ex=i,ey=j;
sr[i][j]+=sr[i][j-1],sc[i][j]+=sc[i-1][j];
}
int L=ey-1,R=m-ey,U=ex-1,D=n-ex; dp[0][0][0]=0;
For(l,0,L) For(r,0,R) For(u,0,U) For(d,0,D){
int v=dp[r][u][d]; ans=max(ans,v);
int up=max(ex-u,d+1),dn=min(ex+d,n-u);
int le=max(ey-l,r+1),ri=min(ey+r,m-l);
if (up>dn||le>ri) continue;
if (u<U-d) mx(dp[r][u+1][d],v+sr[ex-u-1][ri]-sr[ex-u-1][le-1]);
if (d<D-u) mx(dp[r][u][d+1],v+sr[ex+d+1][ri]-sr[ex+d+1][le-1]);
if (r<R-l) mx(dp[r+1][u][d],v+sc[dn][ey+r+1]-sc[up-1][ey+r+1]);
if (l<L-r) mx(dp[r][u][d],v+sc[dn][ey-l-1]-sc[up-1][ey-l-1]);
}
printf("%d",ans);
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
template<typename T> void amax(T &a, T b){ a = max(a, b); }
const int maxn = 101;
short dp[maxn][maxn][maxn][maxn];
int acum[maxn][maxn];
char g[maxn][maxn];
int main(){
int n, m; cin >> n >> m;
int xe = -1, ye = -1;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
cin >> g[i][j];
if(g[i][j] == 'E'){
xe = i;
ye = j;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
acum[i + 1][j + 1] = (g[i][j] == 'o') + acum[i + 1][j] + acum[i][j + 1] - acum[i][j];
}
}
function<int (int,int,int,int)> get = [&](int a, int b, int c, int d){
a++; b++; c++; d++;
return acum[c][d] - acum[a - 1][d] - acum[c][b - 1] + acum[a - 1][b - 1];
};
for(int l = xe; l >= 0; l--){
for(int d = ye; d >= 0; d--){
for(int r = xe; r < n; r++){
for(int u = ye; u < m; u++){
short now = dp[l][d][r][u];
int in_u = (u - ye);
int to_d = (m - (ye - d) - 1);
int in_r = (r - xe);
int to_l = (n - (xe - l) - 1);
if(l > 0){
short inc = 0;
if(l - 1 >= in_r && in_u <= to_d) inc = get(l - 1, max(in_u, d), l - 1, min(to_d, u));
amax(dp[l - 1][d][r][u], (short)(now + inc));
}
if(r + 1 < n){
short inc = 0;
if(r + 1 <= to_l && in_u <= to_d) inc = get(r + 1, max(in_u, d), r + 1, min(to_d, u));
amax(dp[l][d][r + 1][u], (short)(now + inc));
}
if(d > 0){
short inc = 0;
if(d - 1 >= in_u && in_r <= to_l) inc = get(max(in_r, l), d - 1, min(to_l, r), d - 1);
amax(dp[l][d - 1][r][u], (short)(now + inc));
}
if(u + 1 < m){
short inc = 0;
if(u + 1 <= to_d && in_r <= to_l) inc = get(max(in_r, l), u + 1, min(to_l, r), u + 1);
amax(dp[l][d][r][u + 1], (short)(now + inc));
}
}
}
}
}
cout << dp[0][0][n - 1][m - 1] << endl;
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=110;
short dp[maxn][maxn][maxn][maxn];
int n,m, tx,ty, add;
short sumc[maxn][maxn], sumr[maxn][maxn], ans;
char s[maxn][maxn];
void chkmax(short& x,short y){ if(x<y) x=y; }
short calc(short* sum,int l,int r){
if(l>r) return 0;
return sum[r]-sum[l-1];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++) if(s[i][j]=='E') tx=i, ty=j;
}
swap(tx, ty);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) sumr[i][j]=sumr[i][j-1]+(s[i][j]=='o');
for(int j=1;j<=m;j++) for(int i=1;i<=n;i++) sumc[j][i]=sumc[j][i-1]+(s[i][j]=='o');
swap(n, m);
for(int i=0;i<tx;i++) for(int j=0;j<ty;j++) for(int k=0;k<=n-tx;k++) for(int l=0;l<=m-ty;l++){
int v=min(ty+l, m-j), u=max(ty-j, l+1);
int b=min(tx+k, n-i), a=max(tx-i, k+1);
int now=dp[i][j][k][l];
if(i<tx-1){
if(tx-i-1>k) add=calc(sumc[tx-i-1], u, v); else add=0;
chkmax( dp[i+1][j][k][l], now+add );
}
if(j<ty-1){
if(ty-j-1>l) add=calc(sumr[ty-j-1], a, b); else add=0;
chkmax( dp[i][j+1][k][l], now+add );
}
if(k<n-tx){
if(tx+k+1<=n-i) add=calc(sumc[tx+k+1], u, v); else add=0;
chkmax( dp[i][j][k+1][l], now+add );
}
if(l<m-ty){
if(ty+l+1<=m-j) add=calc(sumr[ty+l+1], a, b); else add=0;
chkmax( dp[i][j][k][l+1], now+add );
}
chkmax( ans, dp[i][j][k][l] );
}
printf("%d",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int res,ll,rr,uu,dd,ad,R,L,D,U,sx,sy,h,w,pr[105][105],pc[105][105],dp[105][105][105];
char s[105][105];
int main()
{
scanf("%d%d",&h,&w);
for (int i=1;i<=h;++i)
scanf("%s",s[i]+1);
for (int i=1;i<=h;++i)
for (int j=1;j<=w;++j) {
pr[i][j]=pr[i-1][j]+(s[i][j]=='o');
pc[i][j]=pc[i][j-1]+(s[i][j]=='o');
if (s[i][j]=='E')
sx=i,sy=j;
}
memset(dp,-1,sizeof dp);
dp[0][0][0]=0;
L=sy-1,R=w-sy,U=sx-1,D=h-sx;
for (int l=0;l<=L;++l)
for (int r=0;r<=R;++r)
for (int u=0;u<=U;++u)
for (int d=0;d<=D;++d) {
if (dp[r][u][d]==-1)
continue;
rr=min(sy+r,w-l);
ll=max(sy-l,r+1);
uu=max(sx-u,d+1);
dd=min(sx+d,h-u);
res=max(res,dp[r][u][d]);
if (l+r<R) {
ad=pr[dd][sy+r+1]-pr[uu-1][sy+r+1];
dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+ad);
}
if (u+d<U) {
ad=pc[sx-u-1][rr]-pc[sx-u-1][ll-1];
dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+ad);
}
if (u+d<D) {
ad=pc[sx+d+1][rr]-pc[sx+d+1][ll-1];
dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+ad);
}
if (l+r<L) {
ad=pr[dd][sy-l-1]-pr[uu-1][sy-l-1];
dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+ad);
}
}
printf("%d\n",res);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
string g[101];
short dp[101][101][101][101];
short px[101][101],py[101][101];
void update(short& x,short y){
x=max(x,y);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>g[i];
for(int i=1;i<=n;i++)
g[i].insert(0," ");
int sx,sy;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(g[i][j]=='E'){
sx=i,sy=j;
}
if(g[i][j]=='o'){
px[i][j]=py[i][j]=1;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
px[i][j]+=px[i-1][j];
py[i][j]+=py[i][j-1];
}
for(int i=sx;i>=1;i--)
for(int j=sy;j>=1;j--)
for(int ii=sx;ii<=n;ii++)
for(int jj=sy;jj<=m;jj++){
if(i>1){
update(dp[i-1][j][ii][jj],dp[i][j][ii][jj]);
int z=max(jj-sy+1,j),zz=min(m-sy+j,jj);
if(ii-sx+1<i&&z<=zz)
update(dp[i-1][j][ii][jj],dp[i][j][ii][jj]+py[i-1][zz]-py[i-1][z-1]);
}
if(ii<n){
update(dp[i][j][ii+1][jj],dp[i][j][ii][jj]);
int z=max(jj-sy+1,j),zz=min(m-sy+j,jj);
if(n-sx+i>ii&&z<=zz)
update(dp[i][j][ii+1][jj],dp[i][j][ii][jj]+py[ii+1][zz]-py[ii+1][z-1]);
}
if(j>1){
update(dp[i][j-1][ii][jj],dp[i][j][ii][jj]);
int z=max(ii-sx+1,i),zz=min(n-sx+i,ii);
if(jj-sy+1<j&&z<=zz)
update(dp[i][j-1][ii][jj],dp[i][j][ii][jj]+px[zz][j-1]-px[z-1][j-1]);
}
if(jj<m){
update(dp[i][j][ii][jj+1],dp[i][j][ii][jj]);
int z=max(ii-sx+1,i),zz=min(n-sx+i,ii);
if(m-sy+j>jj&&z<=zz)
update(dp[i][j][ii][jj+1],dp[i][j][ii][jj]+px[zz][jj+1]-px[z-1][jj+1]);
}
}
cout<<dp[1][1][n][m]<<endl;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
const int N=110;
int h,w,px,py,f[N][N][N][N],col[N][N],row[N][N];
bool mp[N][N];
void Init()
{
scanf("%d%d",&h,&w);
for (int i=1;i<=h;++i)
for (int j=1;j<=w;++j)
{
char ch=getchar();
while (ch!='o' && ch!='.' && ch!='E') ch=getchar();
if (ch=='E') px=i,py=j;
if (ch=='o') mp[i][j]=1;
else mp[i][j]=0;
}
}
int Col(int r,int l,int x)
{
if (l>r) return 0;
return col[r][x]-col[l][x];
}
int Row(int r,int l,int x)
{
if (l>r) return 0;
return row[x][r]-row[x][l];
}
void Solve()
{
for (int i=1;i<=h;++i)
for (int j=1;j<=w;++j)
{
col[i][j]=col[i-1][j]+mp[i][j];
row[i][j]=row[i][j-1]+mp[i][j];
}
int ans=0;
for (int a=px;a>0;--a)
for (int b=px;b<=h;++b)
for (int c=py;c>0;--c)
for (int d=py;d<=w;++d) f[a][b][c][d]=-1e9;
f[px][px][py][py]=0;
for (int a=px;a>0;--a)
for (int b=px;b<=h;++b)
for (int c=py;c>0;--c)
for (int d=py;d<=w;++d)
{
if (a>1 && a-1>b-px)
f[a-1][b][c][d]=max(f[a-1][b][c][d],f[a][b][c][d]+Row(min(w-(py-c),d),max(c-1,d-py),a-1));
if (b<h && h-b>px-a)
f[a][b+1][c][d]=max(f[a][b+1][c][d],f[a][b][c][d]+Row(min(w-(py-c),d),max(c-1,d-py),b+1));
if (c>1 && c-1>d-py)
f[a][b][c-1][d]=max(f[a][b][c-1][d],f[a][b][c][d]+Col(min(h-(px-a),b),max(a-1,b-px),c-1));
if (d<w && w-d>py-c)
f[a][b][c][d+1]=max(f[a][b][c][d+1],f[a][b][c][d]+Col(min(h-(px-a),b),max(a-1,b-px),d+1));
ans=max(ans,f[a][b][c][d]);
}
printf("%d\n",ans);
}
int main()
{
Init();
Solve();
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
#define RI register int
const int N=105;
char mp[N][N];
int f[2][N][N][N],a[N][N],n,m,sx,sy,ans;
int gets(int x1,int y1,int x2,int y2)
{return a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1];}
int main()
{
scanf("%d%d",&n,&m);
for(RI i=1;i<=n;++i) {
scanf("%s",mp[i]+1);
for(RI j=1;j<=m;++j) {
a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+(mp[i][j]=='o');
if(mp[i][j]=='E') sx=i,sy=j;
}
}
for(RI x1=0,t=0;x1<sx;++x1,t^=1)
for(RI y1=0;y1<sy;++y1)
for(RI x2=0;x2<=n-sx;++x2)
for(RI y2=0;y2<=m-sy;++y2) {
if(x1&&sx-x1>x2)
f[t][y1][x2][y2]=max(f[t][y1][x2][y2],f[t^1][y1][x2][y2]+
gets(sx-x1,max(sy-y1,y2+1),sx-x1,min(sy+y2,m-y1)));
if(y1&&sy-y1>y2)
f[t][y1][x2][y2]=max(f[t][y1][x2][y2],f[t][y1-1][x2][y2]+
gets(max(sx-x1,x2+1),sy-y1,min(sx+x2,n-x1),sy-y1));
if(x2<n&&sx+x2<=n-x1)
f[t][y1][x2][y2]=max(f[t][y1][x2][y2],f[t][y1][x2-1][y2]+
gets(sx+x2,max(sy-y1,y2+1),sx+x2,min(sy+y2,m-y1)));
if(y2<m&&sy+y2<=m-y1)
f[t][y1][x2][y2]=max(f[t][y1][x2][y2],f[t][y1][x2][y2-1]+
gets(max(sx-x1,x2+1),sy+y2,min(sx+x2,n-x1),sy+y2));
ans=max(ans,f[t][y1][x2][y2]);
}
printf("%d\n",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int H,W;
string S[101];
int SY,SX;
int dp[101][101][101][101];
int sum[105][105];
int range(int L,int R,int T,int B) {
if(L>R || T>B) return 0;
if(L>=W || R<0 || T>=H || B<0) return 0;
return sum[B+1][R+1]-sum[T][R+1]-sum[B+1][L]+sum[T][L];
}
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>H>>W;
FOR(y,H) {
cin>>S[y];
FOR(x,W) {
if(S[y][x]=='E') S[y][x]='.', SY=y,SX=x;
if(S[y][x]=='o') sum[y+1][x+1]=1;
sum[y+1][x+1] += sum[y][x+1] + sum[y+1][x] - sum[y][x];
}
}
int L,R,U,D;
for(L=0;SX-L>=0;L++) for(R=0;SX+R<W;R++) for(U=0;SY-U>=0;U++) for(D=0;SY+D<H;D++) {
int& ret=dp[L][R][U][D];
//_P("%d %d %d %d : %d\n",L,R,U,D,ret);
int AL=max(SX-L,R);
int AR=min(SX+R,W-1-L);
int AT=max(SY-U,D);
int AB=min(SY+D,H-1-U);
dp[L][R+1][U][D]=max(dp[L][R+1][U][D], ret+((AR+1<W-L)?range(AR+1,AR+1,AT,AB):0));
dp[L+1][R][U][D]=max(dp[L+1][R][U][D], ret+((R+1<=AL)?range(AL-1,AL-1,AT,AB):0));
dp[L][R][U][D+1]=max(dp[L][R][U][D+1], ret+((AB+1<H-U)?range(AL,AR,AB+1,AB+1):0));
dp[L][R][U+1][D]=max(dp[L][R][U+1][D], ret+((D+1<=AT)?range(AL,AR,AT-1,AT-1):0));
}
cout<<dp[SX][W-1-SX][SY][H-1-SY]<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n';
FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
solve(); return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
int n,m,px,py,s1[107][107],s2[107][107],s3[107][107];
char s0[107][107];
short f[107][107][107][107],ans=0;
void maxs(short&a,int b){if(a<b)a=b;}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%s",s0[i]+1);
for(int j=1;j<=m;++j){
if(s0[i][j]=='E')px=i,py=j;
if(s0[i][j]=='o'){
s1[i][j]=s2[i][j]=s3[i][j]=1;
}
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
s1[i][j]+=s1[i][j-1];
s2[i][j]+=s2[i-1][j];
s3[i][j]=s3[i-1][j]+s1[i][j];
}
}
for(int l=1;l<=n;++l){
for(int r=n,xl,xr;r>=l;--r){
xl=max(l,px-(n-r));
xr=min(r,px+(l-1));
if(xl>xr)continue;
for(int u=1;u<=m;++u){
for(int d=m,xu,xd,v;d>=u;--d){
xu=max(u,py-(m-d));
xd=min(d,py+(u-1));
if(xu>xd)continue;
v=f[l][r][u][d];
// printf("[%d,%d][%d,%d] [%d,%d][%d,%d] %d\n",l,r,u,d,xl,xr,xu,xd,v);
maxs(ans,v+s3[xr][xd]+s3[xl-1][xu-1]-s3[xr][xu-1]-s3[xl-1][xd]);
maxs(f[l+1][r][u][d],(l==xl?s1[xl][xd]-s1[xl][xu-1]+v:v));
maxs(f[l][r-1][u][d],(r==xr?s1[xr][xd]-s1[xr][xu-1]+v:v));
maxs(f[l][r][u+1][d],(u==xu?s2[xr][xu]-s2[xl-1][xu]+v:v));
maxs(f[l][r][u][d-1],(d==xd?s2[xr][xd]-s2[xl-1][xd]+v:v));
}
}
}
}
printf("%d\n",(int)ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
#define dmax(x, y) x = max(x, y);
#define N 110
#define M 10000001
int n, m, addx, addy, ans;
int a[N][N], b[N][N], f[M];
char c[N];
inline int pos(int i, int j, int k, int l) {
return ((i * (n - addx + 1) + j) * addy + k) * (m - addy + 1) + l + 1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", c + 1);
for (int j = 1; j <= m; j++) {
a[i][j] = a[i][j - 1] + (c[j] == 'o');
b[i][j] = b[i - 1][j] + (c[j] == 'o');
if (c[j] == 'E') addx = i, addy = j;
}
}
for (int i = 0; i < addx; i++)
for (int j = 0; j <= n - addx; j++)
for (int k = 0; k < addy; k++)
for (int l = 0; l <= m - addy; l++) {
int s = f[pos(i, j, k, l)];
ans = max(ans, s);
if (addx - i - 1 > j)
dmax(f[pos(i + 1, j, k, l)],
s + a[addx - i - 1][min(addy + l, m - k)] -
a[addx - i - 1][max(addy - k, l + 1) - 1]);
if (addx + j + 1 <= n - i)
dmax(f[pos(i, j + 1, k, l)],
s + a[addx + j + 1][min(addy + l, m - k)] -
a[addx + j + 1][max(addy - k, l + 1) - 1]);
if (addy - k - 1 > l)
dmax(f[pos(i, j, k + 1, l)],
s + b[min(addx + j, n - i)][addy - k - 1] -
b[max(addx - i - 1, j)][addy - k - 1]);
if (addy + l + 1 <= m - k)
dmax(f[pos(i, j, k, l + 1)],
s + b[min(addx + j, n - i)][addy + l + 1] -
b[max(addx - i - 1, j)][addy + l + 1]);
}
printf("%d\n", ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<stdio.h>
#include<iostream>
#include<algorithm>
#define N 105
using namespace std;
short line[N][N],list[N][N],ans[N][N][N][N],n,m,op,sx,sy;
char s[N][N];
int main()
{
cin>>n>>m;
for (short i=1;i<=n;i++) scanf("%s",s[i]+1);
for (short i=1;i<=n;i++) for (short j=1;j<=m;j++)
{
line[i][j]=line[i][j-1]+(s[i][j]=='o');
list[i][j]=list[i-1][j]+(s[i][j]=='o');
if (s[i][j]=='E') sx=i,sy=j;
}
for (short i=sx;i;i--)
for (short j=sy;j;j--)
for (short k=sx;k<=n;k++)
for (short p=sy;p<=m;p++)
{
if (1<i && k+1<sx+i) op=max(op,ans[i-1][j][k][p]=max((int)ans[i-1][j][k][p],ans[i][j][k][p]+line[i-1][min((int)p,m-sy+j)]-line[i-1][max(j-1,p-sy)]));
if (k<n && sx+k<n+i) op=max(op,ans[i][j][k+1][p]=max((int)ans[i][j][k+1][p],ans[i][j][k][p]+line[k+1][min((int)p,m-sy+j)]-line[k+1][max(j-1,p-sy)]));
if (1<j && p+1<sy+j) op=max(op,ans[i][j-1][k][p]=max((int)ans[i][j-1][k][p],ans[i][j][k][p]+list[min((int)k,n-sx+i)][j-1]-list[max(i-1,k-sx)][j-1]));
if (p<m && sy+p<m+j) op=max(op,ans[i][j][k][p+1]=max((int)ans[i][j][k][p+1],ans[i][j][k][p]+list[min((int)k,n-sx+i)][p+1]-list[max(i-1,k-sx)][p+1]));
}
cout<<op;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
template<class T1, class T2>
void chmax(T1 &a, T2 b) {
if (a < b) a = b;
}
int h, w;
char g[101][101];
int sh[111][111], sv[111][111];
int main() {
cin >> h >> w;
int y, x;
for (int i = 0; i < h; i++) {
scanf("%s", g[i]);
for (int j = 0; j < w; j++) {
if (g[i][j] == 'E') y = i, x = j;
if (g[i][j] == 'o') {
sh[i][j + 1] = 1;
sv[j][i + 1] = 1;
}
}
}
for (int i = 0; i < h; i++) {
partial_sum(sh[i], sh[i] + w + 1, sh[i]);
}
for (int j = 0; j < w; j++) {
partial_sum(sv[j], sv[j] + h + 1, sv[j]);
}
int L = x;
int R = w - x - 1;
int U = y;
int D = h - y - 1;
typedef vector<int> v1;
typedef vector<v1> v2;
typedef vector<v2> v3;
typedef vector<v3> v4;
v4 dp(L + 2, v3(R + 2, v2(U + 2, v1(D + 2, -1e9))));
dp[0][0][0][0] = 0;
int ans = 0;
for (int l = 0; l <= L; l++) {
for (int r = 0; r <= R; r++) {
for (int u = 0; u <= U; u++) {
for (int d = 0; d <= D; d++) {
int ll = r;
int rr = w - l;
int uu = d;
int dd = h - u;
// left
if (x - l - 1 >= ll) {
chmax(dp[l + 1][r][u][d], dp[l][r][u][d] + sv[x - l - 1][min(y + d + 1, dd)] - sv[x - l - 1][max(y - u, uu)]);
}
// right
if (x + r + 1 < rr) {
chmax(dp[l][r + 1][u][d], dp[l][r][u][d] + sv[x + r + 1][min(y + d + 1, dd)] - sv[x + r + 1][max(y - u, uu)]);
}
// up
if (y - u - 1 >= uu) {
chmax(dp[l][r][u + 1][d], dp[l][r][u][d] + sh[y - u - 1][min(x + r + 1, rr)] - sh[y - u - 1][max(x - l, ll)]);
}
// down
if (y + d + 1 < dd) {
chmax(dp[l][r][u][d + 1], dp[l][r][u][d] + sh[y + d + 1][min(x + r + 1, rr)] - sh[y + d + 1][max(x - l, ll)]);
}
ans = max(ans, dp[l][r][u][d]);
}
}
}
}
cout << ans << endl;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define calc(xl,xr,yl,yr) (sum[xr][yr]-sum[xl-1][yr]-sum[xr][yl-1]+sum[xl-1][yl-1])
const int N=105;
int n,m,ex,ey,ans,sum[N][N],f[2][N][N][N];
char s[N][N];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++){
sum[i][j]=sum[i][j-1];
if(s[i][j]=='E'){
ex=i;
ey=j;
}else if(s[i][j]=='o'){
sum[i][j]++;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
sum[i][j]+=sum[i-1][j];
}
}
int sta=0;
for(int i=1;i<=n;i++,sta^=1){
for(int j=n;j>=i;j--){
for(int k=1;k<=m;k++){
for(int l=m;l>=k;l--){
if(i-1>=1&&((i-1<=ex&&j+ex-(i-1)<=n)||(i-1>=ex&&j-((i-1)-ex)>=1))){
f[sta][j][k][l]=max(f[sta][j][k][l],f[sta^1][j][k][l]+calc(i-1,i-1,max(k,ey-(m-l)),min(l,ey+k-1)));
}
if(j+1<=n&&((j+1<=ex&&i+ex-(j+1)<=n)||(j+1>=ex&&i-((j+1)-ex)>=1))){
f[sta][j][k][l]=max(f[sta][j][k][l],f[sta][j+1][k][l]+calc(j+1,j+1,max(k,ey-(m-l)),min(l,ey+k-1)));
}
if(k-1>=1&&((k-1<=ey&&l+ey-(k-1)<=m)||(k-1>=ey&&l-((k-1)-ey)>=1))){
f[sta][j][k][l]=max(f[sta][j][k][l],f[sta][j][k-1][l]+calc(max(i,ex-(n-j)),min(j,ex+i-1),k-1,k-1));
}
if(l+1<=m&&((l+1<=ey&&k+ey-(l+1)<=m)||(l+1>=ey&&k-((l+1)-ey)>=1))){
f[sta][j][k][l]=max(f[sta][j][k][l],f[sta][j][k][l+1]+calc(max(i,ex-(n-j)),min(j,ex+i-1),l+1,l+1));
}
if(i==j&&k==l){
ans=max(ans,f[sta][j][k][l]+(s[i][k]=='o'));
}
}
}
}
}
printf("%d\n",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<bits/stdc++.h>
#define N 105
using namespace std;
char s[N][N];
int n,m;
int x,y,ans;
int cnt[N][N];
void mmax(int &x,int v){
x=max(x,v);ans=max(ans,v);
}
int get(int x1,int y1,int x2,int y2){
return cnt[x2][y2]-cnt[x2][y1-1]-cnt[x1-1][y2]+cnt[x1-1][y1-1];
}
int dp[2][N][N][N];
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++){
cnt[i][j]=cnt[i-1][j]+cnt[i][j-1]-cnt[i-1][j-1]+(s[i][j]=='o');
if(s[i][j]=='E') x=i,y=j;
}
int now=0;
for (int i=0;i<=x-1;i++){
memset(dp[now^1],0,sizeof(dp[0]));
for (int j=0;j<=n-x;j++)
for (int k=0;k<=y-1;k++)
for (int L=0;L<=m-y;L++){
int v=dp[now][j][k][L];
int xl=max(x-i,j+1),yl=max(y-k,L+1),xr=min(x+j,n-i),yr=min(y+L,m-k);
if (x-i-1>j) mmax(dp[now^1][j][k][L],v+get(x-i-1,yl,x-i-1,yr));
if (x+j+1<n-i+1) mmax(dp[now][j+1][k][L],v+get(x+j+1,yl,x+j+1,yr));
if (y-k-1>L) mmax(dp[now][j][k+1][L],v+get(xl,y-k-1,xr,y-k-1));
if (y+L+1<m-k+1) mmax(dp[now][j][k][L+1],v+get(xl,y+L+1,xr,y+L+1));
}
now^=1;
}
printf("%d\n",ans);
return 0;
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=105;
int n,m;
int dp[MAXN][MAXN][MAXN],E[2];
int sumr[MAXN][MAXN],sumc[MAXN][MAXN];
int ori[MAXN][MAXN];
int main()
{
//freopen("robot.in","r",stdin);
//freopen("robot.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("\n");
for(int j=1;j<=m;j++)
{
char C;
scanf("%c",&C);
if(C=='o')
ori[i][j]=1;
if(C=='E')
{
E[0]=i;
E[1]=j;
}
sumr[i][j]=sumr[i][j-1]+ori[i][j];
sumc[i][j]=sumc[i-1][j]+ori[i][j];
}
}
int L,R,U,D,ans=0;
L=E[1]-1;
R=m-E[1];
U=E[0]-1;
D=n-E[0];
memset(dp,-1,sizeof(dp));
dp[0][0][0]=0;
for(int l=0;l<=L;l++)
{
for(int r=0;r<=R;r++)
{
for(int u=0;u<=U;u++)
{
for(int d=0;d<=D;d++)
{
if(dp[r][u][d]==-1)
continue;
ans=max(dp[r][u][d],ans);
int up=max(E[0]-u,d+1);
int down=min(E[0]+d,n-u);
int left=max(E[1]-l,r+1);
int right=min(E[1]+r,m-l);
if(up>down||left>right)
continue;
int add=0;
if(u<U-d)
{
add=sumr[E[0]-u-1][right]-sumr[E[0]-u-1][left-1];
dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+add);
}
if(d<D-u)
{
add=sumr[E[0]+d+1][right]-sumr[E[0]+d+1][left-1];
dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+add);
}
if(r<R-l)
{
add=sumc[down][E[1]+r+1]-sumc[up-1][E[1]+r+1];
dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+add);
}
if(l<L-r)
{
add=sumc[down][E[1]-l-1]-sumc[up-1][E[1]-l-1];
dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+add);
}
}
}
}
}
printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
}
|
Problem: We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define MAXN 105
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
char str[MAXN];
short f[MAXN][MAXN][MAXN][MAXN];
int n,m,sx,sy,a[MAXN][MAXN],b[MAXN][MAXN];
signed main(){
read (n,m);
for (Int i = 1;i <= n;++ i){
scanf ("%s",str + 1);
for (Int j = 1;j <= m;++ j) a[i][j] = a[i][j - 1] + (str[j] == 'o'),b[i][j] = b[i - 1][j] + (str[j] == 'o'),(str[j] == 'E') && (sx = i,sy = j);
}
int ans = 0;
for (Int i = sx;i;-- i)
for (Int j = sy;j;-- j)
for (Int k = sx;k <= n;++ k)
for (Int l = sy;l <= m;++ l){
if (i > 1 && i - 1 > k - sx) f[i - 1][j][k][l] = max ((int)f[i - 1][j][k][l],f[i][j][k][l] + a[i - 1][min (l,m - (sy - j))] - a[i - 1][max (j - 1,l - sy)]);
if (k < n && n - k > sx - i) f[i][j][k + 1][l] = max ((int)f[i][j][k + 1][l],f[i][j][k][l] + a[k + 1][min (l,m - (sy - j))] - a[k + 1][max (j - 1,l - sy)]);
if (j > 1 && j - 1 > l - sy) f[i][j - 1][k][l] = max ((int)f[i][j - 1][k][l],f[i][j][k][l] + b[min (k,n - (sx - i))][j - 1] - b[max (i - 1,k - sx)][j - 1]);
if (l < m && m - l > sy - j) f[i][j][k][l + 1] = max ((int)f[i][j][k][l + 1],f[i][j][k][l] + b[min (k,n - (sx - i))][l + 1] - b[max (i - 1,k - sx)][l + 1]);
ans = max (ans,(int)f[i][j][k][l]);
}
write (ans),putchar ('\n');
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<stdio.h>
int main(void)
{
int a,s,d,f,g,i;
f=-1;
g=0;
scanf("%d",&a);
for(i=1;i<=a;i++){
scanf("%d %d",&s,&d);
if(f==d){
if(g>s){
g=s;
}
}
else if(f<d){
f=d;
g=s;
}
}
printf("%d %d\n",g,f);
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
//Volume0-0095
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Player implements Comparable<Player>{
public int a,
v;
Player(int a,int v){
this.a = a;
this.v = v;
}
public int compareTo(Player t){
return this.a - t.a;
}
}
public class Main {
public static void main(String[] args){
//declare
ArrayList<Player> ar = new ArrayList<Player>();
int Champion = 0,
num = Integer.MIN_VALUE;
//input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- > 0){
ar.add(new Player(sc.nextInt(),sc.nextInt()));
}
//calculate
Collections.sort(ar);
for(Player p:ar){
if( p.v > num){
Champion = p.a;
num = p.v;
}
}
//output
System.out.println(Champion+" "+num);
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON3):
|
d={}
for _ in[0]*int(input()):
a,v=map(int,input().split())
d.setdefault(v,[])
d[v]+=[a]
m=max(d)
print(min(d[m]),m)
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON3):
|
n = int(input())
dic = {}
max_v = 0
for _ in range(n):
a, v = map(int, input().split())
if not v in dic or a < dic[v]:
dic[v] = a
if v > max_v:
max_v = v
print(dic[max_v], max_v)
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static BufferedReader br = null;
static {
br = new BufferedReader(new InputStreamReader(System.in));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int cnt = parseNum();
int[] out = {-1, -1};
int[] tmp = null;
for (int n = 0; n < cnt; n++) {
tmp = parseScore();
if (out[1] < tmp[1] || out[1] == tmp[1] && out[0] > tmp[0]) {
out[0] = tmp[0];
out[1] = tmp[1];
}
}
System.out.printf("%d %d\n", out[0], out[1]);
}
private static int[] parseScore() {
int[] params = new int[2];
String strin = null;
if ((strin = parseStdin()) != null) {
String[] lines = strin.split(" ");
params[0] = Integer.parseInt(lines[0]);
params[1] = Integer.parseInt(lines[1]);
}
return params;
}
private static int parseNum() {
int param = 0;
String strin = null;
if ((strin = parseStdin()) != null) {
param = Integer.parseInt(strin);
}
return param;
}
private static String parseStdin() {
String stdin = null;
try {
String tmp = br.readLine();
if (!tmp.isEmpty()) stdin = tmp;
}
catch (IOException e) {}
return stdin;
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
using namespace std;
int main() {
int n, id, p, tid = 0, tp = -1;
cin >> n;
while (n--) {
cin >> id >> p;
if (tp == p) {
if (tid > id) {
tid = id;
}
}
else if (tp < p) {
tp = p;
tid = id;
}
}
cout << tid << " " << tp << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.io.*;
import java.util.StringTokenizer;
import java.util.ArrayList;
class List {
ArrayList<Integer> number = new ArrayList<Integer>();
ArrayList<Integer> score = new ArrayList<Integer>();
int max_score,max_n;
List() {
max_score = 0;
max_n = 21;
}
void add(int n,int s) {
number.add(n);
score.add(s);
if (s>max_score) {
max_score = s;
max_n = n;
} else if (s==max_score) {
if (max_n>n) max_n = n;
}
}
void MaxPrint() {
System.out.println(max_n+" "+max_score);
}
}
class Main {
public static void main(String args[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int n = Integer.parseInt(br.readLine());
List list = new List();
for (int i=0;i<n;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int number = Integer.parseInt(st.nextToken());
int score = Integer.parseInt(st.nextToken());
list.add(number,score);
}
list.MaxPrint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String tmp = null;
int i = 0;
ArrayList<Integer> fas = new ArrayList<Integer>();
ArrayList<Integer> has = new ArrayList<Integer>();
ArrayList<Integer> las = new ArrayList<Integer>();
ArrayList<Integer> las2 = new ArrayList<Integer>();
int counta=0;
try {
tmp = br.readLine();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
counta = Integer.parseInt(tmp);
for(i=0;i<counta;i++){
try {
tmp = br.readLine();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
String[] sprite = tmp.split(" ");
fas.add(Integer.parseInt(sprite[0]));
has.add(Integer.parseInt(sprite[1]));
}
int max = 0;
for(i=0;i<counta;i++){
if(max <= has.get(i)){
max = has.get(i);
}
}
for(i=0;i<counta;i++){
if(max == has.get(i)){
las.add(fas.get(i));
las2.add(has.get(i));
}
}
int rrr;
for(i=0;i<las.size();i++){
for(int j=0;j<las.size();j++){
if(las.get(j) < las.get(i)){
rrr = las.get(i);
las.set(i, las.get(j));
las.set(j, las.get(i));
rrr = las2.get(i);
las2.set(i, las2.get(j));
las2.set(j, las2.get(i));
}
}
}
System.out.println(las.get(0)+" "+las2.get(0));
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
using namespace std;
int main()
{
int n;
int min = 21, max = 0;
int a, v;
cin >> n;
for (int i = 0; i < n; i++){
cin >> a >> v;
if (max < v){
max = v;
min = a;
}
else if (max == v){
if (min > a){
min = a;
}
}
}
cout << min << ' ' << max << endl;
return (0);
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct point{
double x, y;
};
bool cmp_x(const point& p, const point& q){
if(p.x != q.x)return p.x>q.x;
return p.y < q.y;
}
int main(void){
int n;
point a;
vector<point>v;
cin >> n;
while(n--){
cin >> a.y >> a.x;
v.push_back(a);
}
sort(v.begin(),v.end(),cmp_x);
cout << v[0].y << " "<< v[0].x <<endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
int n,a,v,mx,mn,i;
scanf("%d",&n);
mx=-1;
for(i=0;i<n;i++) {
scanf("%d %d",&a,&v);
if(mx<v || (mx==v && mn>a)) {
mx=v; mn=a;
}
}
printf("%d %d\n",mn,mx);
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
using namespace std;
int main()
{
int n;
for(;cin>>n;)
{
int a,v;
cin>>a>>v;
for(int i=1;i<n;i++)
{
int ta,tv;
cin>>ta>>tv;
if(tv>v || (tv==v && a>ta))
{
v=tv;
a=ta;
}
}
cout<<a<<" "<<v<<endl;
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, a, v,maxa,maxv;
cin >> n;
maxv = 0;
for (int i = 0; i < n; i++)
{
cin >> a >> v;
if ((maxv < v) || (a < maxa && v==maxv))
{
maxa = a;
maxv = v;
}
}
cout << maxa << ' ' << maxv << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
using namespace std;
int main(){
int n, a, b, m = 0, d;
cin >> n;
for(int i=0;i<n;i++){
cin >> a >> b;
if(m < b){
m = b;
d = a;
}else if(m == b && d > a) d = a;
}
cout << d << ' ' << m << endl;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON3):
|
d={}
for _ in[0]*int(input()):
a,v=map(int,input().split())
d[v]=min(a,d[v])if v in d else a
m=max(d)
print(d[m],m)
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = "";
int[] top = new int[] { 21, -1 };
br.readLine();
// int n = Integer.parseInt(br.readLine());
// int i = 0;
while ((s = br.readLine()) != null) {
// if (i == n)
// break;
int _angler = Integer.parseInt(s.split(" ")[0]);
int _catch = Integer.parseInt(s.split(" ")[1]);
// if (_angler < 0 || _angler > 20)
// continue;
// if (_catch < 0 || _angler > 100)
// continue;
if (top[1] < _catch || (top[1] == _catch && top[0] > _angler)) {
top[0] = _angler;
top[1] = _catch;
}
// i++;
}
System.out.println(top[0] + " " + top[1]);
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.awt.geom.Point2D;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class Main {
Scanner in = new Scanner(System.in);
public static void main(String[] args){
new Main();
}
public Main(){
new AOJ0095();
}
class AOJ0095{
public AOJ0095() {
int n = in.nextInt();
Member[] member = new Member[n];
for(int i=0;i<n;i++){
int num = in.nextInt();
int hiki = in.nextInt();
member[i] = new Member(num,hiki);
}
Arrays.sort(member);
int big = 0;
for(int i=0;i<n;i++)if(member[big].fish<member[i].fish)big = i;
System.out.println(member[big].ban+" "+member[big].fish);
}
class Member implements Comparable<Member>{
int ban,fish;
public Member(int ban,int fish) {
this.ban = ban;
this.fish = fish;
}
public int compareTo(Member o) {
if(this.ban>o.ban)return 1;
else if(this.ban<o.ban)return -1;
else return 0;
}
}
}
class AOJ0097{
int n,m;
public AOJ0097() {
while(true){
n = in.nextInt();//フェーズ数
m = in.nextInt();//最終的な目標数値
if(n+m==0)break;
int[][] dp = new int[1001][n];
for(int i=0;i<100;i++)dp[i][0]=1;
for(int s=0;s<n-1;s++){
for(int i=0;i<1001;i++){
for(int k=0;k<=100;k++){
}
}
}
for(int i=0;i<1001;i++){
for(int s=0;s<n;s++)System.out.print(dp[i][s]);
System.out.println();
}
}
}
}
class AOJ0112{
public AOJ0112() {
ArrayList<Integer> list = new ArrayList<Integer>();
while(true){
int n = in.nextInt();
if(n==0)break;
list.add(n);
}
Collections.sort(list);
for(int i=0;i<list.size();i++)System.out.println(list.get(i));
}
}
class AOJ0106{
int n;
int bfs(int kei,int kin){
if(kei>=n)return kin;
int[] ryou = {200,300,500};
int[] kakaku = {380,550,850};
int[] tani = {5,4,3};
double wari[] = {0.8,0.85,0.88};
int result = Integer.MAX_VALUE/2;
if(n-kei>=1000)for(int i=0;i<3;i++){
double kane = (kakaku[i]*tani[i])*wari[i];
int souryou = ryou[i]*tani[i];
result = Math.min(result,bfs(souryou+kei,(int)kane+kin));
}else for(int i=0;i<3;i++){
result = Math.min(result,bfs(kei+ryou[i],kin+kakaku[i]));
}
return result;
}
public AOJ0106() {
while(true){
n = in.nextInt();
if(n==0)break;
System.out.println(bfs(0,0));
}
}
}
class AOJ2503{
int MAX = 0;
public AOJ2503() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] cost = new int[n][n];
int[] dp = new int[n];
for(int i=0;i<n*n;i++){
cost[i/n][i%n]=MAX;
}
for(int i=0;i<m;i++){
int a = in.nextInt();
int b = in.nextInt();
cost[a][b]=in.nextInt();
}
// TODO 一番大きいパスを0のポイントからn-1のポイントまでの
for(int s=1;s<n;s++){
for(int i=0;i<=s;i++){
if(cost[i][s]==MAX)continue;
dp[s]=Math.max(dp[s],dp[i]+cost[i][s]);
}
}
for(int s=0;s<n;s++)for(int i=0;i<n;i++){
if(cost[s][i]==MAX)continue;
dp[i]=Math.max(dp[s]+cost[s][i],dp[i]);
}
for(int i=0;i<n;i++)System.out.print(dp[i]+" ");
System.out.println();
System.out.println(dp[n-1]);
}
}
class AOJ0155{
int n;
Building[] build;
double[][] cost;
double Max;
int[] list;
void dijkstra(int s,int g){
list = new int[n];
double[] d = new double[n];
boolean used[] = new boolean[n];
Arrays.fill(d,Max);
Arrays.fill(used, false);
d[s] = 0;
int v=-1;
while(true){
int before = v;
v=-1;
for(int u=0;u<n;u++)if(!used[u]&&(v==-1||d[u]<d[v]))v=u;//まだ使われていない頂点のうち最小のものを探す
if(v==-1)break;
System.out.println(v);
list[v] = before;//前の頂点を記憶
if(v==g)break;
used[v]=true;//行き先は頂点v
for(int u=0;u<n;u++){
d[u] = Math.min(d[u],d[v]+cost[v][u]);
}
}
}
ArrayList<Integer> list2;
void bfs(int g){
System.out.println(g);
list2.add(build[g].ban);
if(list[g]==-1)return;
bfs(list[g]);
return;
}
public AOJ0155() {
Max = Double.MAX_VALUE;
while(true){
n = in.nextInt();
if(n==0)break;
cost = new double[n][n];
build = new Building[n];
for(int i=0;i<n;i++){
int ban = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
build[i] = new Building(x, y, ban);
}
for(int s=0;s<n;s++){
for(int i=0;i<n;i++){
cost[s][i] = Math.hypot(build[s].x-build[i].x, build[s].y-build[i].y);
cost[s][i] = cost[s][i]>50? Max:cost[s][i];
cost[i][s]=cost[s][i];
}
}
//costデバック
// double rei = 0;
// for(int i=0;i<n;i++){
// for(int s=0;s<n;s++)if(cost[i][s]!=Max)System.out.printf("%5f ",cost[i][s]);
// else System.out.printf("%5f",rei);
// System.out.println();
// }
//ここまでデバック
int k = in.nextInt();
for(int i=0;i<k;i++){
int from = in.nextInt();
int goal = in.nextInt();
dijkstra(from-1,goal);
list2 = new ArrayList<Integer>();
for(int s=0;s<n;s++)System.out.print(list[s]+" before ");
System.out.println();
bfs(goal-1);
for(int s=list2.size()-1;s>0;s--){
System.out.print(list2.get(s)+" ");
}
System.out.println(list2.get(0));
}
}
}
class Building{
int x,y,ban;
public Building(int x,int y,int ban) {
this.x = x;
this.y = y;
this.ban = ban;
}
}
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int num;
cin >> num;
int a,v,maxa=0,maxv=0;
for(int i=0;i<num;i++){
cin >> a >> v;
if(i==0) {
maxa = a; maxv = v;
}
if(maxv < v){
maxv = v; maxa = a;
}
if(maxv == v){
if(maxa>a){
maxv = v; maxa = a;
}
}
}
cout << maxa << " " << maxv << endl;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
struct Human {
int no;
int point;
};
int compareTo(const Human& h1, const Human& h2) {
if(h1.point > h2.point) return(1);
else if(h2.point > h1.point) return(0);
if(h1.no < h2.no) return(1);
else return(0);
}
int main(void) {
int n; scanf("%d", &n);
Human player[n];
for(int r = 0; r < n; r++) {
scanf("%d %d", &player[r].no, &player[r].point);
}
sort(player, player+n, compareTo);
printf("%d %d\n", player[0].no, player[0].point);
return(0);
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
using namespace std;
int main(){
int n,a,b,ma=1,sum=0;
cin >> n;
for(int i=0;i<n;i++){
cin >> a >> b;
if(b > sum || b == sum && a < ma){
ma = a;
sum = b;
}
}
cout << ma << " " << sum << endl;
return(0);
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <bits/stdc++.h>
#define range(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,n) range(i,0,n)
using namespace std;
int n;
int a[110];
int main(void){
cin >> n;
rep(i,n){
int idx,num;
cin >> idx >> num;
a[idx-1]=num;
}
int ans=max_element(a,a+n)-a;
cout << ans+1 << " " << a[ans] << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON3):
|
n=int(input())
nmb=21
r_m=0
for i in range(n):
a,v=map(int,input().split())
if r_m<v:
nmb,r_m=a,v
elif r_m==v:
nmb=min(nmb,a)
print(nmb,r_m)
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
int[] v=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
v[i]=sc.nextInt();
}
for(int i=0;i<n-1;i++){
for(int j=n-1;j>i;j--){
if(a[j-1]>a[j]){
int box=v[j-1];
v[j-1]=v[j];
v[j]=box;
box=a[j];
a[j]=a[j-1];
a[j-1]=box;
}
}
}
int max=Integer.MIN_VALUE;
int maxPtr=0;
for(int i=0;i<n;i++){
if(v[i]>max){
max=v[i];
maxPtr=a[i];
}
}
System.out.println(maxPtr+" "+max);
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int id = -1;
int max = -1;
for(int i=0; i<n; i++)
{
int a = scanner.nextInt();
int v = scanner.nextInt();
if(v > max)
{
id = a;
max = v;
}
else if(v == max)
{
if(a < id)
{
id = a;
}
}
}
System.out.println(id + " " + max);
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
struct P {
int x, y;
bool operator<(const P& o) const {
if (y == o.y) {
return x < o.x;
} else {
return y > o.y;
}
}
};
using namespace std;
int main() {
int n; cin >> n;
vector<P> Ps;
for (int i = 0; i < n; i++) {
P p; cin >> p.x >> p.y;
Ps.push_back(p);
}
sort(Ps.begin(), Ps.end());
cout << Ps[0].x << ' ' << Ps[0].y << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON3):
|
n=int(input())
l=[]
for i in range(n):
x,y=map(int,input().split())
l.append((y,x))
l.sort(key=lambda x:(x[0],x[1]),reverse=True)
maximum=l[0][0]
young=l[0][1]
for i in l:
if maximum==i[0]:
young=i[1]
else:
break
print(young,maximum)
#print(l[0][1],l[0][0])
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <cstdio>
#define reep(i,n,m) for(int i=(n);i<(m);i++)
#define rep(i,n) reep(i,0,n)
using namespace std;
int main(){
int n;
cin >> n;
int s[100] = {0};
rep(i,n){
int a,v;
cin >> a >> v;
s[a] += v;
}
int ans=1;
int sum=0;
reep(i,1,n+1){
if(sum<s[i]){
ans = i;
sum = s[i];
}
}
printf("%d %d\n",ans, sum);
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <cstdio>
using namespace std;
int main()
{
int n;
int a[20], v[20];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d %d", a + i, v + i);
int ai = 9999, vi = -1;
for (int i = 0; i < n; i++){
if (v[i] > vi){
vi = v[i];
ai = a[i];
}
else if (v[i] == vi){
if (ai > a[i]) ai = a[i];
}
}
return (!printf("%d %d\n", ai, vi));
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <stdio.h>
int main(void)
{
int n,i;
int maxa,maxv;
int a,v;
scanf("%d",&n);
scanf("%d%d",&a,&v);
maxa=a;
maxv=v;
for(i=1;i<n;i++) {
scanf("%d%d",&a,&v);
if(v>maxv || (v==maxv && a<maxa)) {
maxa=a;
maxv=v;
}
}
printf("%d %d\n",maxa,maxv);
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.Scanner;
public class Main {
void run() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int max = -1;
int maxix = 21;
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int v = sc.nextInt();
if (v > max) {
max = v;
maxix = a;
} else if (v == max && maxix > a) {
maxix = a;
}
}
System.out.println(maxix + " " + max);
}
public static void main(String[] args) {
new Main().run();
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.*;
public class Main {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
new Main();
}
public Main() {
while(sc.hasNext()){
new aoj0095().doIt();
}
}
class aoj0095 {
void doIt() {
int n = sc.nextInt();
int asum[] = new int [n+1];
for(int i = 0;i < n;i++){
int a = sc.nextInt();
int v = sc.nextInt();
asum[a] = asum[a] + v;
}
int maxa = 1;
int maxv = 0;
for(int i = 1;i <= n;i++){
if(maxv < asum[i]){
maxa = i;
maxv = asum[i];
}
}
System.out.println(maxa+" "+maxv);
}
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m=0,a,b=100,d;
cin>>n;
while(n--){
cin>>d>>a;
if(m<a){
m=a;
b=d;
}
else if(m==a&&b>d)b=d;
}
cout<<b<<' '<<m<<endl;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n, p, fish;
cin >> n;
int rp = 20;
int max_fish = -100;
for( int i = 0; i < n ; i++ ){
cin >> p >> fish;
if(max_fish < fish || max_fish == fish && rp > p){
rp = p;
max_fish = fish;
}
}
cout << rp << " " << max_fish << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON3):
|
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0095
"""
import sys
def main(args):
winner = [-1, -1]
n = int(input())
for _ in range(n):
a, v = [int(x) for x in input().strip().split(' ')]
if v > winner[1]:
winner[0] = a
winner[1] = v
elif v == winner[1]:
if a < winner[0]:
winner[0] = a
print('{} {}'.format(winner[0], winner[1]))
if __name__ == '__main__':
main(sys.argv[1:])
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int ary[21] = {};
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
ary[a] = b;
}
int *p = max_element(ary + 1, ary + n + 1);
cout << p - ary << ' ' << *p << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON):
|
x=[map(int,raw_input().split()) for i in range(int(raw_input()))]
a,v=sorted(x,key=lambda x:(-x[1],x[0]))[0]
print a,v
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n, dummy1, dummy2, max, ans;
int v[1000];
cin >> n;
for(int i = 1; i <= n; i++){
v[i] = 0;
}
for(int i = 1; i <= n; i++){
cin >> dummy1 >> dummy2;
v[dummy1] = dummy2;
}
max = 0;
for(int i = n; i > 0; i--){
if(v[i] >= max){
ans = i;
max = v[i];
}
}
printf("%d %d\n", ans, v[ans]);
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> array = new ArrayList<String>();
try {
String str = null;
while ((str = br.readLine()) != null) {
array.add(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int N = Integer.parseInt(array.get(0));
int maxNumber = -1;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 1; i <= N; i++) {
String[] data = array.get(i).split(" ");
int id = Integer.parseInt(data[0]);
map.put(id, Integer.parseInt(data[1]));
if(maxNumber == -1 || (map.get(id) > map.get(maxNumber))) {
maxNumber = id;
} else if((map.get(id) == map.get(maxNumber)) && id < maxNumber){
maxNumber = id;
}
}
System.out.println(maxNumber + " " + map.get(maxNumber));
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
using namespace std;
int main(){
int a, v, n, maxv, mina;
cin>>n;
cin>>a>>v;
maxv=v;
mina=a;
for(int i=1;i<n;i++){
cin>>a>>v;
if(maxv<v){
maxv=v;
mina=a;
}else if(maxv==v){
if(mina>a){
mina=a;
}
}
}
cout<<mina<<" "<<maxv<<endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<stdio.h>
int main(void)
{
int n;
int a[21],v[101];
int i;
int max=0,m;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++){
scanf("%d %d",&a[i],&v[i]);
if(max<v[i]){
max=v[i];
m=a[i];
}
if(max==v[i] && m>a[i]){
m=a[i];
}
}
printf("%d %d\n",m,max);
}
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in PYTHON):
|
n=input()
L=[0]*n
for i in range(n):
a,v=map(int,raw_input().split())
L[a-1]+=v
print L.index(max(L))+1,max(L)
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
typedef pair < int , int > Pi;
int main(){
int n;
Pi a[20];
cin >> n;
for(int i = 0 ; i < n ; i++ ){
cin >> a[i].second >> a[i].first;
}
int no = -1, big = -1;
for(int i = 0 ; i < n ; i++ ){
if(big < a[i].first) big = a[i].first, no = a[i].second;
else if(big == a[i].first && no > a[i].second) no = a[i].second;
}
cout << no << " " << big << endl;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ma=-1, mv=0;
for(int i=0;i<n;i++) {
int a = sc.nextInt(), v = sc.nextInt();
if(ma<0 || mv<v || (mv==v && ma>a)) {ma = a; mv = v;}
}
System.out.println(ma + " " + mv);
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
bool great(const pair<int, int>& l, const pair<int, int>& r) {
if (l.second == r.second) return l.first < r.first;
else return l.second > r.second;
}
int main(){
int n,a,b;
vector<pair<int, int> > v;
cin >> n;
for (int i = 0; i < n; i++){
cin >> a >> b;
v.push_back(make_pair(a,b));
}
sort(v.begin(), v.end(), great);
cout << v[0].first << " " << v[0].second << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int,int> p;
int main(){
vector<p> v;
int n,data,num;
cin >> n;
for(int i = 0 ; i < n ; i++){
cin >> num >> data;
v.push_back(p(-data,num));
}
stable_sort(v.begin(),v.end());
cout << v[0].second << ' ' << (-1)*v[0].first << endl;
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.Scanner;
public class Main {
public static void main(String[] arg) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int top = -1;
int max = -1;
for (int i = 0; i < n; i++) {
int a = in.nextInt();
int v = in.nextInt();
if (v > max || v == max && top > a) {
top = a;
max = v;
}
}
System.out.print(top);
System.out.print(' ');
System.out.println(max);
in.close();
}
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <string>
using namespace std;
int main(){
int n, a, v, maxa, maxv;
while (cin >> n) {
int maxa = 21, maxv = 0;
for (int i = 0; i < n; i++) {
cin >> a >> v;
if (v > maxv) {
maxv = v;
maxa = a;
}
else if(v == maxv && maxa > a){
maxa = a;
}
}
cout << maxa << " " << maxv << endl;
}
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int main() {
int n, m = 0;
int a[N + 1] = { 0 };
int x, y;
cin >> n;
for(int i = 0;i < n;++i) {
cin >> x >> y;
a[x] = y;
m = y > m ? y : m;
}
for(int i = 1;i <= N;++i) {
if(a[i] == m) {
cout << i << " " << a[i] << endl;
break;
}
}
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main(){
int n;
cin >> n;
pair <int,int> data[20];
for(int i=0;i<n;i++) {
cin >> data[i].second >> data[i].first;
data[i].second *= -1;
}
sort(data,data + n);
cout << -data[n-1].second << " " << data[n-1].first << endl;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in CPP):
|
#include<cstdio>
using namespace std;
int main(void)
{
int n,a,b,i,max,x;
scanf("%d",&n);
max=0;
for(i=0;i<n;i++){
scanf("%d %d",&a,&b);
if(max<=b){
if(max<b){
x=a;
max=b;
}
else if(x>a)x=a;
}
}
printf("%d %d\n",x,max);
return 0;
}
|
Problem: A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42
Solution (in JAVA):
|
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer,Integer> treeMap = new TreeMap<Integer,Integer>();
Scanner scan = new Scanner(System.in);
int win = 0;
while(scan.hasNext()) {
int num = scan.nextInt();
for(int i = 1;i < num + 1;i++) {
int member = scan.nextInt();
int fish = scan.nextInt();
treeMap.put(member,fish);
if(win < fish) {
win = fish;
}
}
break;
}
for(Integer fin : treeMap.keySet()) {
if(treeMap.get(fin) == win) {
System.out.println(fin + " " + treeMap.get(fin));
break;
}
}
scan.close();
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.