Общий форум 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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace phrase { class Program { static int First(string input, string[]array) { int col = Convert.ToInt32(input.Length); if (col>0&&col<=1000) { int countArr = 0,countStr = 0,c = Convert.ToInt32(array.Length),count = 0,indexStr=0; for (; countArr < c; countArr++) { for (; (indexStr = input.IndexOf(array[countArr], countStr)) !=-1; countStr = indexStr + 1) { count++; } countStr = 0; } return count; } return 0; } static void Main(string[] args) { string[] one = new string[] { "a", "d", "g", "j", "m", "p", "s", "v", "y", ".", " " }; string[] two= new string[] { "b", "e", "h", "k", "n", "q", "t", "w", "z", "," }; string[] three = new string[] { "c", "f", "i", "l", "o", "r", "u", "x", "!" }; string input = Console.ReadLine(); int a = First(input,one); int b = First(input,two); int c = First(input,three); Console.WriteLine(a+(b*2)+(c*3)); } } } var i,n,l,j:longword; found:boolean; begin readln(n); repeat found:=False; for j:=3 to n div 2 do begin if n mod j=0 then begin found:=True; break; end; end; if found then n:=j; until not found; writeln(n-1); readln; end. Please help me to optimize my code. It's 0.405 sec for j := 3 to sqrt(n) do will help you :) > for j := 3 to sqrt(n) do will help you :) will it? for sqrt(8) = 2.8, but answer should be 4 - 1 = 3 #include<stdio.h> #include<stdlib.h> #define x 100000 int main() { long long int n , c=0,i; double ar[x] = {}; while(scanf("%lld",&n)){ if(n < 0) break; ar[c++] = n; } for(i=c-1; 0<=i; i--){ printf("%.4lf\n",sqrt(ar[i])); } return 0; } var
i, n: integer; c : real; begin read(n); c := 0; if n > 0 then begin for i := 1 to n do c := n; write(c); end; if n <= 0 then begin for i := 1 downto n + 1 do c := (-((-n) * (1 - n) / 2) + 1); write(c); end; end. Доп. примеры/add. examples: n=-5 -5+(-4)+(-3)+(-2)+(-1)+0+1 n=5 5+4+3+2+1 n=0 0+1 Арифмет. прогрессия/arithm. progression |
|