Общий форум#include<iostream> #include<string> using namespace std; int main() { string box1[3][3] = { "Alice", "Ariel","Aurora", "Phil", "Peter", "Olaf", "Phoebus", "Ralph", "Robin" }; string box2[3][3] = { "Bambi", "Belle", "Bolt", "Mulan", "Mowgli", "Mickey", "Silver", "Simba", "Stitch" }; string box3[3][3] = { "Dumbo", "Genie", "Jiminy", "Kuzko", "Kida", "Kenai", "Tarzan", "Tiana", "Winnie" }; int n, step; string names; bool here1, here2, here3; here2 = here3 = false; here1 = true; step = 0; cin >> n; for (int m = 0; m < n; m++) { cin >> names; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (box1[i][j] == names) { here1 = true; if (here2) { step++; } else if (here3) { step = step + 2; } here2 = false; here3 = false; break; } } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (box2[i][j] == names) { here2 = true; if (here1) { step++; } else if (here3) { step++; } here1 = false; here3 = false; break; } } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (box3[i][j] == names) { here3 = true; if (here2) { step++; } else if (here1) { step = step + 2; } here1 = false; here2 = false; break; } } } } cout << step << endl; return 0; } #include <iostream> #include <string> #include <cmath> int32_t get_post_case_offset(const std::string& receiver) { uint32_t out = 0; switch (receiver[0]){ case 'A': case 'P': case 'O': case 'R': out = 0; break;
case 'B': case 'M': case 'S': out = 1; break; default: out = 2; } return out; } std::string get_receiver(){ std::string out; std::cin >> out; return std::move(out); } int main() { uint32_t n; std::cin >> n; uint32_t steps = 0; int32_t last_offset = 0;
for (;n;n--){ int32_t offset = get_post_case_offset(get_receiver()); steps += abs(last_offset - offset); last_offset = offset; } std::cout << steps << std::endl;
return 0; } Thanks! I was confused about this before seeing your post. You lie. In problem segment is [ai, bi]. I am getting wrong answer.Please give me some test case. My solution: #include<iostream> #include<string.h> #include<string> #include<stdlib.h> #include<stdio.h> #include<vector> #include<algorithm> using namespace std; string line; bool check(int s,int e) { string temp; for(int i=s; i<=e; i++) temp.push_back(line[i]); string temp2=temp; reverse(temp.begin(),temp.end()); if(temp==temp2) return true; return false; } int dp[4005][4005]; int palindrome(int s,int e) { if(s>=e) return 0; if(dp[s][e]!=-1) return dp[s][e]; int ans=(1<<30); for(int i=s; i<e; i++) if(check(s,i)) { ans=min(ans,1+palindrome(i+1,e)); } return dp[s][e]=ans; } vector<string>anss; void print(int s,int e) { int b=palindrome(s,e); if(b==1) { string temp; for(int i=s; i<e; i++) temp.push_back(line[i]); anss.push_back(temp); return; } for(int i=s; i<e; i++) if(b==1+palindrome(i+1,e)) { string temp; for(int j=s;j<=i;j++) temp.push_back(line[j]); anss.push_back(temp); print(i+1,e); break; } } int main() { cin>>line; int len; len=line.size(); anss.clear(); memset(dp,-1,sizeof dp); cout<<palindrome(0,len)<<endl; print(0,len); len=anss.size(); for(int i=0;i<len-1;i++) cout<<anss[i]<<" "; cout<<anss[len-1]<<"\n"; } Ошибка где-то тут: string line; bool check(int s,int e) { string temp; for(int i=s; i<=e; i++) temp.push_back(line[i]); string temp2=temp; reverse(temp.begin(),temp.end()); if(temp==temp2) return true; return false; } int dp[4005][4005]; int palindrome(int s,int e) { if(s>=e) return 0; if(dp[s][e]!=-1) return dp[s][e]; int ans=(1<<30); for(int i=s; i<e; i++) if(check(s,i)) { ans=min(ans,1+palindrome(i+1,e)); } return dp[s][e]=ans; } vector<string>anss; void print(int s,int e) { int b=palindrome(s,e); if(b==1) { string temp; for(int i=s; i<e; i++) temp.push_back(line[i]); anss.push_back(temp); return; } for(int i=s; i<e; i++) if(b==1+palindrome(i+1,e)) { string temp; for(int j=s;j<=i;j++) temp.push_back(line[j]); anss.push_back(temp); print(i+1,e); break; } } int main() { cin>>line; int len; len=line.size(); anss.clear(); memset(dp,-1,sizeof dp); cout<<palindrome(0,len)<<endl; print(0,len); len=anss.size(); for(int i=0;i<len-1;i++) cout<<anss[i]<<" "; cout<<anss[len-1]<<"\n"; } Edited by author 22.08.2018 12:26 Edited by author 22.08.2018 12:26 #include<stdio.h> #include<math.h> int main(){
float i=0,racuad[61]; int a=1,b=1;
while(i<=pow(10,18)){ racuad[a]=sqrt(i); a++; i=1+i*7843; } for(b=a-2;b>=0;b--){ printf("%.4f\n",racuad[b+1]); }
return 0; } Give me test data I had WA #1 as I printed loops. In other words, if my program had to output a path of length K it would print 1 1 1 2 2 2 2 3 ... (K - 1) (K - 1) (K - 1) K Having deleted them, I secured my AC :P This problem bothered me a lot. So I decide to write something about it to help other people to solve it more quickly. --------------------------------------------- 1.unsigned char or getchar()(which returns int) must be used to deal with the input --------------------------------------------- 2.When you're testing the program you write, maybe you will find that your PC can't run correct answer(even your code is right)(at least on my local PC... My accepted code always output 0 be cause char such as ┌ is always regarded as two char(s?). So here is how I check my program. ---------------------------------------------------------- This is the original sample: .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ...........┌─────┐................................ ...........│.....│................................ ....┌──────│.....│..........┌─┐................... ....│......│.....│..........│.│................... ....│......│.....│..........└─┘................... ....│......│....┌│────┐.............┌─┐........... ....│......└─────┘....│.............│.│........... ....│......│....│.....│.............└─┘........... ....│......│....│.....│.........┌──┐.............. ....└──────┘....│.....│.........│..│.............. ................│.....│.........│..│.............. ................└─────┘.........└──┘.............. .................................................. .................................................. And I changed it to the figure below: .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ...........+,,,,,+................................ ...........*.....*................................ ....+,,,,,,*.....*..........+,+................... ....*......*.....*..........*.*................... ....*......*.....*..........+,+................... ....*......*....+*,,,,+.............+,+........... ....*......+,,,,,+....*.............*.*........... ....*......*....*.....*.............+,+........... ....*......*....*.....*.........+,,+.............. ....+,,,,,,+....*.....*.........*..*.............. ................*.....*.........*..*.............. ................+,,,,,+.........+,,+.............. .................................................. .................................................. In this case my local PC gave the right answer. You just need a little change to make it work (i.e. change the ascii) This task almost pisses me off. I was finally accepteed after writing my own input/output via fgetc/fputc. Both stream and printf/scanf lead to TLE on test 42. To be onest i still beleive that stream is faster than printf/scanf... But i really wonder which IO uses those guys/girls who accepted with less than 0.2 or something... i don't think it's only about IO. i suppose they used not linked list (or just list in c++), but array with 2 iterators + didn't copy the data when there are already more then n - #of_moves elements in array/list Try to check some different algos and take WA1, but answeres for 1-10 and 2-5 is correct. P.S. have AC Edited by author 22.08.2018 03:10 It looks like a very simple problem. In pure C, but not in Python! :) 'Time limit exceeded' despite using precomputed tuple of primes (first 100 and each 100th or 50th), Eratosthenes' sieve, optimizations, wheel factorization etc. Time is 2.10-2.30 regardless of method !!! WTF??? On my PC it finds a prime very fast: ----- Enter the number... 14999 14999-th prime number is 163819 Elapsed time is 0.0010540485382080078 ----- On my computer and python implementation (Intel i3, Linux Fedora 27/ CPython 3.6.4) average time per prime is about 1 ms o less. How to solve the problem in this strangely slow Python implementation? Edited by author 10.03.2018 05:38 def prime(n): for i in range(2, n): if n % i == 0: return False return True
k = int(input()) for i in range(k): n = int(input()) count = 0 for f in range(2, 100): if prime(f) == True: count += 1 if count == n: break print(f) I have same problem I found that my program could't deal with ┌┐└┘ correctly. A sample of input is wanted. Please help. Thanks in advance. I haven't solved this problem yet. The table that you see right after "The frame consists of the following characters:" statement tells you how you should read these special characters. I tried to generate the sample input, but not sure if it is correct since these are unprintable characters (atleast with my local PC encoding) and I cannot really verify it. Nevertheless, here is my attempt to generate it. It looks rugged, but that is because of encoding issues. You should probably output it on your own PC with the program below. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ...........ÚÄÄÄÄÄ¿................................ ...........³.....³................................ ....ÚÄÄÄÄÄij.....³..........ÚÄ¿................... ....³......³.....³..........³.³................... ....³......³.....³..........ÀÄÙ................... ....³......³....Ú³ÄÄÄÄ¿.............ÚÄ¿........... ....³......ÀÄÄÄÄÄÙ....³.............³.³........... ....³......³....³.....³.............ÀÄÙ........... ....³......³....³.....³.........ÚÄÄ¿.............. ....ÀÄÄÄÄÄÄÙ....³.....³.........³..³.............. ................³.....³.........³..³.............. ................ÀÄÄÄÄÄÙ.........ÀÄÄÙ.............. .................................................. .................................................. and the program that output it. It reads squares as in output sample and draws a board. struct square { int x, y, a; square() = default; square(int x, int y, int a) : x(x), y(y), a(a) {} }; void solve(std::istream &in, std::ostream &out) { std::vector<square> squares; int n; in >> n; for (int i = 0; i < n; ++i) { int x, y, a; in >> x >> y >> a; squares.emplace_back(x, y, a); } unsigned char board[20][50]; for (int i = 0; i < 20; ++i) { for (int j = 0; j < 50; ++j) { board[i][j] = '.'; } } for (square sq : squares) { board[sq.y][sq.x] = 218; // top-left board[sq.y][sq.x + sq.a - 1] = 191; // top-right board[sq.y + sq.a - 1][sq.x] = 192; // bottom-left board[sq.y + sq.a - 1][sq.x + sq.a - 1] = 217; // bottom-right for (int x = sq.x + 1; x < sq.x + sq.a - 1; ++x) { board[sq.y][x] = 196; // top edge board[sq.y + sq.a - 1][x] = 196; // bottom edge } for (int y = sq.y + 1; y < sq.y + sq.a - 1; ++y) { board[y][sq.x] = 179; // left edge board[y][sq.x + sq.a - 1] = 179; // right edge } } for (int i = 0; i < 20; ++i) { for (int j = 0; j < 50; ++j) { out << board[i][j]; } out << '\n'; } } int main() { solve(std::cin, std::cout); } As for reading it. I think you used char, while you need unsigned char or int for this problem. You can use getchar() that returns int or read into unsigned char s[SIZE]
Edited by author 20.08.2018 20:13 Edited by author 20.08.2018 20:13 Edited by author 20.08.2018 20:13 Edited by author 20.08.2018 20:19 Edited by author 20.08.2018 20:26 Edited by author 20.08.2018 20:34 Thank you for such long explaination! I will try it again. The problem is solved successfully. Thank you very much! I don't understand it well... As the frame defined by the tripple (X, Y, A) will have such vertices: (X, Y) (X, Y + A - 1) (X + A - 1, Y) (X + A - 1, Y + A - 1) Thank you (for answer my question again)!8D The figure given is very "ugly" so that I didn't realize that it's a square! Edited by author 20.08.2018 17:33 (Maybe we can make a friend OvO. My codeforces id is the same as my id here. Thank you for your kind suggestion, but I am not a big fan of such things. Could you answer my another question please? How to read the input?(The problem has been posted) This has really bothered me a lot... The test lasts 8 sec on my PC. But I got AC in 0.1 sec. I want to know if my PC is too slow or my compiler optimization not working. Thanks, Ade input: first line: 5000 2500 second line is : 1500 "1", 2000 "2", 1500 "3" third line is: 2500 1250 output: 14243163 My AC program also gives this result. I don't think the tests for the problem are very hard. Using Haskell my program solves this in about 0.5 secs but gets an AC in .031 secs (which I think is the lowest time you can get now), so they could definitely make the problems harder. I don't know, my program passes this test in 0.001341s. on my local PC. I got AC in 0.015s. The tests for this program are probably just fine. The Haskell program could be compiled without optimizations or someone has a wooden PC. I really don't know why I cant reach optimal time. It's always more than 1 sec. Here's my code: n = int(input()) data = [[] for k in range(101)] for i in range(n): ID, tasks = map(int, input().split()) data[tasks] += [ID] for i in range(100, -1, -1): for j in data[i]: print(j, i) Meanwhile, I tried to reach time less that 1 sec by not using sorting but as I can see it's not working =( Edited by author 20.08.2018 16:29 Is it height or width of frame? Edited by author 27.05.2015 16:11 Do you know what's the test 62? My solution in Python 3 took 0.078 seconds and 516 KB with recursion. Can you do it faster? you can try to to it with stack instead of recursion Can anybody that had WA7 give me some hints? Thanks Maybe that's because you didn't calculate the right shortest-path with the least boots changed(that is, your first number of the output is wrong, but the other one is right). I've got WA7 because I used only one bfs to calculate both values. 2 bfs should be used. (BTW, some of my classmates said only one bfs could also work??? I'm not sure about this. what is case 8 like,i just can't solve it with divede and conquer... I have solved 1009&1012 using dp. My algorithm is O(n). Obviously it can't pass this problem's test. I think maybe there are some ways to make it quicker but I can't figure out. When I was searching in internet for solutions, I found that they are all for 1012. I'm a Junior high school students and my math is poor (for this question) TAT. Can anybody help me?? THx in advance. You should try to look up on the Internet how to find Fibonacci number with matrix exponentiation. The level of material should be accessible to a high school student if you are willing to spend couple of hours getting comfortable with new notations/definitions. There's not much theory behind this. You also need to know how to do fast exponentiation (i.e. in O(lon n) instead of O(n)) and use Long arithmetics (like BigInteger in Java). Thanks! Luckily I have known about knoledge about quickpow. Very helpful segguestion!XD Thank you again. I think I gain a lot after solving this question! I nearly knew nothing about matrix in the past. |
|