Common BoardI did Dijkstra algorithm for S to F, but seems to exceeds time on test #3 ? Anyone got a clue? Tnx in advance Well, I do not know what this test is, but the answer for this test is "No solution". I don't know how to solve this problem with Dijkstra algorithm. My approach is another. Perhaps one pair of tests below can help you to understand the problem more clearely: 7 7 1 2 10 2 3 10 3 4 5 1 5 1 5 6 50 6 7 50 7 3 50 1 4 ans: 156 7 7 1 2 10 2 3 10 3 4 5 1 5 50 5 6 50 6 7 50 7 3 50 1 4 ans: 205 What you need to do is to find the path with maximum weights of all edges while Dijkstra tries to find minimum. You're solving the wrong problem. This is my code. #include <iostream> #include <vector> using namespace std; int main() { int n,a,k=0; cin>>n; vector <int> v; vector <int> sum; vector <int> index; for(int i=0; i<n; i++) { cin>>a; v.push_back(a); } for(int i=1; i<n-1; i++) { sum.push_back(v[i-1]+v[i]+v[i+1]); index.push_back(i); } int max=sum[0]; for(int i=1; i<n; i++) { if(sum[i]>max) {max=sum[i]; k=i;} } cout<<max<<" "<<index[k]+1; system("pause"); return 0; } If you look carefully, you don't get n sums and indexes, but actually n-2 sums and indexes. just be patient while making precalculation :) Жеванный крот, одна халтура! Мало того что английский перевод кривой как рог барана, так ещё и проверяющая система хромает! Отправляем на проверку: Var s, s1: ansistring; a, b: byte; Begin readln(s, s1); val(s[length(s)], b); End. Получаем вполне обоснованный WA1. Теперь дополним: Var s, s1: ansistring; a, b: byte; Begin readln(s, s1); val(s[length(s)], b); val(s1[length(s1)], a); End. Всё, вселенная дрогнула: "Access violation"... *facepalm* И всё бы ничего, но подобное происходит уже где-то раз пятый (в разных задачах). И да, прочитать string целиком, а затем разрезать - выдаёт ту же ошибку AV: Var s, s1: ansistring; i, a, b: byte; Begin readln(s); for i:=1 to length(s) do if s[i]:=' ' then begin val(s[i-1], b); val(s[length(s)], a); break; end; End. Сделайте уже нормальный ввод!!!!1 Edited by author 01.02.2020 21:32 I recommend you this resource: https://e-maxx.ru/algo/bfsalso you can make a function instead of the lyambda, but I just like lyambdas more. #include <iostream> #include <vector> #include <queue> using namespace std; int main() { int N; cin >> N; vector <vector <int>> g(N); int h; for (int i = 0; i < N; i++) { for (;;) { cin >> h; if (!h) break; g[i].push_back(h - 1); } } int n = g.size(); queue<int> q; vector<bool> used(n); vector<int> d(n); int s; auto bfs = [&](int start) { s = start; q.push(s); used[s] = true; while (!q.empty()) { int v = q.front(); q.pop(); for (size_t i = 0; i<g[v].size(); ++i) { int to = g[v][i]; if (!used[to]) { used[to] = true; q.push(to); d[to] = d[v] + 1; } } } }; for (int start = 0; start < n; ++start) if (!used[start]) { bfs(start); } vector <int> ans; for (int i = 0; i < n; i++) { if (d[i] % 2 == 0) ans.push_back(i + 1); } int size = ans.size(); cout << size << '\n'; for (int i = 0; i < size; i++) cout << ans[i] << " "; } Edited by author 26.01.2019 22:53 Edited by author 30.01.2019 03:16 Edited by author 01.02.2020 21:31 using System; using System.Linq; namespace Csharp_1567 { class Program { static void Main(string[] args) { string str1 = Console.ReadLine(); uint sum = 0; int lin = (from n in str1 select n).Count(); for (int i =0;i<lin;i++) { if (str1[i] == 'a' || str1[i] == 'd' || str1[i] == 'g' || str1[i] == 'j' || str1[i] == 'm' || str1[i] == 'p' || str1[i] == 's' || str1[i] == 'v' || str1[i] == 'y' || str1[i] == '_' || str1[i] == '.' || str1[i] == ' ') { sum += 1; continue; } else if (str1[i] == 'b' || str1[i] == 'e' || str1[i] == 'h' || str1[i] == 'k' || str1[i] == 'n' || str1[i] == 'q' || str1[i] == 't' || str1[i] == 'w' || str1[i] == 'z' || str1[i] == ',') { sum += 2; continue; } else sum += 3; } Console.WriteLine(sum); } } } I got WA 1, but when I test this code myself, I always get a correct answer. What's wrong? #include <iostream> using namespace std; int main() { char speech[1001]; cin.getline(speech, 1001); int cost = 0; for (int i = 0; i < 1001; i++) { switch (speech[i]) { case 'a': case 'd': case 'g': case 'j': case 'm': case 'p': case 's': case 'v': case 'y': case '.': case ' ': cost += 1; break; case 'b': case 'e': case 'h': case 'k': case 'n': case 'q': case 't': case 'w': case 'z': case ',': cost += 2; break; case 'c': case 'f': case 'i': case 'l': case 'o': case 'r': case 'u': case 'x': case '!': cost += 3; break;
} } cout << cost << endl;
return 0; } Edited by author 29.08.2018 19:40 why are you iterating to 1001 all the time? maybe to string length? though looks fine with case, but nevertheless worth trying I didn't do anything for 'arbitary' sequence. But my solution got accepted.Can anybody help me clear this doubt? Edited by author 22.01.2019 18:38 Edited by author 09.03.2021 22:26 10 6 1 2 even 1 1 even 3 4 odd 5 6 even 1 6 even 7 10 odd i think the answer of this data should be 1,but my program answer is 4 but it return an accepted! why?i think 4 is correct. "1 2 even" -> first 2 digits are 00 or 11 "1 1 even" -> first digit is 0. So if first 2 digits are 00, everything is ok sort(a + 1, a + 1 + n); reverse(a + 1, a + 1 + n); for(int i = 1; i <= n; i++){ if(sum1 > sum2){ sum2 += a[i]; } else{ sum1 += a[i]; } } cout << abs(sum1 - sum2); let me give you an example 13 14 27 if I understand your solution you are sorting it and then add to left or to the right. in this example the diff is 0 what your program returns? The same thing. I tried to enter almost everything, it works correctly, but i always have WA on 5 test Test Case : 5 5 5 4 3 3 2 Test Case : 5 5 5 4 3 3 Edited by author 15.01.2019 16:49 Edited by author 06.11.2018 20:49 If you write on c++ you can do this: char dot; unsigned int x,ip; ip = 0; for (int i = 24; i >= 8; i -= 8) { cin >> x >> dot; x <<= i; ip |= x; } cin >> x; ip |= x; ------------------ Now you have ip, same you can read mask. Edited by author 21.01.2019 14:50 Edited by author 21.01.2019 14:52 На Паскале не работает код если начать его так: read(n); read(s); (или readln(s)) n - longint, s - string Но если так: readln(s); А затем разрезать на n и s1, то всё ОК. Разве так и должно быть? #include <iostream> using namespace std; long long a,b,t,c,d,k,s,f; int main () { c = 1; k = 0; cin >>t; for (t;t>=1;--t) { cin >>a; cin >>b; if ( b%a==0) { for( ; d != b; ++c ) { d = a*c; if ( b % d == 0) { k= k+1; a = d; c = 1; } } cout << k<<"\n"; } else cout << "0\n"; c=1; k=0; } return 0;
} Excuse me but your algo is wrong. input: 5 1 1000000000 1 1000000000 1 1000000000 2 10 2 10 your answer: 19 0 0 2 0 right anse: 19 19 19 2 2 Check it up!.. Press any key to continue . . . Hi there! I'm curious regarding submission metrics time and memory. Is this the avg time and avg memory of all the tests or maximum time and maximum memory used in the worst test? Can anybody please reveal how does the system count time and memory for different languages? I would like to reproduce it locally before the submission. I use golang. Thanks |
|