Общий форумpackage timus; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Scanner; public class p1016_ { static Integer sx, sy, ex, ey;
public static void main(String[] args) { InputStream is = System.in; Scanner sc = new Scanner(new InputStreamReader(is)); String S = sc.next(); char[] s = S.toCharArray(); String E = sc.next(); char[] e = E.toCharArray(); sx = s[0]-'a'; sy = 8 - (s[1]-'0'); ex = e[0]-'a'; ey = 8 - (e[1]-'0'); int a[] = new int[6]; for (int i=0; i<6; i++) { a[i] = sc.nextInt(); } for (int i=0; i<24; i++) { int b[] = R[i]; bottoms[i] = a[b[4]]; } //======= for (int r=0; r<24; r++) { int next[] = moves[r]; long costToMove[] = new long[4]; for (int i=0; i<4; i++) { costToMove[i] = bottoms[next[i]]; } for (int y=0; y<8; y++) for (int x=0; x<8; x++) { totalCost_TO_YX24[y][x][r] = MAX; //l r u d for (int i=0; i<4; i++) { costYX24lrud[y][x][r][i] = MAX; } if (x>0) costYX24lrud[y][x][r][0] = costToMove[0]; if (x<7) costYX24lrud[y][x][r][1] = costToMove[1]; if (y>0) costYX24lrud[y][x][r][2] = costToMove[2]; if (y<7) costYX24lrud[y][x][r][3] = costToMove[3]; } } LinkedList<Integer> X = new LinkedList<Integer>(); LinkedList<Integer> Y = new LinkedList<Integer>(); LinkedList<Integer> St = new LinkedList<Integer>(); LinkedList<Integer> Dir = new LinkedList<Integer>(); totalCost_TO_YX24[sy][sx][0] = bottoms[0];
LinkedList<Integer> X0 = new LinkedList<Integer>(); LinkedList<Integer> Y0 = new LinkedList<Integer>(); LinkedList<Integer> St0 = new LinkedList<Integer>(); X0.add(sx); Y0.add(sy); St0.add(0);
while( !X0.isEmpty() ) { Integer x = X0.removeFirst(); Integer y = Y0.removeFirst(); Integer st = St0.removeFirst(); if (x>0) {X.add(x-1); Y.add(y); St.add(moves[st][0]); Dir.add(0);} if (x<7) {X.add(x+1); Y.add(y); St.add(moves[st][1]); Dir.add(1);} if (y>0) {X.add(x); Y.add(y-1); St.add(moves[st][2]); Dir.add(2);} if (y<7) {X.add(x); Y.add(y+1); St.add(moves[st][3]); Dir.add(3);} while ( !X.isEmpty() ) { Integer x1 = X.removeFirst(); Integer y1 = Y.removeFirst(); Integer st1 = St.removeFirst(); int dir1 = Dir.removeFirst(); long cost1 = costYX24lrud[y][x][st][dir1] + totalCost_TO_YX24[y][x][st]; if (totalCost_TO_YX24[y1][x1][st1]>cost1) { totalCost_TO_YX24[y1][x1][st1] = cost1; prevDir[y1][x1][st1] = dir1; prevSt[y1][x1][st1] = st; X0.add(x1); Y0.add(y1); St0.add(st1); } } } long min = MAX; int st = -1; for (int i=0; i<24; i++) { if (min > totalCost_TO_YX24[ey][ex][i]) { min = totalCost_TO_YX24[ey][ex][i]; st = i; } } //PRINT int x = ex; int y = ey; LinkedList<String> route = new LinkedList<String>(); while(x != sx || y!= sy || st!=0) { route.add(0, tok(x,y)); int dir = prevDir[y][x][st]; st = prevSt[y][x][st]; x += xS[reverse[dir]]; y += yS[reverse[dir]]; } route.add(0,tok(sx,sy)); System.out.print(min); for (String mv: route) System.out.print(" " + mv); }//lrud reversed RLDU static int prevDir[][][] = new int[8][8][24]; static int prevSt[][][] = new int[8][8][24]; static long[][][][] costYX24lrud = new long[8][8][24][4]; static long[][][] totalCost_TO_YX24 = new long[8][8][24]; static String tok(int x, int y){ char ah = (char) ('a'+x); char oe = (char) ('1'+(7-y)); return ah+""+oe; } static long MAX = Long.MAX_VALUE/3; static int xS[] = new int[]{-1,+1,0,0}; static int yS[] = new int[]{0,0,-1,+1}; static int reverse[] = new int[]{1,0,3,2}; //======================================================== static int R[][] = new int[][]{ //front, back, up, right, down, left {0,1, 2,3,4,5}, {0,1, 5,2,3,4}, {0,1, 4,5,2,3}, {0,1, 3,4,5,2}, {1,0, 2,5,4,3}, {1,0, 3,2,5,4}, {1,0, 4,3,2,5}, {1,0, 5,4,3,2}, {3,5, 0,2,1,4}, {3,5, 4,0,2,1}, {3,5, 1,4,0,2}, {3,5, 2,1,4,0}, {5,3, 0,4,1,2}, {5,3, 4,1,2,0}, {5,3, 1,2,0,4}, {5,3, 2,0,4,1}, {4,2, 1,5,0,3}, {4,2, 3,1,5,0}, {4,2, 0,3,1,5}, {4,2, 5,0,3,1}, {2,4, 1,3,0,5}, {2,4, 3,0,5,1}, {2,4, 0,5,1,3}, {2,4, 5,1,3,0}, }; //l r u d static int moves[][] = new int[][] { {3, 1, 18, 20}, {0, 2, 8, 14}, {1, 3, 22, 16}, {2, 0, 12, 10}, {7, 5, 16, 22}, {4, 6, 14, 8}, {5, 7, 20, 18}, {6, 4, 10, 12}, {11, 9, 5, 1}, {8, 10, 21, 19}, {9, 11, 3, 7}, {10, 8, 17, 23}, {13, 15, 7, 3}, {14, 12, 23, 17}, {15, 13, 1, 5}, {12, 14, 19, 21}, {19, 17, 2, 4}, {16, 18, 13, 11}, {17, 19, 6, 0}, {18, 16, 9, 15}, {21, 23, 0, 6}, {22, 20, 15, 9}, {23, 21, 4, 2}, {20, 22, 11, 13}, };
static int bottoms[] = new int [24]; } Please help. Anyone knows about the test? Edited by author 04.03.2014 21:46 Edited by author 05.03.2014 10:38 use bitmask. i got ac at 0.093 var m, n, b, y, k, x: integer; function St(x, n: integer): integer; var i, f: integer; begin f := 1; for i := 1 to n do f := f * x; St := f; end; begin readln(n, m, y); for x := 0 to m - 1 do begin b := st(x,n); if (b mod m) = y then begin write(x,' '); k := k + 1; end; end; if k = 0 then writeln(-1); end. #include <iostream> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(int argc, char *argv[]) { int k,n,i; cin>>k>>n; int y[n+1]; for (i=1;i<=n;++i) { cin>>y[i]; } for (i=1;i<=n;++i) { if (y[i]>k) {y[i+1]+=y[i]-k;} else {y[i]=0;} } cout<<y[n+1]<<endl; return 0; } My AC program gives wrong answer on this test: ?????xy????? xyxy My answer: 3 Right answer: 4 Hello, I've implemented two solutions: DP and Brut Force on ruby, but still no luck to pass tests (#3, #4). Does someone have good solution on ruby for that problem? Is it my bad hands, or tests were changed and ruby cant process it fast. If you have accepted solution, please try to submit it again. Also, if you resolved it on ruby, please give some specific hints about solution. Thanks! I don't know much about Ruby but I've solved this problem on Python which I suppose has similar performance. It's very important to optimize algorithm very well in order to pass all tests on those script languages. Time your code and try not to exceed 1 sec. limit with 20 stones. Sorry, no hints about algorithm from me. If you are not understand problem: when print undefined and when error, try to brute force it:) Why I always WA on text 2? If you sort points by angle from Wally's home, there is one tricky case - when angle difference between two sequential points is greater than PI. For example if angle range is [-PI; PI]: 0 0 3 -1 -1 1 0 1 2 -1 1 3 Program that sort angles will output 0 1 2 3 0 and it is wrong, 1-2 will intersect with 3-0. Text for angle range [0; 2PI] is 0 0 3 1 1 1 0 -1 2 1 -1 3 Again sorting program will output 0 1 2 3 0 and again it is wrong, 1-2 will intersect with 3-0. Hint: after sorting there may be not more than one pair of sequential points with angle difference greater than PI. I tried a number of tests, and neither of p,q is very big(Many of them beyond 1000). Maybe there's some mathematical property of the f function? can someone tell me the test case 4 for this question. kindly help I thihk Test4 is about "Combined" int GetCost() const { return abon + (totalMinut > minutLimit ? totalMinut - minutLimit : 0) * minutCost; } import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class p2015_11_17_DZnaOlympSSAU_Timus1020_Thread implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } public static void main(String[] args) { new Thread(null, new p2015_11_17_DZnaOlympSSAU_Timus1020_Thread(), "", 256 * (1L << 20)).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run() { try { timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } void solve() throws IOException { int numberOfNails=readInt(); double radiusOfNails=readDouble(); if (numberOfNails==1){ out.println(2*PI*radiusOfNails); } else { double xFirstNail = readDouble(); double yFirstNail = readDouble(); double xLastNail=xFirstNail; double yLastNail=yFirstNail; double xOfTheCurrentNail=xFirstNail; double yOfTheCurrentNail=yFirstNail; double lengthOfTheThread=0; for (int currentNail = 2; currentNail <= numberOfNails; currentNail++) { xLastNail=xOfTheCurrentNail; yLastNail=yOfTheCurrentNail; xOfTheCurrentNail=readDouble(); yOfTheCurrentNail=readDouble(); lengthOfTheThread+=(Math.sqrt((xOfTheCurrentNail-xLastNail)*(xOfTheCurrentNail-xLastNail)+(yOfTheCurrentNail-yLastNail)*(yOfTheCurrentNail-yLastNail))); } lengthOfTheThread+=(Math.sqrt((xFirstNail-xOfTheCurrentNail)*(xFirstNail-xOfTheCurrentNail)+(yFirstNail-yOfTheCurrentNail)*(yFirstNail-yOfTheCurrentNail))); lengthOfTheThread+=(2*PI*radiusOfNails); System.out.println( String.format("%.2f", lengthOfTheThread) ); } } } I found a problem. When n==1, program print (2*PI*radiusOfNails), but must ("%.2f",2*PI*radiusOfNails) // del Edited by author 26.11.2015 22:08 There was an error with data input using StreamTokenizer, so be careful if you are using it. Edited by author 05.04.2008 21:15 Edited by author 06.04.2008 04:02 Thank you! Scanner() got AC. int main() { int n; cin>>n; int i; for(i=0;i<n/2;i++) cout<<i+1<<" "<<n-i<<" "; if(n%2!=0) cout<<((int)n/2)+1<<" "; cout<<endl; cout<<"1 "; for(i=3;i<n;i+=2) cout<<i<<" "; int p=n; if(p%2!=0) { p-=1; cout<<n<<" "; } for(p=p;p>1;p-=2) cout<<p<<" "; return 0; } input1: 1651 1654654564 output1: 1043406233 intput2: 77251 78156464666454 output2: 37919407177871 intput3: 1 999999999999 output3: 537412247190 input4: 101231 1000000000000000000 output4: 391773528202165271 input5: 123456789 987654321 output5: 567000597 input6: 202250020115 3000200002203254 output6: 1339741370499750 O(n^3) dp with optimizations gets AC in 0.38 sec. Probably there is some asymptotically faster solution. #include <iostream> using namespace std; int main() { char first[4] = { 'A', 'P', 'O', 'R' }; char second[3] = { 'B', 'M', 'S'}; int mail = 0; bool AF=false;//already find char *input=new char[200]; int steps=0,duck=1; cin >> mail; for (int i = 0; i < mail; i++) { cin >> input; for (int j = 0; j < 4;j++) { if (input[0] == first[j]&&!AF) { AF = true; if (duck == 1)break; if (duck == 2)steps++; if (duck == 3)steps += 2; duck = 1; break; } } if (!AF){ for (int j = 0; j < 3; j++) { if (input[0] == second[j] && !AF) { AF = true; if (duck == 2)break; else steps++; duck = 2; break; } } } if (!AF) { AF = true; if (duck == 3)break; if (duck == 2)steps++; if (duck == 1)steps += 2; duck = 3; } AF = false;
} cout << steps; return 0; } Edited by author 17.11.2015 11:26 I found a solution to my problem was laziness. When I described the search for the third block - the answer was accepted as correct. Apparently in the verification test on the 8th provide those names are not on the shelves. Edited by author 17.11.2015 11:35 Try test 8 3 Answer may be O-O-O-O-O-O-O-O |\ \| | | |/ /| O-O O O O O O-O |/ / /| |\ \ \| O-O-O-O-O-O-O-O have a theotical solution? (i.e. with searching or something like..). No, try test 3 8 First time I failed at this test but I fixed this Pay attention that the algorithm is used only the function has some specific property. But unfortunately, the property does not always exist. So why do you take the algorithm for granted? If I want to type name "de", and both names "dd" and "de" are exist in contacts, how much seconds it will take? Should i wait 1 second after first pressing 'd'-key? Statement doesn't say anything about waiting, it only says "1 push = 1 second", so i guess only this should be taken into account. ok, then I could mean both ways of pressing key 'def' three times: "ed" or "de" (if this names are in list) Edited by author 14.11.2015 18:13 I think so! I'll have a bit of time in 7 hours, so i'll try to solve this task by then to see if i'm right (but i think, most likely i am) Hmm, i started to try to implement it just now and that made me wonder... If we have test data like 53 wwa wwb ... wwz wy wza wzb ... wzz And say, we want to reach "wy" (or anything after it for that matter), and i wonder what happens... 1) we press 9, then press 9 thrice and reach it, or 2) we press 9, then press 9 twice, and at that point it occurs there's nothing starting with "wx", and the 2nd letter is removed? Well i guess i'll follow 2) for now, but that feels somewhat suboptimal for real phones if we talk about it... Edit: alright, nevermind... seems i was wrong, should've looked into neighbouring topics initially. Gotta redo it... Edited by author 16.11.2015 07:20 I got problem in test#1. please help Edited by author 15.11.2015 23:24 |
|