Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> using namespace std; using Int = long long; using Double = long double; signed main(){ Int h,w; cin>>w>>h; vector<string> s(h); for(Int i=0;i<h;i++) cin>>s[i]; vector<vector<Int> > dg(h,vector<Int>(w,-1)),ds=dg; using T = pair<Int,int>; queue<T> qg,qs; for(Int i=0;i<h;i++){ for(Int j=0;j<w;j++){ if(s[i][j]=='g'){ qg.push(T(i,j)); dg[i][j]=0; } if(s[i][j]=='*'){ qs.push(T(i,j)); ds[i][j]=0; } } } Int dy[]={0,0,1,-1}; Int dx[]={1,-1,0,0}; auto bfs=[&](queue<T> &q,vector<vector<Int> > &d){ while(!q.empty()){ T t=q.front();q.pop(); Int y=t.first,x=t.second; for(Int k=0;k<4;k++){ Int ny=y+dy[k],nx=x+dx[k]; if(s[ny][nx]=='#'||s[ny][nx]=='*') continue; if(~d[ny][nx]&&d[ny][nx]<=d[y][x]+1) continue; d[ny][nx]=d[y][x]+1; q.push(T(ny,nx)); } } if(0){ cout<<endl; for(Int i=0;i<h;i++){ for(Int j=0;j<w;j++){ if(d[i][j]<0) cout<<"x"; else cout<<hex<<d[i][j]; } cout<<endl; } } }; bfs(qg,dg); bfs(qs,ds); auto get=[&](Int i,Int j,Double p){ if(~dg[i][j]&&~ds[i][j]) return min((Double)dg[i][j],ds[i][j]+p); if(~ds[i][j]) return ds[i][j]+p; if(~dg[i][j]) return (Double)dg[i][j]; return Double(0); }; auto calc=[&](Double p){ Double q=0,c=0; for(Int i=0;i<h;i++){ for(Int j=0;j<w;j++){ if(s[i][j]=='#'||s[i][j]=='g'||s[i][j]=='*') continue; c+=1.0; q+=get(i,j,p); } } q/=c; //printf("%.12Lf %.12Lf\n",p, p-q); return p-q; }; Double l=0,r=1e15; for(int k=0;k<300;k++){ //while(abs(calc(l))>1e-10){ Double m=(l+r)/2; if(calc(m)<=Double(0)) l=m; else r=m; } //printf("%.12f\n",calc(l)); for(Int i=0;i<h;i++) for(Int j=0;j<w;j++) if(s[i][j]=='s') printf("%.12Lf\n",get(i,j,l)); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-10; const int H = 500; const int W = 500; const int dy[4] = {-1,0,1,0}; const int dx[4] = {0,-1,0,1}; struct P{ int x, y; P(int x=0, int y=0):x(x),y(y){} }; struct state{ int x, y, cnt; state(int x=0, int y=0, int cnt=0):x(x),y(y),cnt(cnt){} bool operator < (const state &s) const { return cnt < s.cnt; } }; int w, h, dis_go[H][W], dis_sp[H][W]; long double cnt_nowall; P st, go; vector<P> v; bool wall[H][W]; void init_dis(){ queue<state> q; state u, u2; cnt_nowall = 0; for(int i=1;i<h-1;i++){ for(int j=1;j<w-1;j++){ dis_go[i][j] = dis_sp[i][j] = -1; cnt_nowall += !wall[i][j]; } } for(q.push(state(go.x, go.y, 0));!q.empty();){ u = q.front(); q.pop(); for(int i=0;i<4;i++){ u2 = state(u.x+dx[i], u.y+dy[i], u.cnt+1); if(!wall[u2.y][u2.x] && (dis_go[u2.y][u2.x] == -1 || dis_go[u2.y][u2.x] > u2.cnt)){ dis_go[u2.y][u2.x] = u2.cnt; q.push(u2); } } } for(int i=0;i<v.size();i++){ for(q.push(state(v[i].x, v[i].y, 0));!q.empty();){ u = q.front(); q.pop(); for(int j=0;j<4;j++){ u2 = state(u.x+dx[j], u.y+dy[j], u.cnt+1); if(!wall[u2.y][u2.x] && (dis_sp[u2.y][u2.x] == -1 || dis_sp[u2.y][u2.x] > u2.cnt)){ dis_sp[u2.y][u2.x] = u2.cnt; q.push(u2); } } } } } long double a_ruiseki[H*W], b_ruiseki[H*W], g_ruiseki[H*W]; void calc_exp_init(){ vector<state> pv; for(int i=1;i<h-1;i++){ for(int j=1;j<w-1;j++){ if(!wall[i][j]){ pv.push_back(state(j, i, dis_go[i][j] - dis_sp[i][j])); if(dis_sp[i][j] == -1) pv[pv.size()-1].cnt = -2; else if(dis_go[i][j] == -1) pv[pv.size()-1].cnt = -1; else if(dis_go[i][j] < dis_sp[i][j]) pv[pv.size()-1].cnt = 0; } } } fill(a_ruiseki, a_ruiseki+H*W, 0); fill(b_ruiseki, b_ruiseki+H*W, 0); fill(g_ruiseki, g_ruiseki+H*W, 0); sort(pv.begin(), pv.end()); a_ruiseki[h*w-1] = 1.0; for(int i=0;i<pv.size();i++){ if(pv[i].cnt == -2){ // dis_sp[i][j] == -1 g_ruiseki[0] += (long double)dis_go[pv[i].y][pv[i].x] / cnt_nowall; } else if(pv[i].cnt == -1){ // dis_go[i][j] == -1 b_ruiseki[h*w-1] += (long double)dis_sp[pv[i].y][pv[i].x] / cnt_nowall; a_ruiseki[h*w-1] -= (long double)1.0 / cnt_nowall; } else if(pv[i].cnt == 0){ //b_ruiseki[h*w-1] += (long double)dis_sp[pv[i].y][pv[i].x] / cnt_nowall; g_ruiseki[1] += (long double)dis_go[pv[i].y][pv[i].x] / cnt_nowall; } else { b_ruiseki[pv[i].cnt] += (long double)dis_sp[pv[i].y][pv[i].x] / cnt_nowall; g_ruiseki[pv[i].cnt+1] += (long double)dis_go[pv[i].y][pv[i].x] / cnt_nowall; a_ruiseki[pv[i].cnt] -= (long double)1.0 / cnt_nowall; } } for(int i=0;i<h*w-1;i++){ g_ruiseki[i+1] += g_ruiseki[i]; } for(int i=h*w-1;i>0;i--){ b_ruiseki[i-1] += b_ruiseki[i]; a_ruiseki[i-1] += a_ruiseki[i]; } } long double calc_exp(int base){ long double a = 1.0, b = 0.0, exp_go = 0.0; /* for(int i=1;i<h-1;i++){ for(int j=1;j<w-1;j++){ if(!wall[i][j]){ if(dis_go[i][j] == -1 || dis_sp[i][j] != -1 && dis_go[i][j] >= dis_sp[i][j] + base){ a -= 1.0 / cnt_nowall; b += (long double)dis_sp[i][j] / cnt_nowall; } else { exp_go += (long double)dis_go[i][j] / cnt_nowall; } } } } */ //printf("g %d: %.10Lf %.10Lf\n", base, g_ruiseki[base], exp_go); //printf("b %d: %.10Lf %.10Lf\n", base, b_ruiseki[base], b); //printf("a %d: %.10Lf %.10Lf\n", base, a_ruiseki[base], a); //return (b + exp_go) / a; return (b_ruiseki[base] + g_ruiseki[base]) / a_ruiseki[base]; } long double solve(){ long double ans = -1; init_dis(); calc_exp_init(); if(dis_go[st.y][st.x] != -1) ans = dis_go[st.y][st.x]; int cnt = 0; for(int i=2;i<(h-2)*(w-2);i++){ if(dis_sp[st.y][st.x] == -1 || dis_go[st.y][st.x] != -1 && dis_go[st.y][st.x] < dis_sp[st.y][st.x] + i){ break; } if(ans < -0.9) ans = calc_exp(i) + dis_sp[st.y][st.x]; else ans = min(ans, calc_exp(i) + dis_sp[st.y][st.x]); } /* if(dis_sp[st.y][st.x] != -1){ int le = 2, ri = dis_go[st.y][st.x] - dis_sp[st.y][st.x] + 1, mid1, mid2; long double res0, res1, res2; if(dis_go[st.y][st.x] == -1) ri = (h-2) * (w-2); while(ri - le >= 4){ mid1 = (le * 2 + ri) / 3; mid2 = (le + ri * 2) / 3; res0 = calc_exp(le) + dis_sp[st.y][st.x]; res1 = calc_exp(mid1) + dis_sp[st.y][st.x]; res2 = calc_exp(mid2) + dis_sp[st.y][st.x]; if(mid2 - mid1 <= 1) break; if(res0 >= res1 - EPS && res1 >= res2 - EPS) le = mid1; else ri = mid2; } for(int i=le;i<le+5;i++) { if(dis_go[st.y][st.x] != -1 && dis_go[st.y][st.x] < dis_sp[st.y][st.x] + i) break; if(ans < -0.9) ans = calc_exp(i) + dis_sp[st.y][st.x]; else ans = min(ans, calc_exp(i) + dis_sp[st.y][st.x]); } } */ return ans; } int main(){ char c; while(cin >> w >> h){ v.clear(); int cnt = 0; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cin >> c; wall[i][j] = true; if(c == 's') st = P(j, i), wall[i][j] = false; else if(c == 'g') go = P(j, i); else if(c == '*') v.push_back(P(j, i)), cnt++; else if(c == '.') wall[i][j] = false; } } printf("%.12Lf\n", solve()); } }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<iostream> #include<climits> #include<cstdio> #include<queue> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) typedef long long ll; const int N = 500; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; const ll inf = 1LL<<60; char m[N][N+1]; ll dcost[N][N];//’¼ÚƒS[ƒ‹‚܂ōs‚­ƒRƒXƒg ll scost[N][N];//ƒoƒl‚ðŽg‚Á‚½Žž‚̃RƒXƒg void bfs(int r,int c,char s,ll cost[N][N]){ queue<int> Q; rep(i,r)rep(j,c) if (m[i][j] == s)cost[i][j] = 0,Q.push(i*c+j); else cost[i][j] = inf; while(!Q.empty()){ int y = Q.front()/c,x=Q.front()%c;Q.pop(); rep(i,4){ int ney = y+dy[i],nex = x+dx[i]; if (m[ney][nex] == '*' || m[ney][nex] == 'g' || m[ney][nex] == '#' || cost[ney][nex] != inf)continue; cost[ney][nex] = cost[y][x] + 1; Q.push(ney*c+nex); } } } /* Šeƒ}ƒX‚©‚ç‚ÌŠú‘Ò’l‚́A min(’¼ÚƒS[ƒ‹‚܂ōs‚­AÅ’Z‚̃oƒl‚܂ŊJ‚¢‚Ä‚¢‚Á‚Ä”ò‚ԁ{Šú‘Ò’l) ‚±‚±‚ł̊ú‘Ò’l‚Í (”ò‚ñ‚¾‚ ‚ƍs‚­‰Â”\«‚ª‚ ‚éƒ}ƒX‚ÌŠú‘Ò’l‚Ì‘˜a)/(‚»‚̂悤‚ȃ}ƒX‚̐”)B “K“–‚ÉŠú‘Ò’l‚ðŒˆ‚ß‘Å‚¿‚µ‚āA‚Ç‚¤–µ‚‚·‚é‚©‚ðl‚¦‚Ă݂éB Šú‘Ò’l‚ð‚³‚ç‚ɏ¬‚³‚­‚Å‚«‚é‚Ȃ珬‚³‚­‚µ‚āA‘å‚«‚­‚È‚¢‚Æ‚¢‚¯‚È‚¢‚È‚ç‘å‚«‚­‚·‚éB */ double exp[N][N]; double bf(int r,int c){ double e=0;//Šú‘Ò’l‚Ì‘˜a‚ðŽ‚Á‚Ä‚¨‚­‚Æ–ð‚É—§‚ double pos = 0;//ƒoƒl‚ōs‚­‰Â”\«‚Ì‚ ‚éêŠ‚̐” int sy,sx; rep(i,r){ rep(j,c){ if (m[i][j] == '*' || m[i][j] == 'g' || m[i][j] == '#')continue; exp[i][j] = dcost[i][j]; e += dcost[i][j]; pos++; if (m[i][j] == 's')sy = i,sx = j; } } /* ƒoƒl‚Å”ò‚ñ‚Å‚©‚çƒS[ƒ‹‚܂ł½‚ǂ蒅‚­‚܂ł̊ú‘Ò’l‚ðŒˆ‚ß‘Å‚¿‚·‚éB ‚»‚ÌŠú‘Ò’l‚ðŒ³‚ÉŽÀÛ‚ÌŠú‘Ò’l‚ðŒvŽZ‚µ‚Ă݂āA¬‚³‚¯‚ê‚ÎŒˆ‚ߑł¿‚µ‚½Šú‘Ò’l‚æ‚è‚à‚Á‚Ə¬‚³‚­‚Å‚«‚éB ‘å‚«‚¯‚ê‚΂à‚Á‚Ƒ傫‚­‚È‚¢‚Æ‚¢‚¯‚È‚­‚È‚éB */ double L = 0,R=1e20; double ans = -1; rep(i,300){ double mid = (L+R)/2.; double newe = 0; rep(i,r){ rep(j,c){ if (m[i][j] == 'g' || m[i][j] == '#' || m[i][j] == '*')continue; double tmp = dcost[i][j]; double tmp2 = scost[i][j] + mid/pos; newe += min(tmp,tmp2); } } if (newe < mid){//—\‘z‚æ‚菬‚³‚©‚Á‚½ ans = mid; R = mid - 1e-10; }else {//—\‘z‚æ‚è‘å‚«‚©‚Á‚½ L = mid + 1e-10; } } return min((double)dcost[sy][sx],scost[sy][sx]+ans/pos); /* Žû‘©‚·‚é‚܂ʼnñ‚µ‚Ă݂½ */ /* while(true){ bool isupdate = false; double newe = 0; rep(i,r){ rep(j,c){ if (m[i][j] == 'g' || m[i][j] == '#')continue;//‚±‚±‚͍l‚¦‚È‚¢B double tmp = dcost[i][j]; double tmp2 = scost[i][j] + (e)/(pos); if (exp[i][j] > min(tmp,tmp2)){ isupdate = true; e -= exp[i][j]; exp[i][j] = min(tmp,tmp2); e += exp[i][j]; } newe += exp[i][j]; } } e = newe; if (!isupdate)break; } /* ‰ð•ú‚ÌŽè–@‚ª³‚µ‚­“®‚­‚©ƒ`ƒFƒbƒN‚·‚邽‚߂ɏ‘‚¢‚½ * double hoge = 0; rep(i,r){ rep(j,c){ if (m[i][j] == '#' || m[i][j] == 'g' || m[i][j] == '*')continue; hoge += min((double)dcost[i][j],scost[i][j] + e/pos); cout << min((double)dcost[i][j],scost[i][j] + e/pos) <<" " ; } cout << endl; } cout <<"hoge " << e <<" " << hoge/pos << endl; */ //rep(i,r)rep(j,c)if (m[i][j] == 's')return exp[i][j]; } int main(){ int r,c; while(cin>>c>>r && r){ rep(i,r)cin>>m[i]; bfs(r,c,'*',scost); bfs(r,c,'g',dcost); printf("%.12lf\n",bf(r,c)); } }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; #define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define all(c) begin(c),end(c) #define mp make_pair #define mt make_tuple typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; const ll INF=1e10; // 期待値の最大値はおよそ(500*250/2)*500*250<1e10 const int MOD=1e9+7; const double EPS=1e-9; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename T> ostream& operator<<(ostream& os,const vector<T>& a){ os<<'['; rep(i,a.size()) os<<(i?" ":"")<<a[i]; return os<<']'; } int main() { for(int w,h;cin>>w>>h && w|h;){ vs grid(h); rep(i,h) cin>>grid[i]; vvl stair(h,vl(w,INF)),spring(h,vl(w,INF)); queue<tuple<int,int,ll>> q; rep(i,h) rep(j,w) if(grid[i][j]=='g') q.emplace(i,j,0); while(q.size()){ int i,j; ll d; tie(i,j,d)=q.front(); q.pop(); if(0<=i && i<h && 0<=j && j<w && strchr(".sg",grid[i][j]) && stair[i][j]==INF){ stair[i][j]=d; rep(k,4) q.emplace(i+"\xff\x1\0\0"[k],j+"\0\0\xff\x1"[k],d+1); } } rep(i,h) rep(j,w) if(grid[i][j]=='*') q.emplace(i,j,0); while(q.size()){ int i,j; ll d; tie(i,j,d)=q.front(); q.pop(); if(0<=i && i<h && 0<=j && j<w && strchr(".sg*",grid[i][j]) && spring[i][j]==INF){ spring[i][j]=d; rep(k,4) q.emplace(i+"\xff\x1\0\0"[k],j+"\0\0\xff\x1"[k],d+1); } } vl st,sp; rep(i,h) rep(j,w) if(strchr(".s",grid[i][j])){ st.push_back(stair[i][j]); sp.push_back(spring[i][j]); } int n=st.size(); vi is(n); iota(all(is),0); sort(all(is),[&](int i,int j){return st[i]-sp[i]<st[j]-sp[j];}); int si=-1,sj=-1; rep(i,h) rep(j,w) if(grid[i][j]=='s') si=i,sj=j; double res=stair[si][sj]; ll sum1=0,sum2=accumulate(all(sp),0ll),cnt=0; for(int i:is){ sum1+=st[i],sum2-=sp[i],cnt++; res=min(res,spring[si][sj]+double(sum1+sum2)/cnt); } printf("%.10f\n",res); } }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))<EPS) using namespace std; typedef long double ld; const int MAX = 501,IINF = INT_MAX; const ld LDINF = 1e100; int H,W,sx,sy,gx,gy; ld mincost[MAX][MAX][2]; // mincost[][][0] => from start, [1] = > from star char c[MAX][MAX]; bool ban[MAX][MAX]; vector<int> star,plane; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; inline bool isValid(int x,int y) { return 0 <= x && x < W && 0 <= y && y < H; } inline void bfs(vector<int> sp,vector<int> Forbidden,int type){ rep(i,H)rep(j,W) mincost[i][j][type] = LDINF, ban[i][j] = false; queue<int> que; rep(i,(int)sp.size()) que.push(sp[i]), mincost[sp[i]/W][sp[i]%W][type] = 0; rep(i,(int)Forbidden.size()) ban[Forbidden[i]/W][Forbidden[i]%W] = true; while(!que.empty()){ int cur = que.front(); que.pop(); rep(i,4){ int nx = cur % W + dx[i], ny = cur / W + dy[i]; if( c[ny][nx] == '#' ) continue; if( ban[ny][nx] ) continue; if( mincost[ny][nx][type] == LDINF ) { mincost[ny][nx][type] = mincost[cur/W][cur%W][type] + 1; que.push(nx+ny*W); } } } } bool check(ld E){ ld T = 0; rep(i,(int)plane.size()){ int x = plane[i] % W, y = plane[i] / W; T += min(mincost[y][x][0],mincost[y][x][1]+E); } ld len = plane.size(); return len * E > T; } int main(){ cin >> W >> H; rep(i,H)rep(j,W){ cin >> c[i][j]; if( c[i][j] == 's' ) sx = j, sy = i, c[i][j] = '.'; if( c[i][j] == 'g' ) gx = j, gy = i; if( c[i][j] == '*' ) star.push_back(j+i*W); if( c[i][j] == '.' ) plane.push_back(j+i*W); } vector<int> sp,forbidden; sp.push_back(gx+gy*W); forbidden = star; forbidden.push_back(gx+gy*W); bfs(sp,forbidden,0); sp = star; forbidden.push_back(gx+gy*W); //forbidden.clear(); bfs(sp,forbidden,1); ld L = 0, R = 1e10, M = 0; rep(i,75){ M = ( L + R ) * (ld)0.5; if( check(M) ) R = M; else L = M; } cout << setiosflags(ios::fixed) << setprecision(20) << min((ld)mincost[sy][sx][0],(ld)mincost[sy][sx][1]+L) << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; using dbl = long double; using Pi = pair<int, int>; const dbl eps = 1e-15; #define lt(a, b) ((a)-(b) < -eps) #define eq(a, b) (fabs((a)-(b)) < eps) int W, H; char mas[505][505]; Pi S, G; vector<Pi> Bs; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; bool in(int y, int x) { return 0<=y&&y<H&&0<=x&&x<W; } vector<vector<dbl> > bfs(const vector<Pi>& s) { //cout<<"!!!!!"<<endl; queue<Pi> que; vector<vector<dbl> > dist(H, vector<dbl>(W, -1)); for(Pi p : s) { que.emplace(p); dist[p.first][p.second] = 0; } while(!que.empty()) { int y, x; tie(y, x) = que.front(); que.pop(); //cout<<mas[y][x]<<" "<<y<<" "<<x<<endl; for(int i = 0; i < 4; ++i) { int ny = y+dy[i], nx = x+dx[i]; if(!in(ny, nx) || mas[ny][nx] == '#' || mas[ny][nx] == '*') continue; if(dist[ny][nx] == -1) { dist[ny][nx] = dist[y][x]+1; que.emplace(ny, nx); } } } return dist; } vector<vector<dbl> > db; vector<vector<dbl> > dg; bool check(dbl mb) { dbl sum = 0; int num = 0; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { if(mas[i][j] == '.') { if(dg[i][j] == -1 && db[i][j] == -1) continue;//assert(false); else if(dg[i][j] == -1) sum += db[i][j]+mb; else if(db[i][j] == -1) sum += dg[i][j]; else sum += min(dg[i][j], db[i][j]+mb); ++num; } } } assert(num > 0); sum /= num; return sum >= mb; } int main() { cin >> W >> H; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { cin >> mas[i][j]; if(mas[i][j] == 's') S = Pi(i, j), mas[i][j] = '.'; else if(mas[i][j] == 'g') G = Pi(i, j); else if(mas[i][j] == '*') Bs.emplace_back(i, j); } } db = bfs(Bs); dg = bfs({G}); dbl ans = dg[S.first][S.second]; //cout<<ans<<endl; /* bool flag = false; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { if(mas[i][j] == '.' && dg[i][j] == -1 && db[i][j] == -1) flag = true; } } */ if(ans == -1) ans = DBL_MAX/2; else if(db[S.first][S.second] == -1) { //else if(flag) { cout << fixed << setprecision(12) << ans << endl; return 0; } dbl lb = 0, ub = 1e20; for(int i = 0; i < 200; ++i) { dbl mb = (lb+ub)/2; if(check(mb)) lb = mb; else ub = mb; } ans = min(ans, db[S.first][S.second]+lb); cout << fixed << setprecision(12) << ans << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) #define MAX_V 1000 using namespace std; typedef long long ll; typedef pair<int,int> P; const long long int MOD = 1000000007; const long long int INF = 100000000000000; int h, w; P st, go; char b[500][500]; ll gist[500][500], wist[500][500]; queue<P> que; int dh[4] = {1,0,-1,0}; int dw[4] = {0,1,0,-1}; int flo = 0; int main(){ cin >> w >> h; rep(i,h) rep(j,w) cin >> b[i][j]; rep(i,h) rep(j,w){ if(b[i][j] == 's'){ st.first = i; st.second = j; b[i][j] = '.'; } if(b[i][j] == '.'){ flo++; } if(b[i][j] == 'g'){ go.first = i; go.second = j; b[i][j] = '.'; } } rep(i,h) rep(j,w){ gist[i][j] = INF; wist[i][j] = INF; } gist[go.first][go.second] = 0; que.push(go); while(!que.empty()){ P p = que.front(); que.pop(); rep(i,4){ int nh = p.first+dh[i]; int nw = p.second+dw[i]; if(0>nh||nh>=h||0>nw||nw>=w) continue; if(b[nh][nw] != '.') continue; if(gist[nh][nw] <= gist[p.first][p.second]+1) continue; gist[nh][nw] = gist[p.first][p.second]+1; que.push(P(nh,nw)); } } rep(i,h) rep(j,w){ if(b[i][j] == '*'){ que.push(P(i,j)); wist[i][j] = 0; } } while(!que.empty()){ P p = que.front(); que.pop(); rep(i,4){ int nh = p.first+dh[i]; int nw = p.second+dw[i]; if(0>nh||nh>=h||0>nw||nw>=w) continue; if(b[nh][nw] != '.') continue; if(wist[nh][nw] <= wist[p.first][p.second]+1) continue; wist[nh][nw] = wist[p.first][p.second]+1; que.push(P(nh,nw)); } } long double s = 0.0, e = 1000000000000.0, mid; rep(u,1000){ mid = (s+e)/2.0; long double sum = 0.0; rep(i,h) rep(j,w){ if(b[i][j] == '.' && go != P(i,j)){ sum += min((long double)gist[i][j],wist[i][j]+mid); } } sum /= (long double)flo; if(mid > sum){ e = mid; } else{ s = mid; } } long double ans = min((long double)gist[st.first][st.second],wist[st.first][st.second]+mid); printf("%.15Lf\n",ans); }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} template<class T> inline T sqr(T x) {return x*x;} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define int ll #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(), (a).rend() #define pb push_back #define mp make_pair #define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define exist(s,e) ((s).find(e)!=(s).end()) #define range(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) range(i,0,n) #define clr(a,b) memset((a), (b) ,sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; const double eps = 1e-10; const double pi = acos(-1.0); const ll INF =1LL << 62; const int inf =1 << 29; vs field; int w, h; //((cost, (y, x)) typedef pair<int, pii> State; #define F first #define S second vi dx = { 1, 0,-1, 0}; vi dy = { 0,-1, 0, 1}; void bfs(char tar, vvi & minDist){ minDist = vvi(h, vi(w, inf)); priority_queue<State, vector<State>, greater<State>> q; rep(y, h){ rep(x, w){ if(field[y][x] != tar) continue; q.push(mp(0, mp(y, x))); } } while(!q.empty()){ int cur_cost = q.top().F; pii cur_pos = q.top().S; q.pop(); if(minDist[cur_pos.F][cur_pos.S] != inf) continue; if(field[cur_pos.F][cur_pos.S] == '.'){ minDist[cur_pos.F][cur_pos.S] = cur_cost; } int next_cost = cur_cost + 1; rep(i, 4){ pii next_pos = mp(cur_pos.F + dy[i], cur_pos.S + dx[i]); if(field[next_pos.F][next_pos.S] == '#' || field[next_pos.F][next_pos.S] == 'g' || field[next_pos.F][next_pos.S] == '*' || minDist[next_pos.F][next_pos.S] != inf) continue; q.push(mp(next_cost, next_pos)); } } } signed main(void){ for(; cin >> w >> h;){ field = vs(h); pii s; int num_f = 0; rep(y, h){ cin >> field[y]; rep(x, w){ if(field[y][x] == 's'){ s = mp(y, x); field[y][x] = '.'; } if(field[y][x] == '.'){ num_f++; } } } vvi minDist_s, minDist_g; bfs('*', minDist_s); bfs('g', minDist_g); // rep(y, h){ // rep(x, w){ // printf("%4d", (minDist_s[y][x] == inf ? -1:minDist_s[y][x])); // } // cout << endl; // } // cout << endl; // rep(y, h){ // rep(x, w){ // printf("%4d", (minDist_g[y][x] == inf ? -1:minDist_g[y][x])); // } // cout << endl; // } // cout << "----------" <<endl; vector<pair<int, pii>> deltaSG; int sum_dist = 0, num_tos = 0; rep(y, h){ rep(x, w){ if(field[y][x] != '.') continue; if(minDist_g[y][x] != inf){ sum_dist += minDist_g[y][x]; if(minDist_s[y][x] != inf){ deltaSG.pb(mp(minDist_g[y][x] - minDist_s[y][x], mp(y, x))); } } else{ sum_dist += minDist_s[y][x]; num_tos++; } } } sort(all(deltaSG), greater<pair<int, pii>>()); #define F first #define S second long double e = 1.0 / (num_f - num_tos) * sum_dist; for(auto state : deltaSG){ pii pos = state.S; num_tos++; sum_dist = sum_dist - minDist_g[pos.F][pos.S] + minDist_s[pos.F][pos.S]; if(num_f == num_tos) break; long double cur = 1.0 / (num_f - num_tos) * sum_dist; if(cur < e) e = cur; else break; } long double res = minDist_s[s.F][s.S] + e; if(minDist_g[s.F][s.S] != inf) res = min(res, (long double)minDist_g[s.F][s.S]); printf("%.12Lf\n", res); } return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))<EPS) using namespace std; typedef long double ld; const int MAX = 501,IINF = INT_MAX; const ld LDINF = 1e100; int H,W,sx,sy,gx,gy; ld mincost[MAX][MAX][2]; // mincost[][][0] => from start, [1] = > from star char c[MAX][MAX]; bool ban[MAX][MAX]; vector<int> star,plane; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; inline bool isValid(int x,int y) { return 0 <= x && x < W && 0 <= y && y < H; } inline void bfs(vector<int> sp,vector<int> Forbidden,int type){ rep(i,H)rep(j,W) mincost[i][j][type] = LDINF, ban[i][j] = false; queue<int> que; rep(i,(int)sp.size()) que.push(sp[i]), mincost[sp[i]/W][sp[i]%W][type] = 0; rep(i,(int)Forbidden.size()) ban[Forbidden[i]/W][Forbidden[i]%W] = true; while(!que.empty()){ int cur = que.front(); que.pop(); rep(i,4){ int nx = cur % W + dx[i], ny = cur / W + dy[i]; if( c[ny][nx] == '#' ) continue; if( ban[ny][nx] ) continue; if( mincost[ny][nx][type] == LDINF ) { mincost[ny][nx][type] = mincost[cur/W][cur%W][type] + 1; que.push(nx+ny*W); } } } } bool check(ld E){ ld T = 0; rep(i,(int)plane.size()){ int x = plane[i] % W, y = plane[i] / W; T += min(mincost[y][x][0],mincost[y][x][1]+E); } ld len = plane.size(); return len * E > T; } int main(){ cin >> W >> H; rep(i,H)rep(j,W){ cin >> c[i][j]; if( c[i][j] == 's' ) sx = j, sy = i, c[i][j] = '.'; if( c[i][j] == 'g' ) gx = j, gy = i; if( c[i][j] == '*' ) star.push_back(j+i*W); if( c[i][j] == '.' ) plane.push_back(j+i*W); } vector<int> sp,forbidden; sp.push_back(gx+gy*W); forbidden = star; forbidden.push_back(gx+gy*W); bfs(sp,forbidden,0); sp = star; forbidden.push_back(gx+gy*W); //forbidden.clear(); bfs(sp,forbidden,1); ld L = 0, R = 1e160, M = 0; rep(i,1020){ M = ( L + R ) * (ld)0.5; if( check(M) ) R = M; else L = M; } cout << setiosflags(ios::fixed) << setprecision(20) << min((ld)mincost[sy][sx][0],(ld)mincost[sy][sx][1]+L) << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
from collections import deque import sys readline = sys.stdin.readline write = sys.stdout.write def solve(): W, H = map(int, readline().split()) S = [readline().strip() for i in range(H)] R = [[0]*W for i in range(H)] P = [] cnt = 0 for i in range(H): Si = S[i] for j in range(W): if Si[j] == 's': sx = j; sy = i R[i][j] = 1 cnt += 1 elif Si[j] == 'g': gx = j; gy = i elif Si[j] == '*': P.append((j, i)) elif Si[j] == '.': cnt += 1 R[i][j] = 1 dd = ((-1, 0), (0, -1), (1, 0), (0, 1)) INF = 10**18 dist1 = [[INF]*W for i in range(H)] que = deque(P) for x, y in P: dist1[y][x] = 0 while que: x, y = que.popleft() c = dist1[y][x]+1 for dx, dy in dd: nx = x + dx; ny = y + dy if S[ny][nx] in '*#' or dist1[ny][nx] != INF: continue dist1[ny][nx] = c que.append((nx, ny)) dist2 = [[INF]*W for i in range(H)] que = deque([(gx, gy)]) dist2[gy][gx] = 0 while que: x, y = que.popleft() c = dist2[y][x]+1 for dx, dy in dd: nx = x + dx; ny = y + dy if S[ny][nx] in '*#' or dist2[ny][nx] != INF: continue dist2[ny][nx] = c que.append((nx, ny)) left = 0; right = 10**21 BASE = 10**9 for i in range(71): mid = (left + right) // 2 su = 0 for i in range(H): for j in range(W): if not R[i][j]: continue su += min(dist1[i][j]*BASE + mid, dist2[i][j]*BASE) if mid*cnt > su: right = mid else: left = mid write("%.16f\n" % min(dist1[sy][sx] + left / BASE, dist2[sy][sx])) solve()
PYTHON3
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<iostream> #include<climits> #include<cstdio> #include<queue> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) typedef long long ll; const int N = 500; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; const ll inf = 1LL<<60; char m[N][N+1]; ll dcost[N][N];//直接ゴールまで行くコスト ll scost[N][N];//バネを使った時のコスト void bfs(int r,int c,char s,ll cost[N][N]){ queue<int> Q; rep(i,r)rep(j,c) if (m[i][j] == s)cost[i][j] = 0,Q.push(i*c+j); else cost[i][j] = inf; while(!Q.empty()){ int y = Q.front()/c,x=Q.front()%c;Q.pop(); // cout << y <<" " << x << endl; rep(i,4){ int ney = y+dy[i],nex = x+dx[i]; if (m[ney][nex] == '*' || m[ney][nex] == 'g' || m[ney][nex] == '#' || cost[ney][nex] != inf)continue; cost[ney][nex] = cost[y][x] + 1; Q.push(ney*c+nex); } } } //brute force,とりあえず収束するまで回してみる //入力がgs*みたいなのが来ると落ちる可能性がある double exp[N][N]; double bf(int r,int c){ double e=0;//期待値の総和を持っておくと役に立つ double pos = 0;//バネで行く可能性のある場所の数 int sy,sx; rep(i,r){ rep(j,c){ if (m[i][j] == '*' || m[i][j] == 'g' || m[i][j] == '#')continue; exp[i][j] = dcost[i][j]; e += dcost[i][j]; pos++; if (m[i][j] == 's')sy = i,sx = j; } } double L = 0,R=1e20; double ans = -1; rep(i,300){ double mid = (L+R)/2.; double newe = 0; rep(i,r){ rep(j,c){ if (m[i][j] == 'g' || m[i][j] == '#' || m[i][j] == '*')continue; double tmp = dcost[i][j]; double tmp2 = scost[i][j] + mid/pos; newe += min(tmp,tmp2); } } //cout <<"owari " << newe <<" " << L << " " << mid <<" " << R << endl; if (newe < mid){//予想より小さかった ans = mid; R = mid - 1e-10; }else {//予想より大きかった L = mid + 1e-10; } } //cout << ans<< " " << ans/pos << endl; return min((double)dcost[sy][sx],scost[sy][sx]+ans/pos); /* while(true){ bool isupdate = false; double newe = 0; rep(i,r){ rep(j,c){ if (m[i][j] == 'g' || m[i][j] == '#')continue;//ここは考えない。 double tmp = dcost[i][j]; double tmp2 = scost[i][j] + (e)/(pos); if (exp[i][j] > min(tmp,tmp2)){ isupdate = true; e -= exp[i][j]; exp[i][j] = min(tmp,tmp2); e += exp[i][j]; } newe += exp[i][j]; } } e = newe; cout <<"debug " << endl; rep(i,r){ rep(j,c){ printf("%.3lf ",exp[i][j]); } printf("\n"); } cout <<"--------------==" << endl; if (!isupdate)break; } double hoge = 0; rep(i,r){ rep(j,c){ if (m[i][j] == '#' || m[i][j] == 'g' || m[i][j] == '*')continue; hoge += min((double)dcost[i][j],scost[i][j] + e/pos); cout << min((double)dcost[i][j],scost[i][j] + e/pos) <<" " ; } cout << endl; } cout <<"hoge " << e <<" " << hoge/pos << endl; */ rep(i,r)rep(j,c)if (m[i][j] == 's')return exp[i][j]; } int main(){ int r,c; while(cin>>c>>r && r){ rep(i,r)cin>>m[i]; bfs(r,c,'*',scost); bfs(r,c,'g',dcost); printf("%.12lf\n",bf(r,c)); } }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> #define int long long using namespace std; template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); } template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); } const int INF = 1e+15; const double EPS = 1e-8; const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1}; int w,h,sx,sy,gx,gy; string field[510]; template<class T = int> vector<vector<T>> bfs(int ind){ using P = pair<int,int>; vector<vector<T>> dist(h,vector<T>(w,INF)); queue<P> que; if(!ind){ dist[gx][gy] = 0; que.emplace(gx,gy); }else{ for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ if(field[i][j] == '*'){ dist[i][j] = 0; que.emplace(i,j); } } } } for(T d = 0;;d++){ int siz = que.size(); if(siz == 0) break; for(int i = 0;i < siz;i++){ int x,y; tie(x,y) = que.front();que.pop(); for(int j = 0;j < 4;j++){ int nx = x + dx[j],ny = y + dy[j]; if(nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] != '#' && (ind || field[nx][ny] != '*') && dist[nx][ny] == INF){ dist[nx][ny] = d + 1; que.emplace(nx,ny); } } } } return dist; } signed main(){ cin >> w >> h; for(int i = 0;i < h;i++) cin >> field[i]; for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ if(field[i][j] == 's') sx = i,sy = j; if(field[i][j] == 'g') gx = i,gy = j; } } auto dist1 = bfs(0),dist2 = bfs(1); int d = 0,xcnt = 0,cnt = 0; vector<int> vec; for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ if(field[i][j] != '#' && field[i][j] != '*'){ cnt++; if(dist1[i][j] <= dist2[i][j]){ if(dist1[i][j] != INF) d += dist1[i][j]; }else{ d += dist2[i][j]; xcnt++; if(dist1[i][j] != INF) vec.push_back(dist1[i][j] - dist2[i][j]); } } } } double ans = dist1[sx][sy]; sort(vec.begin(),vec.end()); for(int v : vec){ double x = (double)d / (cnt - xcnt - 1); //cout << v << " " << x << " " << d << " " << cnt - xcnt << " " << endl; if(x < v + EPS) chmin(ans,dist2[sx][sy] + x); d += v; xcnt--; } double x = (double)d / (cnt - xcnt - 1); chmin(ans,dist2[sx][sy] + x); printf("%.15lf\n",ans); }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<stdio.h> #include<algorithm> #include<queue> using namespace std; char str[600][600]; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; long double ijk[600][600]; long double ijk2[600][600]; long double eps=1e-10L; pair<long double,pair<int,int> > v[1000000]; int t[600][600]; int main(){ int a,b; scanf("%d%d",&b,&a); for(int i=0;i<a;i++)scanf("%s",str[i]); for(int i=0;i<a;i++)for(int j=0;j<b;j++) ijk[i][j]=ijk2[i][j]=1e20L; int sr,sc,gr,gc; queue<pair<int,int> > Q; int cnt=0; for(int i=0;i<a;i++){ for(int j=0;j<b;j++){ if(str[i][j]=='s'){sr=i;sc=j;} if(str[i][j]=='g'){ gr=i;gc=j; ijk[i][j]=0; Q.push(make_pair(i,j)); } } } for(int i=0;i<a;i++)for(int j=0;j<b;j++)if(str[i][j]!='#'&&str[i][j]!='*'&&str[i][j]!='g')cnt++; while(Q.size()){ int row=Q.front().first; int col=Q.front().second;Q.pop(); for(int i=0;i<4;i++){ if(0<=row+dx[i]&&row+dx[i]<a&&0<=col+dy[i]&&col+dy[i]<b&&str[row+dx[i]][col+dy[i]]!='#'&&str[row+dx[i]][col+dy[i]]!='*'&&ijk[row+dx[i]][col+dy[i]]>eps+ijk[row][col]+1){ ijk[row+dx[i]][col+dy[i]]=ijk[row][col]+1; Q.push(make_pair(row+dx[i],col+dy[i])); } } } for(int i=0;i<a;i++)for(int j=0;j<b;j++){ if(str[i][j]=='*'){ ijk2[i][j]=0; Q.push(make_pair(i,j)); } } while(Q.size()){ int row=Q.front().first; int col=Q.front().second;Q.pop(); for(int i=0;i<4;i++){ if(0<=row+dx[i]&&row+dx[i]<a&&0<=col+dy[i]&&col+dy[i]<b&&str[row+dx[i]][col+dy[i]]!='#'&&str[row+dx[i]][col+dy[i]]!='*'&&ijk2[row+dx[i]][col+dy[i]]>eps+ijk2[row][col]+1){ ijk2[row+dx[i]][col+dy[i]]=ijk2[row][col]+1; Q.push(make_pair(row+dx[i],col+dy[i])); } } } int sz=0; long double L=0; long double R=0; int n=0; int m=0; for(int i=0;i<a;i++){ for(int j=0;j<b;j++){ if(str[i][j]=='s'||str[i][j]=='.'){ if(ijk[i][j]<1e10L&&ijk2[i][j]<1e10L) v[sz++]=make_pair(ijk[i][j]-ijk2[i][j],make_pair(i,j)); else if(ijk[i][j]<1e10L){ n++; L+=ijk[i][j]; } else if(ijk2[i][j]<1e10L){ m++; R+=ijk2[i][j]; } } } } std::sort(v,v+sz); for(int i=0;i<a;i++)for(int j=0;j<b;j++){ if(ijk[i][j]<1e10L&&ijk2[i][j]<1e10L) if(str[i][j]=='s'||str[i][j]=='.')R+=ijk2[i][j]; } long double ret=ijk[sr][sc]; // for(int i=0;i<a;i++)for(int j=0;j<b;j++) // printf("%d %d: %Lf %Lf\n",i,j,ijk[i][j],ijk2[i][j]); for(int i=0;i<=sz;i++){ if(t[sr][sc]){ break; } if(n+i){ // printf("%d: %Lf\n",i,ijk2[sr][sc]+R/(sz-i)*((long double)sz/i-1)+L/(n+i)); ret=min(ret,ijk2[sr][sc]+R/(m+sz-i)*((long double)(sz+m+n)/(n+i)-1)+L/(n+i)); } if(i<sz){ t[v[i].second.first][v[i].second.second]=1; L+=ijk[v[i].second.first][v[i].second.second]; R-=ijk2[v[i].second.first][v[i].second.second]; } } printf("%.12Lf\n",ret); }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> using namespace std; const int M = 1000000007; const long long LM = (long long)1e17; int d[5] = { 0, 1, 0, -1, 0 }; int main() { int w, h, si, sj, gi, gj; cin >> w >> h; vector<string> s(h); queue<pair<int, int>> q; vector<vector<int>> dsp(h, vector<int>(w, M)); vector<vector<long long>> dg(h, vector<long long>(w, LM * 2)); for (int i = 0; i < h; ++i) { cin >> s[i]; for (int j = 0; j < w; ++j) { if (s[i][j] == 's') { si = i; sj = j; } else if (s[i][j] == 'g') { gi = i; gj = j; dg[i][j] = 0; } else if (s[i][j] == '*') { dsp[i][j] = 0; q.push(make_pair(i, j)); } } } while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); int pi = p.first, pj = p.second; for (int i = 0; i < 4; ++i) { int ti = pi + d[i], tj = pj + d[i + 1]; if (s[ti][tj] != '#' && dsp[ti][tj] > dsp[pi][pj] + 1) { dsp[ti][tj] = dsp[pi][pj] + 1; q.push(make_pair(ti, tj)); } } } q.push(make_pair(gi, gj)); while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); int pi = p.first, pj = p.second; for (int i = 0; i < 4; ++i) { int ti = pi + d[i], tj = pj + d[i + 1]; if (s[ti][tj] != '#' && s[ti][tj] != '*' && dg[ti][tj] > dg[pi][pj] + 1) { dg[ti][tj] = dg[pi][pj] + 1; q.push(make_pair(ti, tj)); } } } long long l = 0, r = LM; double k = -1; while (l <= r) { long long m = (l + r) / 2; int c = 0; long long dist = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s[i][j] == 's' || s[i][j] == '.') { ++c; if (dg[i][j] > dsp[i][j] + m) { --c; dist += dsp[i][j]; } else { dist += dg[i][j]; } } } } k = dist / (double)c; if (m - 1e-9 < k && k < m + 1) { break; } else if (m < k) { l = m + 1; } else { r = m - 1; } } printf("%.12lf\n", min((double)dg[si][sj], dsp[si][sj] + k)); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; } inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const double INF = 1e18; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int W, H; void bfs(double dist[500][500], int sx, int sy, string grid[500]){ queue<int> qx, qy; qx.push(sx); qy.push(sy); dist[sy][sx] = 0; while(!qx.empty()){ int x = qx.front(), y = qy.front(); qx.pop(); qy.pop(); REP(r, 4){ int nx = x + dx[r], ny = y + dy[r]; if(valid(nx, ny, W, H) && grid[ny][nx] == '.'){ if(dist[ny][nx] == -1.0 || dist[ny][nx] > dist[y][x] + 1){ dist[ny][nx] = dist[y][x] + 1; qx.push(nx); qy.push(ny); } } } } } int main(){ while(cin >> W >> H && W){ string grid[500]; REP(i, H) cin >> grid[i]; int sx, sy; REP(y, H) REP(x, W) if(grid[y][x] == 's') { sx = x, sy = y; grid[y][x] = '.'; } int N = 0; REP(y, H) REP(x, W) if(grid[y][x] == '.') N++; double dist_goal[500][500], dist_spring[500][500]; REP(y, H) REP(x, W) dist_goal[y][x] = dist_spring[y][x] = -1.0; REP(y, H) REP(x, W) if(grid[y][x] == 'g') bfs(dist_goal, x, y, grid); REP(y, H) REP(x, W) if(grid[y][x] == '*') bfs(dist_spring, x, y, grid); REP(y, H) REP(x, W) if(dist_goal[y][x] == -1) dist_goal[y][x] = INF; REP(y, H) REP(x, W) if(dist_spring[y][x] == -1) dist_spring[y][x] = INF; double lb = 0, ub = 1e18; REP(_, 200){ const long double sum = (ub + lb) * 0.5; long double S = 0; REP(y, H) REP(x, W)if(grid[y][x] == '.'){ long double exp = min((long double)dist_goal[y][x], dist_spring[y][x] + sum / N); S += exp; } if(sum - S > 0){ ub = sum; }else{ lb = sum; } } double all_exp = lb / N; printf("%.12f\n", min((double)dist_goal[sy][sx], dist_spring[sy][sx] + all_exp)); } return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def bs(f, mi, ma): st = time.time() mm = -1 mi = fractions.Fraction(mi, 1) ma = fractions.Fraction(ma, 1) while ma > mi + eps: gt = time.time() mm = (ma+mi) / 2 if gt - st > 35: return mm if isinstance(mm, float): tk = max(1, int(10**15 / mm)) mm = fractions.Fraction(int(mm*tk), tk) if float(mm) == float(ma) or float(mm) == float(mi): return mm if f(mm): mi = mm else: ma = mm if f(mm): return mm + eps return mm def main(): rr = [] def f(w,h): a = [S() for _ in range(h)] si = sj = -1 for i in range(1,h-1): for j in range(1,w-1): if a[i][j] == 's': si = i sj = j break def search(sc): d = collections.defaultdict(lambda: inf) q = [] for i in range(1,h-1): for j in range(1,w-1): if a[i][j] == sc: d[(i,j)] = 0 heapq.heappush(q, (0, (i,j))) v = collections.defaultdict(bool) while len(q): k, u = heapq.heappop(q) if v[u]: continue v[u] = True for di,dj in dd: ni = u[0] + di nj = u[1] + dj if not a[ni][nj] in '.s': continue uv = (ni,nj) if v[uv]: continue vd = k + 1 if d[uv] > vd: d[uv] = vd heapq.heappush(q, (vd, uv)) return d gd = search('g') wd = search('*') cgs = [] cws = [] for i in range(1,h-1): for j in range(1,w-1): if not a[i][j] in '.s': continue if gd[(i,j)] >= inf: cgs.append((i,j)) else: cws.append((i,j)) cc = len(cgs) + len(cws) cgc = len(cgs) cgw = sum([wd[(i,j)] for i,j in cgs]) sgw = [(inf,0,0)] for i,j in cws: gt = gd[(i,j)] wt = wd[(i,j)] sgw.append((gt-wt, wt, gt)) sgw.sort() # print(sgw[:5], sgw[-5:]) ls = len(sgw) - 1 # print('ls', ls, len(cws)) def ff(t): # print('ff', t) s = cgw si = bisect.bisect_left(sgw,(t,0,0)) # print('si', si, sgw[si], sgw[si-1]) tc = cgc s2 = cgw tc2 = tc + ls - si for i in range(si): s2 += sgw[i][2] for i in range(si,ls): s2 += sgw[i][1] # for i,j in cws: # gt = gd[(i,j)] # wt = wd[(i,j)] # if t + wt < gt: # s += wt # tc += 1 # else: # s += gt # print('s,s2,tc,tc2', s,s2,tc,tc2) av = (s2 + t*tc2) / cc # print('av,t', float(av), float(t)) return t < av k = bs(ff, 0, 10**10) gt = gd[(si,sj)] wt = wd[(si,sj)] r = gt if wt + k < gt: r = wt + k # print('r', r) return '{:0.10f}'.format(float(r)) while 1: n,m = LI() if n == 0: break rr.append(f(n,m)) break return '\n'.join(map(str, rr)) print(main())
PYTHON3
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} const long double INF = 1e10; using vd = vector<long double>; using V = vector<vd>; using pi = pair<int,int>; const int dy[4]={1,-1,0,0}, dx[4]={0,0,1,-1}; int main(){ int w,h; cin >>w >>h; vector<string> s(h); rep(i,h) cin >>s[i]; auto IN = [&](int y, int x){ return 0<=y && y<h && 0<=x && x<w; }; auto calc = [&](double m){ V dp(h,vd(w,INF)); queue<pi> que; rep(i,h)rep(j,w){ if(s[i][j]=='g'){ dp[i][j]=0; que.push({i,j}); } if(s[i][j]=='*'){ dp[i][j]=m; que.push({i,j}); } } while(!que.empty()){ pi pos = que.front(); que.pop(); rep(d,4){ int ny = pos.fi+dy[d], nx = pos.se+dx[d]; if(IN(ny,nx) && s[ny][nx]!='#' && s[ny][nx]!='*'){ if(dp[ny][nx] > dp[pos.fi][pos.se]+1){ dp[ny][nx] = dp[pos.fi][pos.se]+1; que.push({ny,nx}); } } } } return dp; }; long double l=0, r=INF; rep(loop,80){ long double m = (l+r)/2; V E = calc(m); int n = 0; rep(i,h)rep(j,w)if(s[i][j]=='.' || s[i][j]=='s') ++n; long double sumE = 0; rep(i,h)rep(j,w){ if(s[i][j]=='.' || s[i][j]=='s') sumE += E[i][j]/n; } if(sumE<m) r=m; else l=m; } // dbg(l); // dbg(r-l); V E = calc(l); long double ans=-1; rep(i,h)rep(j,w)if(s[i][j]=='s') ans = E[i][j]; printf("%.15Lf\n", ans); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<cstdio> #include<queue> #include<vector> #include<algorithm> using namespace std; const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; char field[550][550]; int H,W; int dis[550][550],dis2[550][550]; vector<int> si,sj; void bfs(int res[550][550]){ for(int i=0;i<H;i++) for(int j=0;j<W;j++) res[i][j]=-1; queue<int> qi,qj; for(int i=0;i<si.size();i++){ qi.push(si[i]); qj.push(sj[i]); res[si[i]][sj[i]]=0; } while(!qi.empty()){ int i=qi.front(); qi.pop(); int j=qj.front(); qj.pop(); int c=res[i][j]; for(int k=0;k<4;k++){ int nc=c+1; int ni=i+dx[k]; int nj=j+dy[k]; if(field[ni][nj]!='.') continue; if(res[ni][nj]!=-1&&res[ni][nj]<=nc) continue; res[ni][nj]=nc; qi.push(ni); qj.push(nj); } } } bool check(long double x){ int cnt=0; long double sum=0; for(int i=0;i<H;i++) for(int j=0;j<W;j++){ if(field[i][j]!='.') continue; long double tmp=1e11; if(dis[i][j]!=-1){ tmp=dis[i][j]; } if(dis2[i][j]!=-1){ tmp=min(tmp,x+dis2[i][j]); } sum+=tmp; cnt++; } if(x<sum/cnt) return true; return false; } int main(){ scanf("%d%d",&W,&H); for(int i=0;i<H;i++){ scanf("%s",field[i]); } int sti=-1,stj=-1; int gi=-1,gj=-1; int cnt=0; for(int i=0;i<H;i++) for(int j=0;j<W;j++){ if(field[i][j]=='s'){ field[i][j]='.'; sti=i,stj=j; } else if(field[i][j]=='g'){ field[i][j]='#'; gi=i,gj=j; } else if(field[i][j]=='*'){ si.push_back(i); sj.push_back(j); cnt++; } } bfs(dis2); si.clear(); sj.clear(); si.push_back(gi); sj.push_back(gj); bfs(dis); if(cnt==0){ printf("%d\n",dis[sti][stj]); return 0; } // for(int i=0;i<H;i++){ // for(int j=0;j<W;j++) printf("%d ",dis[i][j]); // printf("\n"); // } long double lb=0,ub=1e10; for(int stage=0;stage<500;stage++){ long double mid=(ub+lb)/2; bool flg=check(mid); if(flg) lb=mid; else ub=mid; } long double ans=1e10; if(dis[sti][stj]!=-1) ans=dis[sti][stj]; if(dis2[sti][stj]!=-1) ans=min(ans,dis2[sti][stj]+lb); printf("%.9llf\n",ans); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) typedef long long LL; struct position{ LL x; LL y; LL count; }; int main(){ LL w,h; LL houkou[5]={0,1,0,-1,0}; cin >> w >> h; vector<vector<LL>> field(h,vector<LL>(w)); list<struct position> spring; char c; LL sx,sy,gx,gy; LL numofFloor=0; for(LL i=0;i<h;i++){ for(LL j=0;j<w;j++){ cin >> c; if(c=='.'){ field[i][j]=1; numofFloor++; }else if(c=='#'){ field[i][j]=0; }else if(c=='*'){ field[i][j]=2; spring.push_back({j,i,0}); }else if(c=='s'){ field[i][j]=1; sx=j; sy=i; numofFloor++; }else{ field[i][j]=3; gx=j; gy=i; } } } vector<vector<LL>> toGoal(h,vector<LL>(w,0)); vector<vector<LL>> toSpring(h,vector<LL>(w,0)); queue<struct position> bfs; struct position now; LL nx,ny; bfs.push({gx,gy,0}); while(!bfs.empty()){ now=bfs.front(); bfs.pop(); for(LL i=0;i<4;i++){ nx=now.x+houkou[i]; ny=now.y+houkou[i+1]; if(0<=nx&&nx<=w&&0<=ny&&ny<=h){ if(toGoal[ny][nx]==0&&field[ny][nx]==1){ toGoal[ny][nx]=now.count+1; bfs.push({nx,ny,now.count+1}); } } } } for(auto itr=spring.begin();itr!=spring.end();itr++){ bfs.push({(*itr).x,(*itr).y,0}); } while(!bfs.empty()){ now=bfs.front(); bfs.pop(); for(LL i=0;i<4;i++){ nx=now.x+houkou[i]; ny=now.y+houkou[i+1]; if(0<=nx&&nx<=w&&0<=ny&&ny<=h){ if(toSpring[ny][nx]==0&&field[ny][nx]==1){ toSpring[ny][nx]=now.count+1; bfs.push({nx,ny,now.count+1}); } } } } // for(LL i=0;i<h;i++){ // for(LL j=0;j<w;j++){ // cout << toGoal[i][j] << " "; // } // cout << endl; // } // cout << endl; // for(LL i=0;i<h;i++){ // for(LL j=0;j<w;j++){ // cout << toSpring[i][j] << " "; // } // cout << endl; // } // cout << numofFloor << endl; cout << fixed << setprecision(12); double left=0; double right=10000000000000000; double mid; double count; int spcount=0; while(right-left>0.0000000002*right){ mid=(left+right)/2; spcount=0; count=0; for(LL i=1;i<h-1;i++){ for(LL j=1;j<w-1;j++){ if(field[i][j]==1){ if(toGoal[i][j]!=0){ if(toSpring[i][j]!=0){ if((double)toGoal[i][j]<(double)toSpring[i][j]+(double)mid/(double)numofFloor){ count+=(double)toGoal[i][j]; }else{ count+=(double)toSpring[i][j]; spcount++; } }else{ count+=(double)toGoal[i][j]; } }else{ if(toSpring[i][j]!=0){ count+=(double)toSpring[i][j]; spcount++; } } } } } count+=(double)mid*(double)spcount/(double)numofFloor; if(count<mid){ right=mid; }else{ left=mid; } } // cout << left << endl; // cout << right << endl; // cout << count << endl; // cout << toSpring[sy][sx] << endl; // for(LL i=0;i<h;i++){ // for(LL j=0;j<w;j++){ // if(field[i][j]==1){ // if(toGoal[i][j]!=0){ // if(toSpring[i][j]!=0){ // cout << min((double)toGoal[i][j],(double)toSpring[i][j]+(double)left/(double)numofFloor) << " "; // }else{ // cout << (double)toGoal[i][j] << " "; // } // }else{ // if(toSpring[sy][sx]!=0) cout << (double)toSpring[i][j]+(double)left/(double)numofFloor << " "; // } // }else{ // cout << 0 << " "; // } // } // cout << endl; // } if(toGoal[sy][sx]!=0){ if(toSpring[sy][sx]!=0){ cout << min((double)toGoal[sy][sx],(double)toSpring[sy][sx]+(double)left/(double)numofFloor) << endl; }else{ cout << (double)toGoal[sy][sx] << endl; } }else{ if(toSpring[sy][sx]!=0) cout << (double)toSpring[sy][sx]+(double)left/(double)numofFloor << endl; } return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template<class S, class T> istream& operator>>(istream& is, pair<S,T>& p){ return is >> p.FF >> p.SS; } template<class S, class T> ostream& operator<<(ostream& os, const pair<S,T>& p){ return os << p.FF << " " << p.SS; } template<class T> void maxi(T& x, T y){ if(x < y) x = y; } template<class T> void mini(T& x, T y){ if(x > y) x = y; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; const int INF = 1e9; int dx[] = {-1,0,1,0}; int dy[] = {0,-1,0,1}; int H, W; double ex[500][500]; string vs[500]; using D = pair<double,PII>; void bfs(PII p, VVI& dist){ queue<PII> q; q.push(p); dist[p.FF][p.SS] = 0.; while(!q.empty()){ p = q.front(); q.pop(); REP(d,4){ PII np(p.FF + dy[d], p.SS + dx[d]); if(vs[np.FF][np.SS] != '.') continue; int nc = dist[p.FF][p.SS] + 1; if(dist[np.FF][np.SS] > nc){ dist[np.FF][np.SS] = nc; q.push(np); } } } } int main(){ cin.tie(0); ios_base::sync_with_stdio(false); cin >> W >> H; REP(y,H) cin >> vs[y]; fill((double*)ex, (double*)ex+500*500, INF); PII S, G; vector<PII> star; int sum = 0; REP(y,H) REP(x,W){ if(vs[y][x] == 's'){ vs[y][x] = '.'; S = MP(y,x); ++sum; } else if(vs[y][x] == 'g'){ G = MP(y,x); ++sum; } else if(vs[y][x] == '*'){ star.PB(MP(y,x)); } } shuffle(ALL(star), mt19937()); VVI dist(H, VI(W, INF)); bfs(G, dist); VVI sp_dist(H, VI(W, INF)); for(auto&& p: star) bfs(p, sp_dist); using LD = long double; LD lb = 0., ub = 1e15; REP(i,100){ LD X = (lb + ub) / 2.; LD acc = 0.; int n = 0; REP(y,H) REP(x,W){ if(vs[y][x] == '.'){ ++n; if(dist[y][x] != INF){ if(sp_dist[y][x] != INF) acc += min((LD)dist[y][x], sp_dist[y][x] + X); else acc += dist[y][x]; } else acc += sp_dist[y][x] + X; } } if(acc / n < X) ub = X; else lb = X; } LD ans = 0.; if(dist[S.FF][S.SS] != INF){ if(sp_dist[S.FF][S.SS] != INF) ans += min((LD)dist[S.FF][S.SS], sp_dist[S.FF][S.SS] + lb); else ans += dist[S.FF][S.SS]; } else ans += sp_dist[S.FF][S.SS] + lb; cout << fixed << setprecision(11) << ans << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 500 struct LOC{ void set(int arg_row,int arg_col){ row = arg_row; col = arg_col; } int row,col; }; struct Info{ LOC loc; int to_goal,to_spring; bool is_floor; }; struct State{ State(int arg_row,int arg_col,int arg_sum_dist){ row = arg_row; col = arg_col; sum_dist = arg_sum_dist; } bool operator<(const struct State &arg) const{ return sum_dist > arg.sum_dist; } int row,col,sum_dist; }; char base_map[NUM][NUM+1]; int W,H; int floor_num; int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0}; int goal_table[NUM][NUM],spring_table[NUM][NUM]; LOC start,goal; Info info[NUM][NUM]; bool rangeCheck(int row,int col){ if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true; else{ return false; } } int main(){ scanf("%d %d",&W,&H); for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ info[row][col].loc.set(row,col); info[row][col].is_floor = false; } } floor_num = 0; for(int row = 0; row < H; row++){ scanf("%s",base_map[row]); for(int col = 0; col < W; col++){ switch(base_map[row][col]){ case '.': info[row][col].is_floor = true; floor_num++; break; case 's': info[row][col].is_floor = true; start.set(row,col); floor_num++; base_map[row][col] = '.'; break; case 'g': goal.set(row,col); break; case '#': //Do nothing break; case '*': //Do nothing break; } } } for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++)goal_table[row][col] = BIG_NUM; } priority_queue<State> Q; goal_table[goal.row][goal.col] = 0; Q.push(State(goal.row,goal.col,0)); int adj_row,adj_col,next_dist; while(!Q.empty()){ for(int i = 0; i < 4; i++){ adj_row = Q.top().row+diff_row[i]; adj_col = Q.top().col+diff_col[i]; if(rangeCheck(adj_row,adj_col) == false || base_map[adj_row][adj_col] != '.')continue; next_dist = Q.top().sum_dist+1; if(goal_table[adj_row][adj_col] > next_dist){ goal_table[adj_row][adj_col] = next_dist; Q.push(State(adj_row,adj_col,next_dist)); } } Q.pop(); } for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++)spring_table[row][col] = BIG_NUM; } for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ if(base_map[row][col] != '*')continue; spring_table[row][col] = 0; Q.push(State(row,col,0)); while(!Q.empty()){ for(int i = 0; i < 4; i++){ adj_row = Q.top().row+diff_row[i]; adj_col = Q.top().col+diff_col[i]; if(rangeCheck(adj_row,adj_col) == false || base_map[adj_row][adj_col] != '.')continue; next_dist = Q.top().sum_dist+1; if(spring_table[adj_row][adj_col] > next_dist){ spring_table[adj_row][adj_col] = next_dist; Q.push(State(adj_row,adj_col,next_dist)); } } Q.pop(); } } } int maximum = 0; map<pair<int,int>,int> MAP; for(int row = 0;row < H;row++){ for(int col = 0; col < W; col++){ if(info[row][col].is_floor){ info[row][col].to_goal = goal_table[row][col]; if(info[row][col].to_goal != BIG_NUM){ maximum = max(maximum,info[row][col].to_goal); } info[row][col].to_spring = spring_table[row][col]; MAP[make_pair(goal_table[row][col],spring_table[row][col])]++; } } } long double E = INFINITY; for(int value = 0; value <= maximum; value++){ long double a = 0; long double b = 0; for(auto it: MAP){ int to_goal,to_spring; to_goal = it.first.first; to_spring = it.first.second; int count = it.second; long double possib = count/(long double)floor_num; if(to_goal == BIG_NUM){ a += possib; b += possib*to_spring; }else if(to_spring == BIG_NUM){ b += possib*to_goal; }else{ if(to_goal <= to_spring+value){ b += possib*to_goal; }else{ a += possib; b += possib*to_spring; } } } E = min(E,b/(1-a)); } long double ans; if(info[start.row][start.col].to_goal == BIG_NUM){ ans = info[start.row][start.col].to_spring+E; }else if(info[start.row][start.col].to_spring == BIG_NUM){ ans = info[start.row][start.col].to_goal; }else{ ans = min<long double>(info[start.row][start.col].to_goal,info[start.row][start.col].to_spring+E); } printf("%.10Lf\n",ans); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; #define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,n) repl(i,0,n) #define mp(a,b) make_pair((a),(b)) #define pb(a) push_back((a)) #define all(x) (x).begin(),(x).end() #define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x)) #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} #define INF 21474836001234567 #define double long double int main(){ int w,h; cin>>w>>h; vector<string> vec(h); rep(i,h) cin>>vec[i]; int sx,sy,tx,ty; vector<int> spx,spy; rep(i,h)rep(j,w){ if(vec[i][j]=='s') sx=i,sy=j; if(vec[i][j]=='g') tx=i,ty=j; if(vec[i][j]=='*') spx.pb(i), spy.pb(j); } vector<vector<long>> d(h, vector<long>(w, INF)); // g?????§??????????????¢ { queue<int> xs,ys; d[tx][ty] = 0; xs.push(tx); ys.push(ty); while(!xs.empty()){ int x = xs.front(); xs.pop(); int y = ys.front(); ys.pop(); const int dx[] = {0,0,-1,1}, dy[] = {-1,1,0,0}; rep(i,4){ int nx = x+dx[i]; int ny = y+dy[i]; if(vec[nx][ny]!='#' && vec[nx][ny]!='*' && d[nx][ny]>d[x][y]+1){ d[nx][ny] = d[x][y]+1; xs.push(nx); ys.push(ny); } } } } vector<vector<long>> ds(h,vector<long>(w,INF)); // ????????????spring?????§??????????????¢ { queue<int> xs,ys; rep(i,spx.size()){ xs.push(spx[i]); ys.push(spy[i]); ds[spx[i]][spy[i]] = 0; } while(!xs.empty()){ int x = xs.front(); xs.pop(); int y = ys.front(); ys.pop(); const int dx[] = {0,0,-1,1}, dy[] = {-1,1,0,0}; rep(i,4){ int nx = x+dx[i]; int ny = y+dy[i]; if(vec[nx][ny]!='#' && vec[nx][ny]!='*' && ds[nx][ny]>ds[x][y]+1){ ds[nx][ny] = ds[x][y]+1; xs.push(nx); ys.push(ny); } } } } double p = INF; // ????????§?£???°??????????????¨?????????????§???????????????? { int cnt=1; rep(i,h)rep(j,w)if(vec[i][j]=='.') cnt++; double l = 0, r = 500.0*500*500*500; rep(_,1000){ double m = (l+r)/2.0; double accm = min<double>(d[sx][sy], ds[sx][sy]+m); rep(i,h) rep(j,w)if(vec[i][j]=='.'){ accm += min<double>(d[i][j], ds[i][j]+m); } p = accm/cnt; if(p<m) r = m; else l = m; } p = (r+l)/2.0; } printf("%.10llf\n", min<double>(d[sx][sy], ds[sx][sy]+p)); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; #define FOR(i,k,n) for(int i = (k); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define ALL(a) a.begin(), a.end() #define MS(m,v) memset(m,v,sizeof(m)) #define D10 fixed<<setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; const ll INF = pow(500,4); const int MOD = 1000000007; const double EPS = 1e-10; template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); } int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 }; bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); } int place(int x, int y, int w) { return w*x + y; } /*--------------------template--------------------*/ ll dg[555][555], ds[555][555]; int main() { REP(i, 555)REP(j, 555) dg[i][j] = ds[i][j] = INF; int w, h; cin >> w >> h; vs fld(h); REP(i, h) cin >> fld[i]; pii s, g; vector<pii> sp, fl; REP(i, h)REP(j, w) { if (fld[i][j] == 's') { s = pii(i, j); fld[i][j] = '.'; } if (fld[i][j] == 'g') { g = pii(i, j); } if (fld[i][j] == '.') fl.push_back(pii(i, j)); if (fld[i][j] == '*') { sp.push_back(pii(i, j)); ds[i][j] = 0; } } queue<pii> que; que.push(g); dg[g.first][g.second] = 0; while (que.size()) { int tx = que.front().first; int ty = que.front().second; que.pop(); REP(i, 4) { int nx = tx + dx[i]; int ny = ty + dy[i]; if (fld[nx][ny] == '.'&&dg[nx][ny] == INF) { dg[nx][ny] = dg[tx][ty] + 1; que.push(pii(nx, ny)); } } } while (que.size()) que.pop(); REP(i, sp.size()) que.push(sp[i]); while (que.size()) { int tx = que.front().first; int ty = que.front().second; que.pop(); REP(i, 4) { int nx = tx + dx[i]; int ny = ty + dy[i]; if (fld[nx][ny] == '.'&&ds[nx][ny] == INF) { ds[nx][ny] = ds[tx][ty] + 1; que.push(pii(nx, ny)); } } } ld lb = 0, ub = INF; REP(cnt,200) { ld mid = (lb + ub) / 2; ld sum = 0; REP(i, fl.size()) { int x = fl[i].first, y = fl[i].second; sum += min((ld)dg[x][y], ds[x][y] + mid); } sum /= fl.size(); if (mid > sum) ub = mid; else lb = mid; } ld sum = 0; REP(i, fl.size()) sum += min((ld)dg[fl[i].first][fl[i].second], ds[fl[i].first][fl[i].second] + lb); cout << D10 << min((ld)dg[s.first][s.second], ds[s.first][s.second] + lb) << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define all(v) (v).begin(),(v).end() #define fi first #define se second typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} #define double long double const int INF=1001001001; const int INFLL=1001001001001001001ll; const int mod=1000000007; inline void add(int &a,int b){ a+=b; if(a>=mod)a-=mod; } int dy[4]={-1,0,1,0}; int dx[4]={0,-1,0,1}; int H,W; char fld[555][555]; double E[555][555]; bool check(double e){ queue<pint>que; fill_n(*E,555*555,1e14); rep(i,H)rep(j,W)if(fld[i][j]=='g'){ E[i][j]=0; que.push({i,j}); } bool flag=false; while(true){ if(que.size()==0){ if(flag)break; rep(i,H)rep(j,W)if(fld[i][j]=='*'){ E[i][j]=e; que.push({i,j}); } flag=true; continue; } int y,x; tie(y,x)=que.front(); que.pop(); if(E[y][x]+1>=e&&!flag){ rep(i,H)rep(j,W)if(fld[i][j]=='*'){ E[i][j]=e; que.push({i,j}); } flag=true; } rep(d,4){ int ny=y+dy[d],nx=x+dx[d]; if(fld[ny][nx]=='#'||fld[ny][nx]=='*')continue; if(E[ny][nx]!=1e14)continue; E[ny][nx]=E[y][x]+1; que.push({ny,nx}); } } double sum=0; int cnt=0; rep(i,H)rep(j,W){ if(fld[i][j]=='#'||fld[i][j]=='*'||fld[i][j]=='g')continue; cnt++;sum+=E[i][j]; } return sum<=e*cnt; } signed main(){ cin>>W>>H; rep(i,H)cin>>fld[i]; vector<vint>dist(H,vint(W,INT_MAX)); queue<pint>que; rep(i,H)rep(j,W)if(fld[i][j]=='s'){ dist[i][j]=0; que.push({i,j}); } bool flag=false; while(que.size()){ int y,x; tie(y,x)=que.front(); que.pop(); rep(d,4){ int ny=y+dy[d],nx=x+dx[d]; if(fld[ny][nx]=='#'||dist[ny][nx]!=INT_MAX)continue; if(fld[ny][nx]=='*')flag=true; dist[ny][nx]=dist[y][x]+1; que.push({ny,nx}); } } if(!flag){ rep(i,H)rep(j,W)if(fld[i][j]=='g'){ printf("%.20Lf\n",(double)dist[i][j]); return 0; } } double lb=0,ub=1e12; rep(i,70){ double mid=(ub+lb)/2; if(check(mid))ub=mid; else lb=mid; } rep(i,H)rep(j,W){ if(fld[i][j]=='s'){ printf("%.20Lf\n",E[i][j]); } } return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
from collections import deque w, h = map(int, input().split()) mp = [input() for _ in range(h)] springs = [] tile_cnt = 0 for y in range(h): for x in range(w): if mp[y][x] == "*": springs.append((x, y)) if mp[y][x] == "g": gx, gy = x, y if mp[y][x] == "s": sx, sy = x, y tile_cnt += 1 if mp[y][x] == ".": tile_cnt += 1 vec = ((1, 0), (0, -1), (-1, 0), (0, 1)) INF = 10 ** 10 g_dist = [[INF] * w for _ in range(h)] que = deque() que.append((0, gx, gy)) g_dist[gy][gx] = 0 while que: score, x, y = que.popleft() for dx, dy in vec: nx, ny = x + dx, y + dy if mp[ny][nx] in (".", "s"): if g_dist[ny][nx] == INF: g_dist[ny][nx] = score + 1 que.append((score + 1, nx, ny)) s_dist = [[INF] * w for _ in range(h)] que = deque() for x, y in springs: s_dist[y][x] = 0 que.append((0, x, y)) while que: score, x, y = que.popleft() for dx, dy in vec: nx, ny = x + dx, y + dy if mp[ny][nx] in (".", "s"): if s_dist[ny][nx] == INF: s_dist[ny][nx] = score + 1 que.append((score + 1, nx, ny)) sorted_tiles = sorted([(g_dist[y][x] - s_dist[y][x] if g_dist[y][x] != INF else INF, x, y) for y in range(h) for x in range(w) if mp[y][x] in (".", "s")]) acc_g = 0 acc_s = 0 acc_t = 0 acc_g_dic = {} acc_s_dic = {} acc_t_dic = {} keys = set() for key, x, y in sorted_tiles: acc_g += g_dist[y][x] acc_s += s_dist[y][x] acc_t += 1 acc_g_dic[key] = acc_g acc_s_dic[key] = acc_s acc_t_dic[key] = acc_t keys.add(key) keys = sorted(list(keys)) length = len(keys) for i in range(length - 1): key = keys[i] next_key = keys[i + 1] E = (acc_g_dic[key] + acc_s - acc_s_dic[key]) / acc_t_dic[key] if key <= E < next_key: print(min(g_dist[sy][sx], s_dist[sy][sx] + E)) break else: print(g_dist[sy][sx])
PYTHON3
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <iostream> #include <cstdio> #include <vector> #include <queue> #include <cmath> #include <algorithm> #include <functional> using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) #define rep1(i,n) for(int i=1;i<=(n);++i) #define all(c) (c).begin(),(c).end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl; typedef long long ll; typedef pair<int,int> P; typedef pair<ll,P> PP; int w,h,sx,sy,gx,gy,num; string s[500]; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; ll dg[500][500],db[500][500],inf=1e18; void gbfs(int gx,int gy){ rep(i,h) rep(j,w) dg[i][j]=inf; dg[gx][gy]=0; queue<PP> que; que.push(PP(dg[gx][gy],P(gx,gy))); while(!que.empty()){ PP pp=que.front(); que.pop(); P p=pp.second; rep(i,4){ int nx=p.fs+dx[i],ny=p.sc+dy[i]; if(s[nx][ny]!='#'&&s[nx][ny]!='*'&&dg[nx][ny]==inf){ dg[nx][ny]=pp.fs+1; que.push(PP(dg[nx][ny],P(nx,ny))); } } } } void bbfs(){ queue<PP> que; rep(i,h) rep(j,w){ if(s[i][j]!='*') db[i][j]=inf; else que.push(PP(0,P(i,j))); } while(!que.empty()){ PP pp=que.front(); que.pop(); P p=pp.second; rep(i,4){ int nx=p.fs+dx[i],ny=p.sc+dy[i]; if(s[nx][ny]!='#'&&s[nx][ny]!='*'&&db[nx][ny]==inf){ db[nx][ny]=pp.fs+1; que.push(PP(db[nx][ny],P(nx,ny))); } } } } bool check(long double x){ long double sum=0; rep(i,h) rep(j,w) if(s[i][j]!='#'&&s[i][j]!='*') sum+=min(x+db[i][j],(long double)dg[i][j]); return sum/num<x; } int main(){ cin>>w>>h; rep(i,h) cin>>s[i]; rep(i,h) rep(j,w){ if(s[i][j]=='s') sx=i,sy=j,num++; if(s[i][j]=='g') gx=i,gy=j; if(s[i][j]=='.') num++; } gbfs(gx,gy); bbfs(); bool flag=false; rep(i,h) rep(j,w){ if(s[i][j]!='#'&&dg[i][j]==inf&&db[i][j]==inf) flag=true; } if(flag){ printf("%lld\n",dg[sx][sy]); return 0; } long double ub=500.0*500*500*500,lb=0; int cnt=0; while(cnt<100){ // cout<<ub<<" "<<lb<<endl; cnt++; long double mid=(ub+lb)/2; if(check(mid)) ub=mid; else lb=mid; } printf("%.12f\n",(double)min(ub+db[sx][sy],(long double)dg[sx][sy])); }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))<EPS) using namespace std; typedef long double ld; const int MAX = 501,IINF = INT_MAX; const ld LDINF = 1e100; int H,W,sx,sy,gx,gy; ld mincost[MAX][MAX][2]; // mincost[][][0] => from start, [1] = > from star char c[MAX][MAX]; bool ban[MAX][MAX]; vector<int> star,plane; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; inline bool isValid(int x,int y) { return 0 <= x && x < W && 0 <= y && y < H; } inline void bfs(vector<int> sp,vector<int> Forbidden,int type){ rep(i,H)rep(j,W) mincost[i][j][type] = LDINF, ban[i][j] = false; queue<int> que; rep(i,(int)sp.size()) que.push(sp[i]), mincost[sp[i]/W][sp[i]%W][type] = 0; rep(i,(int)Forbidden.size()) ban[Forbidden[i]/W][Forbidden[i]%W] = true; while(!que.empty()){ int cur = que.front(); que.pop(); rep(i,4){ int nx = cur % W + dx[i], ny = cur / W + dy[i]; if( c[ny][nx] == '#' ) continue; if( ban[ny][nx] ) continue; if( mincost[ny][nx][type] == LDINF ) { mincost[ny][nx][type] = mincost[cur/W][cur%W][type] + 1; que.push(nx+ny*W); } } } } bool check(ld E){ ld T = 0; rep(i,(int)plane.size()){ int x = plane[i] % W, y = plane[i] / W; T += min(mincost[y][x][0],mincost[y][x][1]+E); } ld len = plane.size(); return len * E > T; } int main(){ cin >> W >> H; rep(i,H)rep(j,W){ cin >> c[i][j]; if( c[i][j] == 's' ) sx = j, sy = i, c[i][j] = '.'; if( c[i][j] == 'g' ) gx = j, gy = i; if( c[i][j] == '*' ) star.push_back(j+i*W); if( c[i][j] == '.' ) plane.push_back(j+i*W); } vector<int> sp,forbidden; sp.push_back(gx+gy*W); forbidden = star; forbidden.push_back(gx+gy*W); bfs(sp,forbidden,0); sp = star; forbidden.push_back(gx+gy*W); //forbidden.clear(); bfs(sp,forbidden,1); ld L = 0, R = 1e10, M = 0; rep(i,80){ M = ( L + R ) * (ld)0.5; if( check(M) ) R = M; else L = M; } cout << setiosflags(ios::fixed) << setprecision(20) << min((ld)mincost[sy][sx][0],(ld)mincost[sy][sx][1]+L) << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Main { public static final int[] vs = {1, 0, -1, 0}; public static final double EPS = 1e-10; public static void bfs(final int H, final int W, final int sx, final int sy, boolean[][] is_wall, boolean[][] is_floor, long[][] expected){ LinkedList<Long> expected_queue = new LinkedList<Long>(); LinkedList<Integer> x_queue = new LinkedList<Integer>(); LinkedList<Integer> y_queue = new LinkedList<Integer>(); expected_queue.add(expected[sy][sx]); x_queue.add(sx); y_queue.add(sy); while(!x_queue.isEmpty()){ final int x = x_queue.poll(); final int y = y_queue.poll(); for(int v = 0; v < vs.length; v++){ final int nx = x + vs[v]; final int ny = y + vs[(v + 1) % vs.length]; if(nx < 0 || nx >= W || ny < 0 || ny >= H){ continue; }else if(is_wall[ny][nx]){ continue; } ///System.out.println(nx + " " + ny + " " + expected[y][x] + " " + expected[ny][nx]); if(is_floor[ny][nx] && expected[ny][nx] > expected[y][x] + 1){ expected[ny][nx] = expected[y][x] + 1; y_queue.add(ny); x_queue.add(nx); } } } } public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); { final int W = sc.nextInt(); final int H = sc.nextInt(); int sx = -1, sy = -1, gx = -1, gy = -1; boolean[][] is_floor = new boolean[H][W]; boolean[][] is_wall = new boolean[H][W]; LinkedList<Integer> spring_xs = new LinkedList<Integer>(); LinkedList<Integer> spring_ys = new LinkedList<Integer>(); for(int i = 0; i < H; i++){ final char[] line = sc.next().toCharArray(); for(int j = 0; j < W; j++){ if(line[j] != '#' && line[j] != '*'){ is_floor[i][j] = true; }else if(line[j] == '*'){ spring_ys.add(i); spring_xs.add(j); }else if(line[j] == '#'){ is_wall[i][j] = true; } if(line[j] == 's'){ sy = i; sx = j; }else if(line[j] == 'g'){ gy = i; gx = j; } } } final long INF = Long.MAX_VALUE / 2 - 1; long[][] from_goal = new long[H][W]; long[][] from_spring = new long[H][W]; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ from_goal[i][j] = from_spring[i][j] = INF; } } from_goal[gy][gx] = 0; for(Iterator<Integer> x_itr = spring_xs.iterator(), y_itr = spring_ys.iterator(); x_itr.hasNext() && y_itr.hasNext(); ){ final int x = x_itr.next(); final int y = y_itr.next(); from_spring[y][x] = 0; } bfs(H, W, gx, gy, is_wall, is_floor, from_goal); for(Iterator<Integer> x_itr = spring_xs.iterator(), y_itr = spring_ys.iterator(); x_itr.hasNext() && y_itr.hasNext(); ){ final int x = x_itr.next(); final int y = y_itr.next(); bfs(H, W, x, y, is_wall, is_floor, from_spring); } /* for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ System.out.print(from_goal[i][j] >= INF ? "x " : from_goal[i][j] + " "); } System.out.println(); } */ double upper = INF; double lower = 0; while(upper - lower > EPS){ final double middle = (upper + lower) / 2; double expected_sum = 0; int count = 0; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(!is_floor[i][j]){ continue; }else if(i == gy && j == gx){ continue; } count++; expected_sum += Math.min(from_goal[i][j], from_spring[i][j] + middle); } } final double expected_value = expected_sum / count; if(expected_value < middle){ upper = middle; }else if(expected_value > middle){ lower = middle; }else{ upper = middle; lower = middle; } //System.out.println(upper + " " + middle + " " + lower + " " + expected_value + " " + count); } System.out.printf("%.10f\n", Math.min(from_goal[sy][sx], from_spring[sy][sx] + lower)); } } }
JAVA
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <cmath> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #define double long double #define INF 1.0e12 using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef pair<double, P> Pd; int main() { int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1}; int w, h; cin>>w>>h; double x1=0, x2=1.0e10; string s[500]; int sx, sy, gx, gy; for(int i=0; i<h; i++){ cin>>s[i]; for(int j=0; j<w; j++){ if(s[i][j]=='s'){ sx=i, sy=j; }else if(s[i][j]=='g'){ gx=i, gy=j; } } } for(int l=0; l<100; l++){ double xmid=(x1+x2)/2; double d[500][500]; priority_queue<Pd, vector<Pd>, greater<Pd> > que; que.push(Pd(0, P(gx, gy))); for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ if(s[i][j]=='g'){ d[i][j]=0; }else if(s[i][j]=='*'){ d[i][j]=xmid; que.push(Pd(xmid, P(i, j))); }else{ d[i][j]=INF; } } } while(!que.empty()){ Pd p=que.top(); que.pop(); int x=p.second.first, y=p.second.second; if(p.first>d[x][y]) continue; for(int i=0; i<4; i++){ if(!(0<=x+dx[i] && x+dx[i]<h && 0<=y+dy[i] && y+dy[i]<w)) continue; if(s[x+dx[i]][y+dy[i]]=='#' || s[x+dx[i]][y+dy[i]]=='*') continue; if(d[x+dx[i]][y+dy[i]]>d[x][y]+1.0){ d[x+dx[i]][y+dy[i]]=d[x][y]+1.0; que.push(Pd(d[x+dx[i]][y+dy[i]], P(x+dx[i], y+dy[i]))); } } } if(l==99){ printf("%.10Lf\n", d[sx][sy]); return 0; } double sum=0; int ct=0; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ if(s[i][j]=='.' || s[i][j]=='s'){ ct++; sum+=d[i][j]; } } } if(sum/(double)ct<xmid){ x2=xmid; }else{ x1=xmid; } } return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> #define syosu(x) fixed<<setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef pair<double,double> pdd; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int,P> pip; typedef vector<pip> vip; const int inf=1<<29; const ll INF=1ll<<60; const double pi=acos(-1); const double eps=1e-8; const ll mod=1e9+7; const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1}; int h,w,gx,gy; vs a; vvl b,c; double f(double x){ double res=0; for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(a[i][j]=='.'||a[i][j]=='s'){ res+=min(b[i][j]-x,(double)c[i][j]); } return res; } int main(){ cin>>w>>h; a=vs(h,string(w,'A')); b=c=vvl(h,vl(w,INF)); queue<P> q; for(int i=0;i<h;i++) for(int j=0;j<w;j++){ cin>>a[i][j]; if(a[i][j]=='g'){ gx=i,gy=j; b[i][j]=0; } if(a[i][j]=='*'){ q.push({i,j}); c[i][j]=0; } } while(!q.empty()){ P p=q.front(); q.pop(); int x=p.first,y=p.second; for(int i=0;i<4;i++){ int X=x+dx[i],Y=y+dy[i]; if(a[X][Y]!='#'&&c[X][Y]==INF){ c[X][Y]=c[x][y]+1; q.push({X,Y}); } } } q.push({gx,gy}); while(!q.empty()){ P p=q.front(); q.pop(); int x=p.first,y=p.second; for(int i=0;i<4;i++){ int X=x+dx[i],Y=y+dy[i]; if((a[X][Y]=='.'||a[X][Y]=='s')&&b[X][Y]==INF){ b[X][Y]=b[x][y]+1; q.push({X,Y}); } } } double l=0,r=INF; for(int i=0;i<200;i++){ double m=(l+r)/2; if(f(m)>=0) l=m; else r=m; } for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(a[i][j]=='s'){ cout<<syosu(11)<<min((double)b[i][j],c[i][j]+r)<<endl; } }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int W, H; int sx, sy, gx, gy; vector<string> vs; vector<vector<long double> > gdist; vector<vector<long double> > sdist; void make() { gdist[gx][gy] = 0; queue<P> que; que.push(P(gx,gy)); while (que.size()) { P p = que.front(); que.pop(); int x = p.first, y = p.second; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 <= nx && nx < H && 0 <= ny && ny < W && (vs[nx][ny] == '.' || vs[nx][ny] == 's') && gdist[nx][ny] > gdist[x][y] + 1) { gdist[nx][ny] = gdist[x][y] + 1; que.push(P(nx,ny)); } } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (vs[i][j] == '*') { que.push(P(i,j)); sdist[i][j] = 0; } } } while (que.size()) { P p = que.front(); que.pop(); int x = p.first, y = p.second; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 <= nx && nx < H && 0 <= ny && ny < W && (vs[nx][ny] == '.' || vs[nx][ny] == 's') && sdist[nx][ny] > sdist[x][y] + 1) { sdist[nx][ny] = sdist[x][y] + 1; que.push(P(nx,ny)); } } } } long double check(long double e) { long double ret = 0.0; int cnt = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (vs[i][j] == '.' || vs[i][j] == 's') { long double dist = 1e+50; dist = min(dist, gdist[i][j]); dist = min(dist, sdist[i][j] + e); ret += dist; cnt ++; } } } return ret / (long double)cnt; } int main() { cin >> W >> H; vs.resize(H); gdist.resize(H); sdist.resize(H); for (auto &i : gdist) i.resize(W, 1e50); for (auto &i : sdist) i.resize(W, 1e50); for (auto &i : vs) cin >> i; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (vs[i][j] == 'g') gx = i, gy = j; if (vs[i][j] == 's') sx = i, sy = j; } } make(); long double l = 0.0, r = 1e+18; for (int c = 0; c < 100; c++) { long double m = (l + r)/2.0; // printf("%.10Lf\n", m); if (check(m) > m) { l = m; } else { r = m; } } printf("%.10Lf\n", min(l + sdist[sx][sy], gdist[sx][sy])); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <iostream> #include <stdio.h> #include <vector> #include <utility> #include <queue> #define inf 1e30 using namespace std; typedef pair<int, int> pos; typedef pair<long double, pos> P; int W, H; char map[505][505]; int sx, sy, gx, gy; long double dist[505][505]; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; void dijkstra(long double m) { for(int x = 1; x <= W; x++){ for(int y = 1; y <= H; y++){ dist[x][y] = inf; } } priority_queue< P, vector<P>, greater<P> > Q; dist[gx][gy] = 0, Q.push( make_pair(0, make_pair(gx, gy)) ); for(int x = 1; x <= W; x++){ for(int y = 1; y <= H; y++){ if(map[x][y] == '*') dist[x][y] = m, Q.push(make_pair(m, make_pair(x, y))); } } int x, y; long double d; while(Q.size()){ d = Q.top().first; x = Q.top().second.first; y = Q.top().second.second; Q.pop(); if(dist[x][y] < d) continue; for(int i = 0; i < 4; i++){ int nx = x + dx[i], ny = y + dy[i]; if(nx <= 0 || nx > W || ny <= 0 || ny > H) continue; if(map[nx][ny] != '.') continue; if(dist[nx][ny] > d + 1){ dist[nx][ny] = d + 1; Q.push( make_pair(dist[nx][ny], make_pair(nx, ny)) ); } } } } long double calc(long double m) { dijkstra(m); long double ret = 0; int cnt = 0; for(int x = 1; x <= W; x++){ for(int y = 1; y <= H; y++){ if(map[x][y] == '.' && (x != gx || y != gy)){ ret += dist[x][y]; cnt++; } } } ret /= cnt; return ret; } int main(void) { cin >> W >> H; for(int y = 1; y <= H; y++){ for(int x = 1; x <= W; x++){ cin >> map[x][y]; if(map[x][y] == 's'){ map[x][y] = '.'; sx = x, sy = y; } if(map[x][y] == 'g'){ map[x][y] = '.'; gx = x, gy = y; } } } long double ub = 1e18, lb = 0.0, mid; for(int i = 0; i < 100; i++){ mid = (ub + lb) * 0.5; long double val = calc(mid); if(val < mid) ub = mid; else if(val > mid) lb = mid; } mid = (ub + lb) * 0.5; calc(mid); long double ret = 0; int cnt = 0; for(int x = 1; x <= W; x++){ for(int y = 1; y <= H; y++){ if(map[x][y] == '.' && (x != gx || y != gy)){ ret += dist[x][y]; cnt++; } } } ret /= cnt; printf("%.11Lf\n", dist[sx][sy]); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <cstdio> #include <queue> #include <vector> #include <algorithm> using namespace std; typedef long double real; const real inf = 1e20; struct point { int x, y; }; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; vector<real> dist; // ret1: true if arg e < calculated e // ret2: expected s-g value if e is correct pair<bool, real> is_lower(const real e, const vector<vector<char> >& f, const point& s, const point& g, const vector<point>& spring) { const int w = f[0].size(), h = f.size(); dist.resize(w * h); fill(dist.begin(), dist.end(), inf); queue<pair<point, real> > q; q.push(make_pair(g, 0)); while(!q.empty()) { const auto vd = q.front(); const auto v = vd.first; const auto d = vd.second; q.pop(); const int idx = v.x * w + v.y; if(dist[idx] < inf) continue; dist[idx] = d; for(int dir = 0; dir < 4; ++dir) { const int nx = v.x + dx[dir], ny = v.y + dy[dir]; const point vvv = {nx, ny}; if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue; char c = f[nx][ny]; if(c == '#' || c == '*') continue; q.push(make_pair(vvv, d + 1)); } } for(auto sp : spring) q.push(make_pair(sp, e)); while(!q.empty()) { const auto vd = q.front(); const auto v = vd.first; const auto d = vd.second; q.pop(); const int idx = v.x * w + v.y; if(dist[idx] <= d) continue; dist[idx] = d; for(int dir = 0; dir < 4; ++dir) { const int nx = v.x + dx[dir], ny = v.y + dy[dir]; const point vvv = {nx, ny}; if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue; char c = f[nx][ny]; if(c == '#' || c == '*') continue; q.push(make_pair(vvv, d + 1)); } } real total = 0; int count = 0; for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { char c = f[i][j]; const int idx = i * w + j; if(c == '#' || c == 'g' || c == '*') continue; total += dist[idx]; count += 1; } } bool ok = e < total / count; real ans = dist[s.x * w + s.y]; return make_pair(ok, ans); } int main() { int w, h; vector<vector<char> > f; scanf("%d%d", &w, &h); f.resize(h); for(int i = 0; i < h; ++i) { char buf[1024]; scanf("%s", buf); for(int j = 0; buf[j] != '\0'; ++j) f[i].push_back(buf[j]); } vector<point> spring; point s, g; for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { char c = f[i][j]; if(c == 's') s = {i, j}; else if(c == 'g') g = {i, j}; else if(c == '*') spring.push_back({i, j}); } } real lb = 0, ub = inf / 2; real ans = 0; const int max_step = 200; for(int step = 0; step < max_step; ++step) { real mid = (lb + ub) / 2; auto ret = is_lower(mid, f, s, g, spring); if(ret.first) lb = mid; else ub = mid; ans = ret.second; } printf("%.20Lf\n", ans); return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<int, int>; const double eps = 1e-8; const ll MOD = 1000000007; const int INF = INT_MAX / 2; const ll LINF = LLONG_MAX / 2; template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) { if(a < b) {a = b; return true;} return false; } template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) { if(a > b) {a = b; return true;} return false; } template<typename T1, typename T2> ostream& operator<<(ostream &os, const pair<T1, T2> p) { os << p.first << ":" << p.second; return os; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for(int i=0;i<((int)(v.size()));++i) { if(i) os << " "; os << v[i]; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int w, h; cin >> w >> h; vector<vector<char>> v(h, vector<char>(w)); vector<P> bane; P st, gl; for(int i=0;i<(h);++i) { for(int j=0;j<(w);++j) { cin >> v[i][j]; if(v[i][j] == 's') { v[i][j] = '.'; st = {i, j}; } else if(v[i][j] == 'g') { v[i][j] = '.'; gl = {i, j}; } else if(v[i][j] == '*') { bane.push_back({i, j}); } } } vvi dg(h, vi(w, -1)); dg[gl.first][gl.second] = 0; queue<P> que; que.push(gl); vi dr = {-1, 1, 0, 0}; vi dc = {0, 0, -1, 1}; while(!que.empty()) { P now = que.front(); que.pop(); int d = dg[now.first][now.second]; for(int i=0;i<(4);++i) { int nr = now.first + dr[i], nc = now.second + dc[i]; if(!(0 <= nr && nr < h && 0 <= nc && nc < w)) continue; if(v[nr][nc] != '.') continue; if(dg[nr][nc] == -1) { dg[nr][nc] = d + 1; que.push({nr, nc}); } } } vvi dbane(h, vi(w, -1)); for(auto &e: bane) dbane[e.first][e.second] = 0, que.push(e); while(!que.empty()) { P now = que.front(); que.pop(); int d = dbane[now.first][now.second]; for(int i=0;i<(4);++i) { int nr = now.first + dr[i], nc = now.second + dc[i]; if(!(0 <= nr && nr < h && 0 <= nc && nc < w)) continue; if(v[nr][nc] != '.') continue; if(dbane[nr][nc] == -1) { dbane[nr][nc] = d + 1; que.push({nr, nc}); } } } double l = 0.0, r = LINF; for(int _=0;_<(500);++_) { double mid = (l + r) / 2; double su = 0; int cnt = 0; for(int i=0;i<(h);++i) { for(int j=0;j<(w);++j) { if(v[i][j] != '.' || gl == make_pair(i, j)) continue; cnt++; double tmp = 2e18; if(dg[i][j] != -1 && tmp > dg[i][j]) tmp = dg[i][j]; if(dbane[i][j] != -1 && tmp > dbane[i][j] + mid) tmp = dbane[i][j] + mid; su += tmp; } } if(cnt * mid < su) { l = mid; } else { r = mid; } } double ans = 2e18; if(dg[st.first][st.second] != -1 && ans > dg[st.first][st.second]) ans = dg[st.first][st.second]; if(dbane[st.first][st.second] != -1 && ans > dbane[st.first][st.second] + l) ans = dbane[st.first][st.second] + l; cout << ans << endl; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))<EPS) using namespace std; typedef long double ld; const int MAX = 501,IINF = INT_MAX; const ld LDINF = 1e100; int H,W,sx,sy,gx,gy; ld mincost[MAX][MAX][2]; // mincost[][][0] => from start, [1] = > from star char c[MAX][MAX]; bool ban[MAX][MAX]; vector<int> star,plane; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; inline bool isValid(int x,int y) { return 0 <= x && x < W && 0 <= y && y < H; } inline void bfs(vector<int> sp,vector<int> Forbidden,int type){ rep(i,H)rep(j,W) mincost[i][j][type] = LDINF, ban[i][j] = false; queue<int> que; rep(i,(int)sp.size()) que.push(sp[i]), mincost[sp[i]/W][sp[i]%W][type] = 0; rep(i,(int)Forbidden.size()) ban[Forbidden[i]/W][Forbidden[i]%W] = true; while(!que.empty()){ int cur = que.front(); que.pop(); rep(i,4){ int nx = cur % W + dx[i], ny = cur / W + dy[i]; if( c[ny][nx] == '#' ) continue; if( ban[ny][nx] ) continue; if( mincost[ny][nx][type] == LDINF ) { mincost[ny][nx][type] = mincost[cur/W][cur%W][type] + 1; que.push(nx+ny*W); } } } } bool check(ld E){ ld T = 0; rep(i,(int)plane.size()){ int x = plane[i] % W, y = plane[i] / W; T += min(mincost[y][x][0],mincost[y][x][1]+E); } ld len = plane.size(); return len * E > T; } int main(){ cin >> W >> H; rep(i,H)rep(j,W){ cin >> c[i][j]; if( c[i][j] == 's' ) sx = j, sy = i, c[i][j] = '.'; if( c[i][j] == 'g' ) gx = j, gy = i; if( c[i][j] == '*' ) star.push_back(j+i*W); if( c[i][j] == '.' ) plane.push_back(j+i*W); } vector<int> sp,forbidden; sp.push_back(gx+gy*W); forbidden = star; forbidden.push_back(gx+gy*W); bfs(sp,forbidden,0); sp = star; forbidden.push_back(gx+gy*W); //forbidden.clear(); bfs(sp,forbidden,1); ld L = 0, R = 1e20, M = 0; rep(i,100){ M = ( L + R ) * (ld)0.5; if( check(M) ) R = M; else L = M; } cout << setiosflags(ios::fixed) << setprecision(20) << min((ld)mincost[sy][sx][0],(ld)mincost[sy][sx][1]+L) << endl; return 0; }
CPP
p01453 Spring Tiles
One morning when you woke up, it was in a springy labyrinth. I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth. When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth. This labyrinth is shaped like a rectangle with several types of tiles, for example: .. ## g. # * #. * #. # ...... # * s # *. * # ".":floor. You can walk around freely on this. "#":wall. You can't get inside the wall. "S": Your first position. Below this tile is the floor. "G": Stairs. If you ride on this tile, you have escaped from the labyrinth. "*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal. The type of tile is one of these five. You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname. Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring. Input W H c11 c12 ... c1w c21 c22 ... c2w ::: ch1 ch2 ... ccw On the first line of input, the integer W (3 ≤ W ≤ 500) and the integer H (3 ≤ H ≤ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth. The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall. In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing. Output Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9. Examples Input 8 6 ######## #..##g.# #*#.*#.# #......# #*s#*.*# ######## Output 5.857142857143 Input 8 6 ..##g.# *#.*#.# ......# *s#*.*# Output 5.857142857143 Input 21 11 *#*.*.*.*.*.*.*.*#*# *#...............#*# *#.#.#.#.#.#.#.#.#*# .#.#.#.#.#.#.#.#.## ...................# .#.#.#.#.#.#.#.#.## s#.#.#.#.#.#.#.#.#g# ...................# ...................# Output 20.000000000000 Input 9 8 ...#*.*# .*.#...# ...#*.*# .s....g# .*#.*..# Output 5.000000000000
7
0
#include <iostream> #include <queue> using namespace std; long long H, W, sx, sy, gx, gy, dist1[509][509], dist2[509][509]; char c[509][509]; int main() { cin >> W >> H; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { cin >> c[i][j]; dist1[i][j] = (1LL << 60); dist2[i][j] = (1LL << 60); if (c[i][j] == 's') { sx = i; sy = j; } if (c[i][j] == 'g') { gx = i; gy = j; } } } int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; queue<pair<int, int>>Q; dist1[gx][gy] = 0; Q.push(make_pair(gx, gy)); while (!Q.empty()) { pair<int, int>a1 = Q.front(); Q.pop(); int cx = a1.first, cy = a1.second; for (int i = 0; i < 4; i++) { int px = cx + dx[i], py = cy + dy[i]; if (c[px][py] == '#' || c[px][py] == '*') continue; if (dist1[px][py] > dist1[cx][cy] + 1) { dist1[px][py] = dist1[cx][cy] + 1; Q.push(make_pair(px, py)); } } } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (c[i][j] == '*') { dist2[i][j] = 0; Q.push(make_pair(i, j)); } } } if (Q.empty()) { cout << dist1[sx][sy] << endl; return 0; } while (!Q.empty()) { pair<int, int>a1 = Q.front(); Q.pop(); int cx = a1.first, cy = a1.second; for (int i = 0; i < 4; i++) { int px = cx + dx[i], py = cy + dy[i]; if (c[px][py] == '#') continue; if (dist2[px][py] > dist2[cx][cy] + 1) { dist2[px][py] = dist2[cx][cy] + 1; Q.push(make_pair(px, py)); } } } long double L = 0.0L, R = 1e12, M; for (int i = 0; i < 100; i++) { M = (L + R) / 2; int cnt = 0; long double sum = 0; for (int j = 1; j <= H; j++) { for (int k = 1; k <= W; k++) { if (c[j][k] == '#' || c[j][k] == '*' || c[j][k] == 'g') continue; cnt++; sum += min(M + 1.0L*dist2[j][k], 1.0L*dist1[j][k]); } } if ((1.0L*sum / cnt) < M) { R = M; } else { L = M; } } printf("%.12Lf\n", min(M + 1.0L*dist2[sx][sy], 1.0L * dist1[sx][sy])); return 0; }
CPP
p01603 Sanpo
Sanpo Problem Statement The cats love walking. There are new discoveries as you walk down the road. The town where Sora lives consists of N squares and N-1 roads. The road just connects the two squares and does not branch or intersect. For the road, the time t to walk, the upper limit m of the number of discoveries that can be obtained, and the value v per discovery are specified. When walking along the road from one end to the other, one discovery of value v can be obtained in the mth pass, but no discovery can be obtained after the m + 1th pass. It seems that they are going for a walk in the town today. The walk in the sky starts at Square 1, takes several roads (maybe 0), and finally returns to Square 1. Also, when the sun goes down, I feel lonely, so I want to set the walking time to T or less. Your friend, your job, is to find the maximum sum of the values ​​of discoveries you can get on a walk route with a walk time of T or less. Constraints * 1 ≤ N ≤ 300 * 1 ≤ T ≤ 10 ^ 4 * 1 ≤ a_i, b_i ≤ N * 1 ≤ t_i ≤ 10 ^ 4 * 0 ≤ m_i ≤ 10 ^ 4 * 1 ≤ v_i ≤ 10 ^ 5 * You can go back and forth between the two squares. Input Input follows the following format. All given numbers are integers. N T a_1 b_1 t_1 m_1 v_1 .. .. a_ {N-1} b_ {N-1} t_ {N-1} m_ {N-1} v_ {N-1} The i + 1 line of the input shows the path of the passage time t_i, the upper limit of the number of discoveries obtained, m_i, and the value v_i per discovery, which connects the squares a_i and b_i. Output Output the maximum value of the total value of the findings obtained on one line. Examples Input 4 5 1 2 1 4 7 2 3 1 2 77 2 4 1 2 777 Output 1568 Input 2 10000 1 2 1 10 1 Output 10 Input 5 15 1 2 3 2 1 2 3 3 2 1 1 4 8 2 777 1 5 1 3 10 Output 32
7
0
#include <bits/stdc++.h> using namespace std; using ll = long long int; void chmax(ll &A, ll B) { A = max(A, B); } struct Edge { Edge() {} Edge(int to_, int tm_, int lim_, int cost_) : to(to_), tm(tm_), lim(lim_), cost(cost_) {} int to, tm, lim, cost; }; using Graph = vector< vector<Edge> >; ll N, T, dp[650][10010]; int euler[650], dir[650]; Edge euler_edge[650]; int idx = 0; void dfs(Graph &G, int cur, int par=-1) { euler[idx++] = cur; for(auto e : G[cur]) { int to = e.to; if(to == par) continue; euler_edge[idx] = e; dir[idx] = 0; dfs(G, to, cur); euler_edge[idx] = e; dir[idx] = 1; euler[idx++] = cur; } } int main() { cin >> N >> T; Graph G(N); for(int i=0; i<N-1; i++) { int a, b, t, m, v; cin >> a >> b >> t >> m >> v; a--; b--; G[a].push_back(Edge{b, t, m, v}); G[b].push_back(Edge{a, t, m, v}); } dfs(G, 0); /* for(int i=0; i<idx; i++) { printf("%d (%d), ", euler[i], euler_edge[i].cost); } cout << endl; */ vector<int> pre(N, -1); fill(dp[0], dp[650], -(1LL << 60)); // for(int i=0; i<idx; i++) dp[i][0] = 0; dp[0][0] = pre[0] = 0; for(int i=1; i<idx; i++) { int prev_idx = pre[ euler[i] ]; int tm = euler_edge[i].tm; int lim = euler_edge[i].lim; int cost = euler_edge[i].cost; for(int t=0; t<=T; t++) { if(prev_idx >= 0) chmax(dp[i][t], dp[prev_idx][t]); } if(dir[i] == 1) { // 帰り for(int t=0; t<=T; t++) { chmax(dp[i][t], dp[i-1][t]); } } else { // 行き if(lim % 2 == 1) { ll take_tm = (lim + 1) * tm, take_cost = lim * cost; for(int t=take_tm; t<=T; t++) { chmax(dp[i][t], dp[i-1][t - take_tm] + take_cost); } lim--; } if(lim == 0) { ll take_tm = 2 * tm; for(int t=take_tm; t<=T; t++) { chmax(dp[i][t], dp[i-1][t - take_tm]); } } else { lim -= 2; for(int m=2; lim>0; m*=2) { ll x = min(lim, m); lim -= x; ll take_tm = x * tm, take_cost = x * cost; for(int t=T; t>=take_tm; t--) { chmax(dp[i-1][t], dp[i-1][t - take_tm] + take_cost); } } ll take_tm = 2 * tm, take_cost = 2 * cost; for(int t=take_tm; t<=T; t++) { chmax(dp[i][t], dp[i-1][t - take_tm] + take_cost); } } } /* for(int t=0; t<=T; t++) { if(dp[i][t] < 0) cout << "-INF" << " "; else cout << dp[i][t] << " "; } cout << endl; */ pre[ euler[i] ] = i; } cout << *max_element(dp[idx-1], dp[idx-1] + T + 1) << endl; return 0; }
CPP
p01603 Sanpo
Sanpo Problem Statement The cats love walking. There are new discoveries as you walk down the road. The town where Sora lives consists of N squares and N-1 roads. The road just connects the two squares and does not branch or intersect. For the road, the time t to walk, the upper limit m of the number of discoveries that can be obtained, and the value v per discovery are specified. When walking along the road from one end to the other, one discovery of value v can be obtained in the mth pass, but no discovery can be obtained after the m + 1th pass. It seems that they are going for a walk in the town today. The walk in the sky starts at Square 1, takes several roads (maybe 0), and finally returns to Square 1. Also, when the sun goes down, I feel lonely, so I want to set the walking time to T or less. Your friend, your job, is to find the maximum sum of the values ​​of discoveries you can get on a walk route with a walk time of T or less. Constraints * 1 ≤ N ≤ 300 * 1 ≤ T ≤ 10 ^ 4 * 1 ≤ a_i, b_i ≤ N * 1 ≤ t_i ≤ 10 ^ 4 * 0 ≤ m_i ≤ 10 ^ 4 * 1 ≤ v_i ≤ 10 ^ 5 * You can go back and forth between the two squares. Input Input follows the following format. All given numbers are integers. N T a_1 b_1 t_1 m_1 v_1 .. .. a_ {N-1} b_ {N-1} t_ {N-1} m_ {N-1} v_ {N-1} The i + 1 line of the input shows the path of the passage time t_i, the upper limit of the number of discoveries obtained, m_i, and the value v_i per discovery, which connects the squares a_i and b_i. Output Output the maximum value of the total value of the findings obtained on one line. Examples Input 4 5 1 2 1 4 7 2 3 1 2 77 2 4 1 2 777 Output 1568 Input 2 10000 1 2 1 10 1 Output 10 Input 5 15 1 2 3 2 1 2 3 3 2 1 1 4 8 2 777 1 5 1 3 10 Output 32
7
0
#include <cstdio> #include <vector> #include <deque> using namespace std; const int MAX_N = 300; const int MAX_W = int(1e4); const int INF = int(1.05e9); //^@本当にこれで大丈夫? template<typename numType> inline bool updateMax(numType& old, const numType& test) { if (old < test) { old = test; return true; } return false; } int N, W; int stamp[2*MAX_N]; vector<int> times[MAX_N]; bool toDown[2*MAX_N]; int ws[2*MAX_N], vs[2*MAX_N], ms[2*MAX_N]; int opt[2*MAX_N][MAX_W + 1]; class Edge { public: int to; int weight, value, amount; Edge(){} Edge(int to_, int weight_, int value_, int amount_): to(to_), weight(weight_), value(value_), amount(amount_) {} }; class Graph : public vector< vector<Edge> > { public: int numV; Graph(){} void init(int numV_) { numV = numV_; init(); } void init() { assign(numV, vector<Edge>()); } void addUndirectedEdge(int from, int to, int weight, int value, int amount) { operator [](from).push_back(Edge(to, weight, value, amount)); operator [](to).push_back(Edge(from, weight, value, amount)); } }; Graph g; void init() { scanf("%d%d", &N, &W); g.init(N); for (int i = 0; i < N - 1; ++i) { int a, b, w, m, v; scanf("%d%d%d%d%d", &a, &b, &w, &m, &v); a--; b--; g.addUndirectedEdge(a, b, w, v, m); } } void traverse(int v, int parent, int &eu) { stamp[eu] = v; toDown[eu] = true; times[v].push_back(eu); for (int i = 0; i < int(g[v].size()); ++i) { const Edge &e = g[v][i]; const int u = e.to; if (u != parent) { ws[eu] = e.weight; vs[eu] = e.value; ms[eu] = e.amount; traverse(u, v, ++eu); stamp[eu] = v; toDown[eu] = true; times[v].push_back(eu); } } toDown[eu++] = false; } int solve() { int eu = 0; traverse(0, -1, eu); for (int i = 1; i < 2*N-1; ++i) { fill(opt[i], opt[i] + W + 1, -INF); } for (int i = 0; i < 2*N-1; ++i) { int vert = stamp[i]; int w = ws[i], m = ms[i], v = vs[i]; if (toDown[i]) { int nextTime = -1; for (int j = 0; j + 1 < int(times[vert].size()); ++j) { if (i == times[vert][j]) { nextTime = times[vert][j+1]; } } if (nextTime != -1) { for (int j = 0; j <= W; ++j) { updateMax(opt[nextTime][j], opt[i][j]); } } if (m%2 == 1) { for (int j = 0; j + (m+1)*w <= W; ++j) { updateMax(opt[i+1][j+(m+1)*w], opt[i][j] + m*v); } --m; } if (m == 0) { for (int j = 0; j + 2*w <= W; ++j) { updateMax(opt[i+1][j+2*w], opt[i][j]); } } else { m -= 2; m /= 2; if (m > 0) { w *= 2; v *= 2; // for (int a = 0; a < w; ++a) { // deque< pair<int, int> > deq; // (index, value) // for (int j = 0; j * w + a <= W; ++j) { // int value = opt[i][j * w + a] - j * v; // for (; !deq.empty() && deq.back().second <= value; deq.pop_back()) ; // deq.push_back( make_pair(j, value) ); // opt[i][j * w + a] = deq.front().second + j * v; // if (deq.front().first == j - m) { // deq.pop_front(); // } // } // } for (int a = 0; a < w; ++a) { static pair<int,int> deq[MAX_W + 1]; int s = 0, t = 0; for (int j = 0; j * w + a <= W; ++j) { int value = opt[i][j * w + a] - j * v; for (; s<t && deq[t-1].second <= value; --t) ; deq[t++] = make_pair(j, value); opt[i][j * w + a] = deq[s].second + j * v; if (deq[s].first == j - m) { ++s; } } } w /= 2; v /= 2; } for (int j = 0; j + 2*w <= W; ++j) { updateMax(opt[i+1][j+2*w], opt[i][j] + 2*v); } } } else { for (int j = 0; j <= W; ++j) { updateMax(opt[i+1][j], opt[i][j]); } } } return opt[2*N-1][W&(~1)]; } int main() { init(); printf("%d\n", solve()); return 0; }
CPP
p01603 Sanpo
Sanpo Problem Statement The cats love walking. There are new discoveries as you walk down the road. The town where Sora lives consists of N squares and N-1 roads. The road just connects the two squares and does not branch or intersect. For the road, the time t to walk, the upper limit m of the number of discoveries that can be obtained, and the value v per discovery are specified. When walking along the road from one end to the other, one discovery of value v can be obtained in the mth pass, but no discovery can be obtained after the m + 1th pass. It seems that they are going for a walk in the town today. The walk in the sky starts at Square 1, takes several roads (maybe 0), and finally returns to Square 1. Also, when the sun goes down, I feel lonely, so I want to set the walking time to T or less. Your friend, your job, is to find the maximum sum of the values ​​of discoveries you can get on a walk route with a walk time of T or less. Constraints * 1 ≤ N ≤ 300 * 1 ≤ T ≤ 10 ^ 4 * 1 ≤ a_i, b_i ≤ N * 1 ≤ t_i ≤ 10 ^ 4 * 0 ≤ m_i ≤ 10 ^ 4 * 1 ≤ v_i ≤ 10 ^ 5 * You can go back and forth between the two squares. Input Input follows the following format. All given numbers are integers. N T a_1 b_1 t_1 m_1 v_1 .. .. a_ {N-1} b_ {N-1} t_ {N-1} m_ {N-1} v_ {N-1} The i + 1 line of the input shows the path of the passage time t_i, the upper limit of the number of discoveries obtained, m_i, and the value v_i per discovery, which connects the squares a_i and b_i. Output Output the maximum value of the total value of the findings obtained on one line. Examples Input 4 5 1 2 1 4 7 2 3 1 2 77 2 4 1 2 777 Output 1568 Input 2 10000 1 2 1 10 1 Output 10 Input 5 15 1 2 3 2 1 2 3 3 2 1 1 4 8 2 777 1 5 1 3 10 Output 32
7
0
#include<cstdio> #include<vector> #include<algorithm> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int INF=1<<29; const int V_MAX=300; const int E_MAX=V_MAX-1; const int T_MAX=10000; struct edge{ int v,cost,capa,val; }; int n; vector<edge> G[V_MAX]; // tree vector< pair<edge,int> > order; // <<to,cost,capa,val>,forward or backward> void dfs(int u,int pre){ int cost_parent=0,capa_parent=0,val_parent=0; rep(i,G[u].size()){ int v=G[u][i].v,cost=G[u][i].cost,capa=G[u][i].capa,val=G[u][i].val; if(v!=pre){ order.push_back(make_pair((edge){u,cost,capa,val},0)); // forward dfs(v,u); } else{ cost_parent=cost; capa_parent=capa; val_parent=val; } } order.push_back(make_pair((edge){u,cost_parent,capa_parent,val_parent},1)); // backward } int main(){ int T; scanf("%d%d",&n,&T); rep(u,n) G[u].clear(); rep(i,n-1){ int u,v,cost,capa,val; scanf("%d%d%d%d%d",&u,&v,&cost,&capa,&val); u--; v--; G[u].push_back((edge){v,cost,capa,val}); G[v].push_back((edge){u,cost,capa,val}); } order.clear(); dfs(0,-1); int m=order.size(); static int pre[V_MAX+E_MAX]; rep(i,m){ pre[i]=-1; int u=order[i].first.v; rep(j,i){ int v=order[j].first.v; if(u==v) pre[i]=j; } } static int dp[V_MAX+E_MAX][T_MAX+1]; rep(i,m) rep(j,T+1) dp[i][j]=-INF; dp[0][0]=0; for(int i=1;i<m;i++){ int cost=order[i-1].first.cost; int capa=order[i-1].first.capa; int val=order[i-1].first.val; int dir=order[i-1].second; rep(j,T+1) if(pre[i]!=-1) dp[i][j]=max(dp[i][j],dp[pre[i]][j]); if(dir==0){ // forward if(capa%2==1){ for(int j=(capa+1)*cost;j<=T;j++){ dp[i][j]=max(dp[i][j],dp[i-1][j-(capa+1)*cost]+capa*val); } capa--; } if(capa==0){ for(int j=2*cost;j<=T;j++) dp[i][j]=max(dp[i][j],dp[i-1][j-2*cost]); } else{ // capa>=2 capa-=2; for(int c=2;capa>0;c*=2){ // O(log capa) 個の商品に分割 c=min(c,capa); for(int j=T;j>=c*cost;j--){ dp[i-1][j]=max(dp[i-1][j],dp[i-1][j-c*cost]+c*val); } capa-=c; } for(int j=2*cost;j<=T;j++) dp[i][j]=max(dp[i][j],dp[i-1][j-2*cost]+2*val); } } else{ // backward rep(j,T+1) dp[i][j]=max(dp[i][j],dp[i-1][j]); } } printf("%d\n",*max_element(dp[m-1],dp[m-1]+T+1)); return 0; }
CPP
p01603 Sanpo
Sanpo Problem Statement The cats love walking. There are new discoveries as you walk down the road. The town where Sora lives consists of N squares and N-1 roads. The road just connects the two squares and does not branch or intersect. For the road, the time t to walk, the upper limit m of the number of discoveries that can be obtained, and the value v per discovery are specified. When walking along the road from one end to the other, one discovery of value v can be obtained in the mth pass, but no discovery can be obtained after the m + 1th pass. It seems that they are going for a walk in the town today. The walk in the sky starts at Square 1, takes several roads (maybe 0), and finally returns to Square 1. Also, when the sun goes down, I feel lonely, so I want to set the walking time to T or less. Your friend, your job, is to find the maximum sum of the values ​​of discoveries you can get on a walk route with a walk time of T or less. Constraints * 1 ≤ N ≤ 300 * 1 ≤ T ≤ 10 ^ 4 * 1 ≤ a_i, b_i ≤ N * 1 ≤ t_i ≤ 10 ^ 4 * 0 ≤ m_i ≤ 10 ^ 4 * 1 ≤ v_i ≤ 10 ^ 5 * You can go back and forth between the two squares. Input Input follows the following format. All given numbers are integers. N T a_1 b_1 t_1 m_1 v_1 .. .. a_ {N-1} b_ {N-1} t_ {N-1} m_ {N-1} v_ {N-1} The i + 1 line of the input shows the path of the passage time t_i, the upper limit of the number of discoveries obtained, m_i, and the value v_i per discovery, which connects the squares a_i and b_i. Output Output the maximum value of the total value of the findings obtained on one line. Examples Input 4 5 1 2 1 4 7 2 3 1 2 77 2 4 1 2 777 Output 1568 Input 2 10000 1 2 1 10 1 Output 10 Input 5 15 1 2 3 2 1 2 3 3 2 1 1 4 8 2 777 1 5 1 3 10 Output 32
7
0
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <iostream> #include <complex> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <functional> #include <cassert> typedef long long ll; using namespace std; #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; #define mod 1000000007 //1e9+7(prime number) #define INF 1000000000 //1e9 #define LLINF 2000000000000000000LL //2e18 #define SIZE 310 #define MAX_T 10000 int n, T; vector<pair<int,int> > way[SIZE]; int a[SIZE], b[SIZE], t[SIZE], m[SIZE], v[SIZE]; void dfs(vector<ll> &dp, int now, int back=-1, int eid = -1){ if(eid >= 0){ int w = max(0, m[eid] - 2) / 2; ll tmp_v = v[eid]*2, tmp_t = t[eid]*2; for(int j=1;j<=w;j*=2){ for(int i=T-tmp_t;i>=0;i--){ dp[i+tmp_t] = max(dp[i+tmp_t], dp[i] + tmp_v); } w -= j; tmp_t *= 2; tmp_v *= 2; } tmp_v=v[eid]*2*w; tmp_t=t[eid]*2*w; for(int i=T-tmp_t;i>=0;i--){ dp[i+tmp_t] = max(dp[i+tmp_t], dp[i] + tmp_v); } if(max(0, m[eid] - 2)%2){ for(int i=T-t[eid]*2;i>=0;i--){ dp[i+t[eid]*2] = max(dp[i+t[eid]*2], dp[i] + v[eid]); } } } for(int i=0;i<way[now].size();i++){ if(way[now][i].first == back) continue; int id = way[now][i].second; vector<ll> dp_dfs(T+1, -LLINF); for(int j=0;j+t[id]*2<=T;j++) dp_dfs[j+t[id]*2] = dp[j]; dfs(dp_dfs, way[now][i].first, now, id); for(int j=0;j<=T;j++) dp[j] = max(dp[j], dp_dfs[j] + v[id]*min(2, m[id])); } } int main(){ scanf("%d%d", &n, &T); for(int i=0;i<n-1;i++){ scanf("%d%d%d%d%d", a+i, b+i, t+i, m+i ,v+i); a[i]--; b[i]--; way[a[i]].push_back({b[i],i}); way[b[i]].push_back({a[i],i}); } vector<ll> dp(T + 1, -LLINF); dp[0] = 0; dfs(dp, 0); ll ans = 0; for(int i=0;i<=T;i++) ans = max(ans, dp[i]); printf("%lld\n", ans); return 0; }
CPP
p01603 Sanpo
Sanpo Problem Statement The cats love walking. There are new discoveries as you walk down the road. The town where Sora lives consists of N squares and N-1 roads. The road just connects the two squares and does not branch or intersect. For the road, the time t to walk, the upper limit m of the number of discoveries that can be obtained, and the value v per discovery are specified. When walking along the road from one end to the other, one discovery of value v can be obtained in the mth pass, but no discovery can be obtained after the m + 1th pass. It seems that they are going for a walk in the town today. The walk in the sky starts at Square 1, takes several roads (maybe 0), and finally returns to Square 1. Also, when the sun goes down, I feel lonely, so I want to set the walking time to T or less. Your friend, your job, is to find the maximum sum of the values ​​of discoveries you can get on a walk route with a walk time of T or less. Constraints * 1 ≤ N ≤ 300 * 1 ≤ T ≤ 10 ^ 4 * 1 ≤ a_i, b_i ≤ N * 1 ≤ t_i ≤ 10 ^ 4 * 0 ≤ m_i ≤ 10 ^ 4 * 1 ≤ v_i ≤ 10 ^ 5 * You can go back and forth between the two squares. Input Input follows the following format. All given numbers are integers. N T a_1 b_1 t_1 m_1 v_1 .. .. a_ {N-1} b_ {N-1} t_ {N-1} m_ {N-1} v_ {N-1} The i + 1 line of the input shows the path of the passage time t_i, the upper limit of the number of discoveries obtained, m_i, and the value v_i per discovery, which connects the squares a_i and b_i. Output Output the maximum value of the total value of the findings obtained on one line. Examples Input 4 5 1 2 1 4 7 2 3 1 2 77 2 4 1 2 777 Output 1568 Input 2 10000 1 2 1 10 1 Output 10 Input 5 15 1 2 3 2 1 2 3 3 2 1 1 4 8 2 777 1 5 1 3 10 Output 32
7
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long LL; struct Edge { int to, t, m; LL v; }; void dfs(int v, int parent, const vector<vector<Edge>> &graph, vector<pair<int,Edge>> &ord) { for(const auto &e : graph[v]) { if(e.to == parent) continue; ord.push_back(make_pair(e.to, e)); dfs(e.to, v, graph, ord); ord.push_back(make_pair(v, e)); } } bool solve() { int N, T; if(!(cin >> N >> T)) return false; if(!N && !T) return false; vector<vector<Edge>> graph(N); for(int i = 0; i < N-1; ++i) { int a, b, t, m; LL v; cin >> a >> b >> t >> m >> v; --a; --b; graph[a].push_back((Edge){b, t, m, v}); graph[b].push_back((Edge){a, t, m, v}); } vector<pair<int,Edge>> ord; ord.push_back(make_pair(0, Edge())); dfs(0, -1, graph, ord); const int M = ord.size(); vector<vector<LL>> dp(M, vector<LL>(T+1, -1)); dp[0][0] = 0; for(int i = 1; i < M; ++i) { const int v = ord[i].first; const Edge &e = ord[i].second; if(v != e.to) { //back int rem = e.m; // should pay 2 for(int t = T; t >= 0; --t) { if(t-e.t*2 >= 0 && dp[i-1][t-e.t*2] >= 0) dp[i][t] = dp[i-1][t-e.t*2] + min(2, e.m)*e.v; } rem -= min(2, e.m); // optionally pay 1 if e.m is odd if(rem % 2 == 1) { for(int t = T; t >= 0; --t) { if(t-e.t*2 >= 0 && dp[i][t-e.t*2] >= 0) dp[i][t] = max(dp[i][t], dp[i][t-e.t*2] + e.v); } --rem; } // binary dp for(int k = 2; rem > 0; k *= 2) { const int s = min(k, rem); const int dt = e.t * s; const int dv = e.v * s; for(int t = T; t >= 0; --t) { if(t-dt >= 0 && dp[i][t-dt] >= 0) dp[i][t] = max(dp[i][t], dp[i][t-dt] + dv); } rem -= s; } } else { dp[i] = dp[i-1]; } // Ignore current subtree for(int j = i-1; j >= 0; --j) { if(ord[j].first == v) { for(int t = 0; t <= T; ++t) { dp[i][t] = max(dp[i][t], dp[j][t]); } break; } } /* cout << i << ' ' << v << ": "; for(auto val : dp[i]) { cout << val << ' '; } cout << endl; */ } cout << *max_element(dp[M-1].begin(), dp[M-1].end()) << endl; return true; } int main() { cin.tie(0); ios::sync_with_stdio(0); while(solve()) ; return 0; }
CPP
p01759 Vongress
Taro loves a game called Vongress. Vongress is a camp game played on a board that is a convex polygon consisting of n vertices. In this game, m players place one piece each inside the board. The position of the piece is represented by a set of x-coordinate and y-coordinate, and the size of the piece is not considered. A player's position is an area on the board that consists of the closest piece placed by that person. Players get the area of ​​their base as a score. Taro always recorded the shape of the board, the position of the placed piece, and the score of each player, but one day he inadvertently forgot to record the position of the piece. Therefore, Taro decided to add the consistent position of the piece from the shape of the board and the score of the player. Your job is to find the position of the piece that is consistent with each player's score on behalf of Taro. Input The input is given in the following format. n m x1 y1 x2 y2 :: xn yn r1 r2 :: rm n is the number of vertices of the convex polygon that is the board, and m is the number of players. (xi, yi) represents the coordinates of the vertices of the board. r1, ..., rm represent the ratio of scores obtained by each player. Constraints * 3 ≤ n ≤ 100 * 1 ≤ m ≤ 100 * −104 ≤ xi, yi ≤ 104 * 1 ≤ rj ≤ 100 * All inputs are integers. * The vertices (xi, yi) of the convex polygon are given in the counterclockwise order. Output Output the positions of m pieces in the following format. x'1 y'1 x'2 y'2 :: x'm y'm (x'k, y'k) is the position of the kth player's piece. The output must meet the following conditions: * The piece is inside the convex polygon. It may be on the border. * Both pieces are more than 10-5 apart. * Let S be the area of ​​the given convex polygon. The absolute or relative error with $ \ frac {r_k} {r_1 + ... + r_m} S $ for the area of ​​the kth player's position obtained from the output is less than 10-3. Sample Input 1 4 5 1 1 -1 1 -1 -1 1 -1 1 1 1 1 1 Output for the Sample Input 1 0.0000000000 0.0000000000 0.6324555320 0.6324555320 -0.6324555320 0.6324555320 -0.6324555320 -0.6324555320 0.6324555320 -0.6324555320 By arranging the pieces as shown in the figure below, all players can get the same score. The black line segment represents the board, the red dot represents the player's piece, and the blue line segment represents the boundary line of the player's position. Vongress Sample Input 2 4 3 1 1 -1 1 -1 -1 1 -1 9 14 9 Output for the Sample Input 2 -0.5 -0.5 0 0 0.5 0.5 Arrange the pieces as shown in the figure below. Vongress Sample Input 3 8 5 2 0 1 2 -1 2 -twenty one -twenty one 0 -2 1-2 twenty one 3 6 9 3 Five Output for the Sample Input 3 1.7320508076 -0.5291502622 1.4708497378 -0.2679491924 -0.4827258592 0.2204447068 -0.7795552932 0.5172741408 -1.0531143674 0.9276127521 Arrange the pieces as shown in the figure below. Vongress Example Input 4 5 1 1 -1 1 -1 -1 1 -1 1 1 1 1 1 Output 0.0000000000 0.0000000000 0.6324555320 0.6324555320 -0.6324555320 0.6324555320 -0.6324555320 -0.6324555320 0.6324555320 -0.6324555320
7
0
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <ctime> #include <queue> #include <stack> #include <algorithm> #include <list> #include <vector> #include <set> #include <map> #include <iostream> #include <deque> #include <complex> #include <string> #include <iomanip> #include <sstream> #include <bitset> #include <valarray> #include <unordered_map> #include <iterator> #include <functional> #include <cassert> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} namespace geom{ #define X real() #define Y imag() #define at(i) ((*this)[i]) #define SELF (*this) enum {TRUE = 1, FALSE = 0, BORDER = -1}; typedef int BOOL; typedef long double R; const R INF = 1e8; R EPS = 1e-6; const R PI = 3.1415926535897932384626; inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } inline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;} typedef complex<R> P; inline R norm(const P &p){return p.X*p.X+p.Y*p.Y;} inline R inp(const P& a, const P& b){return (conj(a)*b).X;} inline R outp(const P& a, const P& b){return (conj(a)*b).Y;} inline P unit(const P& p){return p/abs(p);} inline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);} inline int ccw(const P &s, const P &t, const P &p, int adv=0){ int res = sig(outp(t-s, p-s)); if(res || !adv) return res; if(sig(inp(t-s, p-s)) < 0) return -2; // p-s-t if(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p return 0; // s-p-t } struct L : public vector<P>{ // line L(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);} L(){} P dir()const {return at(1) - at(0);} }; struct S : public L{ // segment S(const P &p1, const P &p2):L(p1, p2){} S(){} }; inline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);} inline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;} inline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));} inline P crosspoint(const L &l, const L &m){ R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]); if(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line if(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } struct G : public vector<P>{ G(size_type size=0):vector(size){} S edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));} R area()const { R sum = 0; REP(i, size()) sum += outp(at(i), at((i+1)%size())); return abs(sum / 2.); } G cut(const L &l)const { G g; REP(i, size()){ const S &s = edge(i); if(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]); if(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0) g.push_back(crosspoint(s, l)); } return g; } }; #undef SELF #undef at } using namespace geom; int f = 0; namespace std{ bool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;} bool operator==(const P &a, const P &b){return abs(a-b) < EPS;} istream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;} istream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];} const R B = 500; const R Z = 50; ostream& operator<<(ostream &os, const P &p){return os << "circle("<<B+Z*(p.X)<<", "<<1000-B-Z*(p.Y)<<", 2)";} ostream& operator<<(ostream &os, const S &s){return os << "line("<<B+Z*(s[0].X)<<", "<<1000-B-Z*(s[0].Y)<<", "<<B+Z*(s[1].X)<<", "<<1000-B-Z*(s[1].Y)<<")";} ostream& operator<<(ostream &os, const G &g){REP(i, g.size()) os << g.edge(i) << endl;return os;} } int T, n, m; R len, rot; P bias; G g; vector<pair<R, int>> areas; R search(R area){ R l=0, r=len; REP(itr, 50){ R mid=(r+l)/2; if(g.cut(L(P(mid, 0), P(mid, 1))).area() < area) l = mid; else r = mid; } return r; } int main(int argc, char *argv[]){ ios::sync_with_stdio(false); cin >> n >> m; g.resize(n); REP(i, n) cin >> g[i]; //cerr << g << endl; tuple<R, int, int> ma(-1, 0, 0); REP(i, n)REP(j, n) chmax(ma, tuple<R, int, int>(abs(g[i]-g[j]), i, j)); len = get<0>(ma); rot = arg(g[get<2>(ma)] - g[get<1>(ma)]); for(auto &p : g) p *= polar((R)1, -rot); bias = g[get<1>(ma)]; for(auto &p : g) p -= bias; R sumarea = g.area(); R sum = 0; REP(i, m){ R r; cin >> r; areas.eb(r*sumarea, i); sum += r; } REP(i, m) areas[i].first /= sum; sort(RALL(areas)); vector<pair<R, int>> tar; R prevw = INF, prevb = 0; int i=0; sumarea = 0; int center = -1; while(center == -1){ REP(i, areas.size()){ R b = search(sumarea + areas[i].first); if(i+1 == areas.size()){ tar.eb(b, areas[i].second); if(prevw > b - prevb) center = tar.size()-1; else center = tar.size()-2; sumarea += areas[i].first; areas.erase(areas.begin() + i); break; }else if(prevw > b - prevb){ tar.eb(b, areas[i].second); prevw = b - prevb; prevb = b; sumarea += areas[i].first; areas.erase(areas.begin() + i); break; } } } while(!areas.empty()){ R b = search(sumarea + areas.back().first); tar.eb(b, areas.back().second); sumarea += areas.back().first; areas.pop_back(); } // cout << center << ", " << tar << endl; //cerr << g << endl; //REP(i, m) cerr << S(P(tar[i].first, -100), P(tar[i].first, 100)) << endl; // REPS(i, tar.size()-1) cout << tar[i].first - tar[i-1].first << endl; vector<P> ans(m); ans[tar[center].second] = P((tar[center].first + tar[center-1].first)/2, (R)0); for(int i=center-1;i>=0;i--){ ans[tar[i].second] = reflect(ans[tar[i+1].second], L(P(tar[i].first, 0), P(tar[i].first, 1))); } for(int i=center+1;i<m;i++){ ans[tar[i].second] = reflect(ans[tar[i-1].second], L(P(tar[i-1].first, 0), P(tar[i-1].first, 1))); } REP(i, m){ P p = (ans[i] + bias) * polar((R)1, rot); // cerr << p << endl; printf("%.11f %.11f\n", (double)p.X, (double)p.Y); } return 0; }
CPP
p01759 Vongress
Taro loves a game called Vongress. Vongress is a camp game played on a board that is a convex polygon consisting of n vertices. In this game, m players place one piece each inside the board. The position of the piece is represented by a set of x-coordinate and y-coordinate, and the size of the piece is not considered. A player's position is an area on the board that consists of the closest piece placed by that person. Players get the area of ​​their base as a score. Taro always recorded the shape of the board, the position of the placed piece, and the score of each player, but one day he inadvertently forgot to record the position of the piece. Therefore, Taro decided to add the consistent position of the piece from the shape of the board and the score of the player. Your job is to find the position of the piece that is consistent with each player's score on behalf of Taro. Input The input is given in the following format. n m x1 y1 x2 y2 :: xn yn r1 r2 :: rm n is the number of vertices of the convex polygon that is the board, and m is the number of players. (xi, yi) represents the coordinates of the vertices of the board. r1, ..., rm represent the ratio of scores obtained by each player. Constraints * 3 ≤ n ≤ 100 * 1 ≤ m ≤ 100 * −104 ≤ xi, yi ≤ 104 * 1 ≤ rj ≤ 100 * All inputs are integers. * The vertices (xi, yi) of the convex polygon are given in the counterclockwise order. Output Output the positions of m pieces in the following format. x'1 y'1 x'2 y'2 :: x'm y'm (x'k, y'k) is the position of the kth player's piece. The output must meet the following conditions: * The piece is inside the convex polygon. It may be on the border. * Both pieces are more than 10-5 apart. * Let S be the area of ​​the given convex polygon. The absolute or relative error with $ \ frac {r_k} {r_1 + ... + r_m} S $ for the area of ​​the kth player's position obtained from the output is less than 10-3. Sample Input 1 4 5 1 1 -1 1 -1 -1 1 -1 1 1 1 1 1 Output for the Sample Input 1 0.0000000000 0.0000000000 0.6324555320 0.6324555320 -0.6324555320 0.6324555320 -0.6324555320 -0.6324555320 0.6324555320 -0.6324555320 By arranging the pieces as shown in the figure below, all players can get the same score. The black line segment represents the board, the red dot represents the player's piece, and the blue line segment represents the boundary line of the player's position. Vongress Sample Input 2 4 3 1 1 -1 1 -1 -1 1 -1 9 14 9 Output for the Sample Input 2 -0.5 -0.5 0 0 0.5 0.5 Arrange the pieces as shown in the figure below. Vongress Sample Input 3 8 5 2 0 1 2 -1 2 -twenty one -twenty one 0 -2 1-2 twenty one 3 6 9 3 Five Output for the Sample Input 3 1.7320508076 -0.5291502622 1.4708497378 -0.2679491924 -0.4827258592 0.2204447068 -0.7795552932 0.5172741408 -1.0531143674 0.9276127521 Arrange the pieces as shown in the figure below. Vongress Example Input 4 5 1 1 -1 1 -1 -1 1 -1 1 1 1 1 1 Output 0.0000000000 0.0000000000 0.6324555320 0.6324555320 -0.6324555320 0.6324555320 -0.6324555320 -0.6324555320 0.6324555320 -0.6324555320
7
0
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <ctime> #include <queue> #include <stack> #include <algorithm> #include <list> #include <vector> #include <set> #include <map> #include <iostream> #include <deque> #include <complex> #include <string> #include <iomanip> #include <sstream> #include <bitset> #include <valarray> #include <unordered_map> #include <iterator> #include <functional> #include <cassert> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} namespace geom{ #define X real() #define Y imag() #define at(i) ((*this)[i]) #define SELF (*this) enum {TRUE = 1, FALSE = 0, BORDER = -1}; typedef int BOOL; typedef double R; const R INF = 1e8; R EPS = 1e-6; const R PI = 3.1415926535897932384626; inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } inline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;} typedef complex<R> P; inline R norm(const P &p){return p.X*p.X+p.Y*p.Y;} inline R inp(const P& a, const P& b){return (conj(a)*b).X;} inline R outp(const P& a, const P& b){return (conj(a)*b).Y;} inline P unit(const P& p){return p/abs(p);} inline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);} inline int ccw(const P &s, const P &t, const P &p, int adv=0){ int res = sig(outp(t-s, p-s)); if(res || !adv) return res; if(sig(inp(t-s, p-s)) < 0) return -2; // p-s-t if(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p return 0; // s-p-t } struct L : public vector<P>{ // line L(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);} L(){} P dir()const {return at(1) - at(0);} }; struct S : public L{ // segment S(const P &p1, const P &p2):L(p1, p2){} S(){} }; inline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);} inline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;} inline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));} inline P crosspoint(const L &l, const L &m){ R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]); if(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line if(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } struct G : public vector<P>{ G(size_type size=0):vector(size){} S edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));} R area()const { R sum = 0; REP(i, size()) sum += outp(at(i), at((i+1)%size())); return abs(sum / 2.); } G cut(const L &l)const { G g; REP(i, size()){ const S &s = edge(i); if(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]); if(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0) g.push_back(crosspoint(s, l)); } return g; } }; #undef SELF #undef at } using namespace geom; int f = 0; namespace std{ bool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;} bool operator==(const P &a, const P &b){return abs(a-b) < EPS;} istream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;} istream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];} const R B = 500; const R Z = 50; ostream& operator<<(ostream &os, const P &p){return os << "circle("<<B+Z*(p.X)<<", "<<1000-B-Z*(p.Y)<<", 2)";} ostream& operator<<(ostream &os, const S &s){return os << "line("<<B+Z*(s[0].X)<<", "<<1000-B-Z*(s[0].Y)<<", "<<B+Z*(s[1].X)<<", "<<1000-B-Z*(s[1].Y)<<")";} ostream& operator<<(ostream &os, const G &g){REP(i, g.size()) os << g.edge(i) << endl;return os;} } int T, n, m; R len, rot; P bias; G g; vector<pair<R, int>> areas; R search(R area){ R l=0, r=len; REP(itr, 30){ R mid=(r+l)/2; if(g.cut(L(P(mid, 0), P(mid, 1))).area() < area) l = mid; else r = mid; } return r; } int main(int argc, char *argv[]){ ios::sync_with_stdio(false); cin >> n >> m; g.resize(n); REP(i, n) cin >> g[i]; //cerr << g << endl; tuple<R, int, int> ma(-1, 0, 0); REP(i, n)REP(j, n) chmax(ma, tuple<R, int, int>(abs(g[i]-g[j]), i, j)); len = get<0>(ma); rot = arg(g[get<2>(ma)] - g[get<1>(ma)]); for(auto &p : g) p *= polar((R)1, -rot); bias = g[get<1>(ma)]; for(auto &p : g) p -= bias; R sumarea = g.area(); R sum = 0; REP(i, m){ R r; cin >> r; areas.eb(r*sumarea, i); sum += r; } REP(i, m) areas[i].first /= sum; sort(RALL(areas)); vector<pair<R, int>> tar; R prevw = INF, prevb = 0; int i=0; sumarea = 0; int center = -1; while(center == -1){ REP(i, areas.size()){ R b = search(sumarea + areas[i].first); if(i+1 == areas.size()){ tar.eb(b, areas[i].second); if(prevw > b - prevb) center = tar.size()-1; else center = tar.size()-2; sumarea += areas[i].first; areas.erase(areas.begin() + i); break; }else if(prevw > b - prevb){ tar.eb(b, areas[i].second); prevw = b - prevb; prevb = b; sumarea += areas[i].first; areas.erase(areas.begin() + i); break; } } } while(!areas.empty()){ R b = search(sumarea + areas.back().first); tar.eb(b, areas.back().second); sumarea += areas.back().first; areas.pop_back(); } // cout << center << ", " << tar << endl; //cerr << g << endl; //REP(i, m) cerr << S(P(tar[i].first, -100), P(tar[i].first, 100)) << endl; // REPS(i, tar.size()-1) cout << tar[i].first - tar[i-1].first << endl; vector<P> ans(m); ans[tar[center].second] = P((tar[center].first + tar[center-1].first)/2, (R)0); for(int i=center-1;i>=0;i--){ ans[tar[i].second] = reflect(ans[tar[i+1].second], L(P(tar[i].first, 0), P(tar[i].first, 1))); } for(int i=center+1;i<m;i++){ ans[tar[i].second] = reflect(ans[tar[i-1].second], L(P(tar[i-1].first, 0), P(tar[i-1].first, 1))); } REP(i, m){ P p = (ans[i] + bias) * polar((R)1, rot); // cerr << p << endl; printf("%.11f %.11f\n", (double)p.X, (double)p.Y); } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<iostream> using namespace std; int main(){ int a,n,d,c=0; cin >> n >> d; for(int i=0;i<n;i++) { cin >> a; if(a>d)c+=a-d; } if(c==0)cout << "kusoge" << endl; else cout << c << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; int main(){ int n,d; cin >> n >> d; int ans = 0; for(int i = 0 ; i < n ; i++){ int p; cin >> p; ans += max(0,p - d); } if( ans == 0 ){ cout << "kusoge" << endl; }else{ cout << ans << endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; #define enld '\n' #define rep(i,n) for(int i=0; i<(n); i++) #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("Ofast") constexpr ll INF = 1e18; constexpr int inf = 1e9; constexpr ll mod = 1000000007; constexpr ll mod2 = 998244353; const double PI = 3.1415926535897932384626433832795028841971; const int dx[8] = {1, 0, -1, 0,1,1,-1,-1}; const int dy[8] = {0, 1, 0, -1,1,-1,-1,1}; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } // --------------------------------------------------------------------------- int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,d; cin >> N >> d; vector<int> p(N); rep(i,N) cin >> p[i]; int sum = 0; rep(i,N){ sum += max(0,p[i]-d); } if(sum == 0){ cout << "kusoge\n"; }else{ cout << sum << "\n"; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<iostream> using namespace std; int n,d,p,sum; int main(){ cin>>n>>d; for(int i=0;i<n;i++){cin>>p;if(p>=d)sum+=(p-d);} if(sum==0)cout<<"kusoge"<<endl; else cout<<sum<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int n,d,p,ans; int main(){ cin>>n>>d; while(n--){ cin>>p; ans+=max(0,p-d); } if(!ans)cout<<"kusoge"<<endl; else cout<<ans<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> using namespace std; int main(){ int n,d,p,total=0; cin >> n >> d; for (int i=0;i<n;i++) { cin >> p; if (p>d) total += p-d; } if (total == 0) cout << "kusoge" << endl; else cout << total << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
n, d = map(int, input().split()) lst = list(map(int, input().split())) ans = sum([x - d for x in lst if x - d >= 0]) print(ans if ans else "kusoge")
PYTHON3
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main() { int N, D; cin >> N >> D; int ret = 0; for(int i = 0; i < N; i++) { int P; cin >> P; ret += max(0, P - D); } if(ret == 0) cout << "kusoge" << endl; else cout << ret << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int d = sc.nextInt(); int[] p = new int[n]; for(int i=0;i<n;i++) { p[i] = sc.nextInt(); } int score = 0; for(int i=0;i<n;i++) { int s = p[i] - d; if (s > 0) { score+=s; } } if (score <= 0) { System.out.println("kusoge"); }else{ System.out.println(score); } } }
JAVA
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> using namespace std; int main(){ int N,d; cin>>N>>d; int ans=0; for(int i=0;i<N;i++){ int p; cin>>p; if(p>d)ans+=p-d; } if(ans)cout<<ans<<endl; else cout<<"kusoge"<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n)-1;i>=0;i--) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define printV(v) for(auto&& x : v){cout << x << " ";} cout << endl #define printVV(vv) for(auto&& v : vv){for(auto&& x : v){cout << x << " ";}cout << endl;} #define printP(p) cout << p.first << " " << p.second << endl #define printVP(vp) for(auto&& p : vp) printP(p); typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> TUPLE; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<Pii> vp; const int inf = 1e9; const int mod = 1e9 + 7; template<typename T> using Graph = vector<vector<T>>; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int N, d; cin >> N >> d; vi p(N); rep(i, N) cin >> p[i]; int ans = 0; rep(i, N) ans += max(0ll, p[i] - d); if (ans == 0) { cout << "kusoge" << endl; } else { cout << ans << endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <stdio.h> int main(void){ int n, d, i, j, ans = 0; scanf("%d%d", &n, &d); int p[n]; for(i = 0; i < n; ++i) { scanf("%d", &p[i]); p[i] -= d; } for(i = 0; i < n; ++i) if(p[i] > 0) ans += p[i]; if(ans > 0) printf("%d\n", ans); else printf("kusoge\n"); return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> #include <vector> #include <string> #include <set> #include <queue> #define rep(i,a) for(int i = 0 ; i < a ; i ++) using namespace std; int main(void){ int n,ret = 0,in,d; cin>>n>>d; rep(i,n){ cin>>in; ret += (in>d?in-d:0); } if(ret==0){ cout<<"kusoge"<<endl; }else{ cout<<ret<<endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
import java.util.Scanner; /** * Yamanote-line Game */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line; int N, d; N = sc.nextInt(); d = sc.nextInt(); int ans = 0; for (int i = 0; i < N; i++) { ans += Math.max(sc.nextInt() - d, 0); } System.out.println(ans > 0 ? ans : "kusoge"); } }
JAVA
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) const double PI = 3.1415926535897932384626433832795028841971; const int INF = 1000000007; const double EPS = 1e-10; const int MOD = 1000000007; using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> PP; int n, d; int p[1000]; int main(){ int cnt = 0; cin >> n >> d; rep(i,n) cin >> p[i]; rep(i,n){ if(p[i] > d){ cnt += p[i]-d; } } if(cnt <= 0) cout << "kusoge" << endl; else cout << cnt << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #include <list> using namespace std; typedef long long ll; #define sz size() #define pb push_back #define mp make_pair #define fi first #define se second #define all(c) (c).begin(), (c).end() #define rep(i,a,b) for(ll i=(a);i<(b);++i) #define clr(a, b) memset((a), (b) ,sizeof(a)) #define ctos(d) string(1,d) #define print(x) cout<<#x<<" = "<<x<<endl; #define MOD 1000000007 int main() { ll n,d; cin>>n>>d; ll c = 0; rep(i,0,n){ ll a; cin>>a; if(a>d){ c += a-d; } } if(c==0){ cout << "kusoge" << endl; } else{ cout << c << endl; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); ++i) int main(){ int n; while(cin >> n){ int d; cin >> d; int a[1001]; rep(i,n) cin >> a[i]; sort(a,a+n); reverse(a, a+n); int ans = 0; for(int i = 1; i <= n; ++i){ int c = accumulate(a, a+i, 0) - d*i; ans = max(ans, c); } if(ans <= 0) cout << "kusoge"; else cout << ans; cout << endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; int main() { int n, d; scanf("%d%d", &n, &d); int sum = 0; rep(i, n) { int p; scanf("%d", &p); if (p > d)sum += p - d; } if (sum == 0)puts("kusoge"); else printf("%d\n", sum); }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; #define lp(i,n) for(int i=0;i<n;i++) int main(){ int n,a; cin>>n>>a; int memo; int ans=0; lp(i,n){ cin>>memo; if(memo>a) ans+=memo-a; } if(ans<=0) cout<<"kusoge"<<endl; else cout<<ans<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; int main(){ int n,d,p; cin >> n >> d; int sum=0; for(int i=0;i<n;i++){ cin >> p; if(p-d>0)sum+=p-d; } if(sum>0)cout << sum << endl; else cout << "kusoge\n"; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main() { int N, D; cin >> N >> D; int ret = 0; for(int i = 0; i < N; i++) { int P; cin >> P; ret += max(P - D, 0); } if(ret == 0) { cout << "kusoge" << endl; } else { cout << ret << endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int N, d, p, a = 0; cin>>N>>d; for(int i = 1; i <= N; i++){ cin>>p; if(p-d > 0)a += p-d; } if(a >= 1) cout<<a<<endl; else cout<<"kusoge"<<endl; return 0; } //9:24
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int N,d,ans=0; int p[1000]; cin>>N>>d; for(int i=0;i<N;i++){ cin>>p[i]; ans+=max(0,p[i]-d); } if(ans>0) cout<<ans<<endl; else cout<<"kusoge"<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; int main(void){ int N, d; cin >> N >> d; int p = 0; int ans = 0; for(int i = 0; i < N; i++){ cin >> p; ans += max(0, p-d); } if(ans == 0){ cout << "kusoge" << endl; }else{ cout << ans << endl; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
// g++ -std=c++11 a.cpp #include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<cmath> #include<random> #include<cstring> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<sstream> #include<iomanip> #include<assert.h> #include<typeinfo> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) using namespace std; //kaewasuretyuui typedef long long ll; //#define int ll typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; #define mt make_tuple typedef tuple<int,int,int> tp; typedef vector<tp> vt; template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;} template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;} //template<class C>constexpr int size(const C &c){return (int)c.size();} //template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;} const double PI=acos(-1); const double EPS=1e-7; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10; int dx[]={0,1,0,-1,1,1,-1,-1}; int dy[]={1,0,-1,0,1,-1,1,-1}; int main(){ int n,m; cin>>n>>m; int out=0; rep(i,n){ int a;cin>>a; out+=max(0,a-m); } if(out)cout<<out<<endl; else cout<<"kusoge"<<endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
import java.util.Scanner; public class Main { void run() { try (Scanner sc = new Scanner(System.in)) { int n = Integer.parseInt(sc.next()); int d = Integer.parseInt(sc.next()); int sum = 0; for (int i = 0; i < n; i++) { int p = Integer.parseInt(sc.next()); if (p > d) { sum += (p - d); } } if(sum == 0) { System.out.println("kusoge"); } else { System.out.println(sum); } } } public static void main(String[] args) { new Main().run(); } }
JAVA
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> #define range(i,a,b) for(int i = (a); i < (b); i++) #define rep(i,b) for(int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; #define INF (1 << 29) using namespace std; int main(){ int n, d, p[1001]; int ans = 0; cin >> n >> d; //rep(i,n) cin >> p[i]; rep(i,n){ int inp; cin >> inp; if(inp >= d){ ans += inp - d; } } if(ans < 1) cout << "kusoge" << endl; else cout << ans << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
/* -*- coding: utf-8 -*- * * 2799.cc: Yamanote-line Game */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 1000; /* typedef */ /* global variables */ /* subroutines */ /* main */ int main() { int n, d; scanf("%d%d", &n, &d); int sum = 0; for (int i = 0; i < n; i++) { int pi; scanf("%d", &pi); if (pi > d) sum += pi - d; } if (sum == 0) puts("kusoge"); else printf("%d\n", sum); return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int n; int d; cin >> n >> d; int yen; int sum=0; for(int i=0;i < n;i++){ cin >> yen; if( yen > d){ sum += yen - d; } } if(sum == 0){ cout << "kusoge" << endl; } else{ cout << sum << endl; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" #define REP(i,a,b) for(i=a;i<b;++i) #define rep(i,n) REP(i,0,n) #define ll long long #define ull unsigned ll typedef long double ld; #define ALL(a) begin(a),end(a) #define ifnot(a) if(not a) #define dump(x) cerr << #x << " = " << (x) << endl using namespace std; // #define int ll bool test = 0; int dx[] = { 0,1,0,-1 }; int dy[] = { 1,0,-1,0 }; #define INF (1 << 28) ull mod = (int)1e9 + 7; //..................... #define MAX (int)1e6 + 5 int M, N; char a[105][105]; signed main(void) { int i, j, k, l; int N, d; int p[1005]; cin >> N >> d; rep(i, N) { cin >> p[i]; } sort(p, p + N); int cnt = 0; for (i = N - 1; i > -1; i--) { if (p[i] < d) break; cnt += p[i] - d; } if (cnt == 0) puts("kusoge"); else cout << cnt << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
/* template.cpp [[[ */ #include <bits/stdc++.h> using namespace std; #define get_macro(a, b, c, d, name, ...) name #define rep(...) get_macro(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep(...) get_macro(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define rep1(n) rep2(i_, n) #define rep2(i, n) rep3(i, 0, n) #define rep3(i, a, b) rep4(i, a, b, 1) #define rep4(i, a, b, s) for (ll i = (a); i < (ll)(b); i += (ll)s) #define rrep1(n) rrep2(i_, n) #define rrep2(i, n) rrep3(i, 0, n) #define rrep3(i, a, b) rrep4(i, a, b, 1) #define rrep4(i, a, b, s) for (ll i = (ll)(b) - 1; i >= (ll)(a); i -= (ll)s) #define each(x, c) for (auto &&x : c) #define fs first #define sc second #define all(c) begin(c), end(c) using ui = unsigned; using ll = long long; using ul = unsigned long long; using ld = long double; const int inf = 1e9 + 10; const ll inf_ll = 1e18 + 10; const ll mod = 1e9 + 7; const ll mod9 = 1e9 + 9; const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1}; const int dy[]{0, -1, 0, 1, -1, -1, 1, 1}; template<class T, class U> void chmin(T &x, const U &y){ x = min<T>(x, y); } template<class T, class U> void chmax(T &x, const U &y){ x = max<T>(x, y); } struct prepare_ { prepare_(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(12); } } prepare__; ll in(){ ll x; cin >> x; return x; } vector<ll> in(size_t n){ vector<ll> v(n); each(x, v) cin >> x; return v; } /* ]]] */ int n, d; int a[1000]; int main(){ cin >> n >> d; rep(i, n) cin >> a[i]; int s = 0; rep(i, n) s += max(0, a[i] - d); if (s <= 0) cout << "kusoge\n"; else cout << s << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; // macro #define rep(i,n) for(i=0;i<n;i++) #define ll long long #define all(v) v.begin(), v.end() // code starts int main() { int n,d;cin>>n>>d; vector<int> p(n); int i; rep(i,n)cin>>p[i]; rep(i,n)p[i]-=d; int ans=0; rep(i,n) { if(p[i]>0)ans+=p[i]; } if(ans<=0)cout<<"kusoge"<<endl; else cout<<ans<<endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(i,n) for (int i = 0; i < (n); ++i) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const ll INF=1LL<<60; const int inf=(1<<30)-1; const int mod=1e9+7; int dx[8]={1,0,-1,0,-1,-1,1,1}; int dy[8]={0,1,0,-1,-1,1,-1,1}; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,d;cin >> n >> d; int ans=0; for(int i=0;i<n;i++){ int p;cin >> p; if(p>d){ ans+=p-d; } } if(ans>0){ cout << ans << endl; } else{ cout << "kusoge" << endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) int main(void){ int N,d,p,res=0; cin>>N>>d; rep(i,N){ cin>>p; res+=max(0,p-d); } if(res==0)cout<<"kusoge"<<endl; else cout<<res<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int N; int main(){ int d; cin >> N >> d; int res = 0; for(int i=0;i<N;i++){ int a; cin >> a; if( a > d ) res += a-d; } if( !res ) cout << "kusoge" << endl; else cout << res << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> #include <vector> #include <algorithm> #define rep(i,n) for(int i = 0; i<n; i++) using namespace std; int main(){ int n,d; cin >> n >> d; int r = 0; vector<int>v(1010,0); rep(i,n) cin>>v[i]; sort(v.begin(),v.end(),greater<int>()); if(v[0] <= d) { cout << "kusoge" << endl; return 0; } for(int j = 0;j <v.size();j++) { if (v[j]- d >0 ) r += v[j] - d; else break; } cout << r << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--) #define pb push_back #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll MOD = 1e9 + 7; double EPS = 1e-8; int main(){ int n, d; cin >> n >> d; vi v(n); rep(i, n) cin >> v[i], v[i] -= d; sort(all(v)); auto it = upper_bound(all(v), 0); if(it == v.end()) cout << "kusoge" << endl; else cout << accumulate(it, v.end(), 0) << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef int _loop_int; #define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i) #define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i) #define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i) #define DEBUG(x) cout<<#x<<": "<<x<<endl #define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl #define ALL(a) (a).begin(),(a).end() #define CHMIN(a,b) a=min((a),(b)) #define CHMAX(a,b) a=max((a),(b)) // mod const ll MOD = 1000000007ll; #define FIX(a) ((a)%MOD+MOD)%MOD // floating typedef double Real; const Real EPS = 1e-11; #define EQ0(x) (abs(x)<EPS) #define EQ(a,b) (abs(a-b)<EPS) typedef complex<Real> P; int n,d; int p[1252]; int main(){ scanf("%d%d",&n,&d); REP(i,n)scanf("%d",p+i); int ans = 0; REP(i,n)ans+=max(0,p[i]-d); if(ans==0)puts("kusoge"); else cout<<ans<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(){ ios_base::sync_with_stdio(false); int n, d; cin >> n >> d; vector<int> a(n); for(int i = 0; i < n; ++i){ cin >> a[i]; } int answer = 0; for(int i = 0; i < n; ++i){ if(a[i] > d){ answer += a[i] - d; } } if(answer <= 0){ cout << "kusoge" << endl; }else{ cout << answer << endl; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<iostream> using namespace std; int main(){ int n, d; cin >> n >> d; int score = 0; for(int i = 0; i < n; i++){ int x; cin >> x; score += max(0, x-d); } if(score == 0) cout << "kusoge" << endl; else cout << score << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n,d; cin >> n >> d; vector<int> a(n); for(int i=0; i<n; i++){ cin >> a[i]; } int ans = 0; for(int i=0; i<n; i++){ if(a[i]-d > 0){ ans += a[i]-d; } } if(ans == 0){ cout << "kusoge" << endl; }else{ cout << ans << endl; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i=0;i<(n);i++) signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int N, d; cin >> N >> d; vector<int> p(N); rep(i, N) cin >> p[i]; int smdiff = 0; rep(i, N) { smdiff += max(0, p[i] - d); } int ans = 0; rep(g, N) { int t = -d + smdiff; if (p[g] - d > 0) { t += d; } ans = max(ans, t); } if (ans == 0) { cout << "kusoge" << endl; } else { cout << ans << endl; } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <algorithm> #include <bitset> #include <cassert> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <map> #include <memory> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <typeinfo> #include <utility> #include <vector> #include <array> #include <chrono> #include <random> #include <tuple> #include <unordered_map> #include <unordered_set> #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, ...)type __VA_ARGS__;Scan(__VA_ARGS__); template<typename T> void Scan(T& t) { std::cin >> t; } template<typename First, typename...Rest>void Scan(First& first, Rest&...rest) { std::cin >> first; Scan(rest...); } #define OUT(d) std::cout<<d; #define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<d; #define SOUT(n, c, d) std::cout<<std::setw(n)<<std::setfill(c)<<d; #define SP std::cout<<" "; #define TAB std::cout<<"\t"; #define BR std::cout<<"\n"; #define ENDL std::cout<<std::endl; #define FLUSH std::cout<<std::flush; #define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& r:c)for(auto& i:r)std::cin>>i; #define ALL(a) (a).begin(),(a).end() #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=(n)-1;i>=0;--i) #define FORLL(i, a, b) for(ll i=ll(a);i<ll(b);++i) #define RFORLL(i, a, b) for(ll i=ll(b)-1;i>=ll(a);--i) #define REPLL(i, n) for(ll i=0;i<ll(n);++i) #define RREPLL(i, n) for(ll i=ll(n)-1;i>=0;--i) #define PAIR std::pair<int, int> #define PAIRLL std::pair<ll, ll> #define IN(a, x, b) (a<=x && x<b) #define SHOW(d) {std::cout << #d << "\t:" << d << "\t";} #define SHOWVECTOR(v) {std::cout << #v << "\t:";for(const auto& i : v){std::cout << i << " ";}std::cout << "\n";} #define SHOWVECTOR2(v) {std::cout << #v << "\t:\n";for(const auto& i : v){for(const auto& j : i){std::cout << j << " ";}std::cout << "\n";}} #define SHOWPAIRVECTOR2(v) {std::cout << #v << "\t:\n";for(const auto& i : v){for(const auto& j : i){std::cout<<'('<<j.first<<", "<<j.second<<") ";}std::cout << "\n";}} #define SHOWPAIRVECTOR(v) {for(const auto& i:v){std::cout<<'('<<i.first<<", "<<i.second<<") ";}std::cout<<"\n";} #define CHECKTIME(state) {auto start=std::chrono::system_clock::now();state();auto end=std::chrono::system_clock::now();auto res=std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();std::cerr<<"[Time:"<<res<<"ns ("<<res/(1.0e9)<<"s)]\n";} #define SHOWQUEUE(a) {std::queue<decltype(a.front())> tmp(a);std::cout << #a << "\t:";for(int i=0; i<static_cast<int>(a.size()); ++i){std::cout << tmp.front() << "\n";tmp.pop();}std::cout << "\n";} #define CHMAX(a, b) a = (((a)<(b)) ? (b) : (a)) #define CHMIN(a, b) a = (((a)>(b)) ? (b) : (a)) //#define int ll using ll = long long; using ull = unsigned long long; constexpr int INFINT = 1 << 30; constexpr ll INFLL = 1LL << 60; constexpr double EPS = 0.0000000001; constexpr int MOD = 1000000007; signed main() { INIT; VAR(int, n, d); VEC(int, p, n); int ans = 0; REP(i, n) { ans += std::max(0, p[i] - d); } if (ans == 0) { OUT("kusoge")BR; } else { OUT(ans)BR; } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<iostream> using namespace std; int main() { int n, d, a, sum = 0; cin >> n >> d; for( int i = 0; i < n; i++ ) { cin >> a; a < d ? : sum += a - d; } if( sum == 0 ) cout << "kusoge" << endl; else cout << sum << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <iostream> #include <algorithm> #define MAX_N 1000 using namespace std; int N,d; int main(void){ int res = 0; int x; cin >> N >> d; for(int i=0;i<N;++i){ cin >> x; res += x > d ? x - d : 0; } if(res>0) cout << res << endl; else cout << "kusoge" << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int n,d,a,ans=0; cin>>n>>d; for(int i=0;i<n;i++){ cin>>a; ans+=max(0,a-d); } if(ans==0)cout<<"kusoge"<<endl; else cout<<ans<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <stdio.h> #include <cmath> #include <algorithm> #include <cfloat> #include <stack> #include <queue> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000001 using namespace std; int main(){ int N,d,sum = 0,tmp; scanf("%d %d",&N,&d); for(int i = 0; i < N; i++){ scanf("%d",&tmp); if(tmp > d){ sum += tmp-d; } } if(sum == 0){ printf("kusoge\n"); }else{ printf("%d\n",sum); } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} int main() { int n,d; cin >>n >>d; int a = 0; rep(i,n) { int p; cin >>p; if(p-d>0) a+=p-d; } if(!a) cout << "kusoge" << endl; else cout << a << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) #define ALL(A) A.begin(), A.end() using namespace std; typedef long long ll; typedef pair<int, int> P; int p[1005]; int main() { memset(p, 0, sizeof(p)); ios_base::sync_with_stdio(0); cin.tie(0); int N, D; cin >> N >> D; rep (i, N) cin >> p[i]; int res = 0; rep (i, N) res += (int)max(p[i] - D, 0); if (res == 0){ cout << "kusoge" << endl; }else{ cout << res << endl; } // end if return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> #define rep(i,n) for(int i=0; i<n; i++) using namespace std; int main(){ int n,d,p; int ans=0; cin >> n >> d; vector<int> v; rep(i,n){ cin >> p; if(p>d)v. push_back(p-d); } if(v.empty() == 0) { rep(i,v.size()){ ans += v[i]; } cout << ans << endl; } else cout << "kusoge" << endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n,d; cin >> n >> d; vector<int> p(n); for(int i=0;i<n;i++) cin >> p[i]; sort(p.begin(), p.end()); int ans = 0; for(int i=0;i<n-1;i++){ if(p[i]-d > 0) ans += p[i]-d; } ans += p[n-1]-d; if(ans > 0) cout << ans << endl; else cout << "kusoge" << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include <cstdio> using namespace std; int N, D; int P[1000]; int main(){ scanf("%d %d", &N, &D); int ans = 0; for(int i = 0; i < N; i++){ scanf("%d", &P[i]); if(P[i] > D){ ans += P[i] - D; } } if(ans > 0){ printf("%d\n", ans); }else{ printf("kusoge\n"); } }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int n,d,an=0; cin>>n>>d; for(int i=0,a;i<n;i++){ cin>>a; an+=max(0,a-d); } if(an)cout<<an<<endl; else cout<<"kusoge"<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int N,d,p[1000]={},f=0,ans=0; cin>>N>>d; for(int i=0;i<N;i++)cin>>p[i]; for(int i=0;i<N;i++)if(p[i]>d)ans+=p[i]-d; if(ans>0)cout<<ans<<endl; else cout<<"kusoge"<<endl; return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int n,w; cin>>n>>w; int a[n]; int ans = 0; for(int i=0;i<n;i++){ cin>>a[i]; a[i] -= w; if(a[i] > 0) ans += a[i]; } if(ans <= 0) { cout << "kusoge\n"; return 0; } cout << ans << endl; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll; template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; } template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; } int main() { int N; int d; while(~scanf("%d%d", &N, &d)) { vector<int> p(N); for(int i = 0; i < N; ++ i) scanf("%d", &p[i]); int ans = 0; rep(i, N) ans += max(0, p[i] - d); if(ans == 0) puts("kusoge"); else printf("%d\n", ans); } return 0; }
CPP
p01899 Yamanote-line Game
B: Yamanote-line Game-Yamanote-line Game- Bean (?) Knowledge The Yamanote line is convenient. The reason is that you can ride as many laps as time allows just by paying 130 yen. However, if you board with a ticket, you must be careful about the valid time of the ticket. It seems safe with an IC card. Taking advantage of the characteristics of the Yamanote Line, there are people in the suburbs of Tokyo who are swayed by trains and devour their sleep. By the way, the questioner of this question has never done it. It's rare for a station employee to find out, but I don't recommend it because it would be annoying. Problem statement Let's play a game that utilizes the characteristics of the Yamanote Line. Here, for generalization, there are N stations numbered from 1 to N, and the stations are lined up in the order of 1, 2, ..., N, and you can go to any station from each station. Let's consider the Yamanote Line Modoki as a route that can be boarded for d yen. The game follows the rules below. * Select your favorite station as the starting point and board the Yamanote Line Modoki for d yen. * After that, get off at any station and get on again. * The game ends when you get off at the station where you started. * If you get off at the i-th station, you can get a reward of p_i yen. * However, you cannot get the reward again at the station where you got the reward once. * Also, it costs d yen to get on the Yamanote Line Modoki again after getting off. Now, how many yen can you make up to? Please give it a try. Input format N d p_1 p_2… p_N All inputs consist of integers. On the first line, the number N of Modoki stations on the Yamanote line and the fare d are given separated by blanks. On the second line, the rewards received at each station are given separated by blanks, and the i-th value p_i represents the reward for station i. Constraint * 3 ≤ N ≤ 1 {,} 000 * 1 ≤ d ≤ 1 {,} 000 * 1 ≤ p_i ≤ 1 {,} 000 (1 ≤ i ≤ N) Output format Output the maximum amount of money you can get in the game on one line. If you can't get more than 1 yen, print "kusoge" on one line. The end of the output should be newline and should not contain extra characters. Input example 1 5 130 130 170 100 120 140 Output example 1 50 Input example 2 3 100 100 90 65 Output example 2 kusoge Input example 3 6 210 300 270 400 330 250 370 Output example 3 660 Input example 4 4 540 100 460 320 280 Output example 4 kusoge Example Input 5 130 130 170 100 120 140 Output 50
7
0
import java.util.Scanner; public class Main { Scanner cin; int N,d; public void solve() { cin = new Scanner(System.in); N = cin.nextInt(); d = cin.nextInt(); int sum = 0; for(int i = 0;i < N;i++){ int p = cin.nextInt() - d; sum += Math.max(0, p); } System.out.println(sum == 0 ? "kusoge" : sum); } public static void main(String[] args) { new Main().solve(); } }
JAVA