Общий форумI am getting a wrong answer in it. Can anyone please help me? #include<stdio.h> #include<stdlib.h> int main() { int m,n,a[50000],b[50000],i,j,p,c; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d",&b[i]); } for(j=0;j<n;j++) { for(p=0;p<m;p++) { c=a[j]+b[p]; if(c==10000) { printf("YES"); exit(0); } } } printf("NO"); return 0; } in my mind, two loops is not required. Try to use binary search in sorted array. steps: 1.Take from first array the number; 2.1000-[this number] 3.find 1000-[this number] in second array(use binary search) This way, you have only one loop. Edited by author 20.11.2015 23:45 Edited by author 20.11.2015 23:45 import java.util.Scanner; public class Factorials { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String k = in.next(); char a[] = k.toCharArray(); int length = a.length; function(n, length); in.close(); } private static void function(int n, int length) { int t = n % length; int c = (t == 0 ? length : t); int sum = n; for (int i = 1;; i++) { sum = sum * (n - i * length); if ((n - i * length) == c) { System.out.println(sum); break; } } } } If k > n then answer is n. Try "5 !!!!!!" I had exactly the same mistake at the beginning. Check this case: FFF LLL Answer should be 6 (not 5). Hint: In your second check the counter 'j' could be advanced as the result of the first check which is wrong... In seems that in C++ for set-object no operator for standard container inquiry : N-tn element, with log N complexity, but should be. With such operator problem 1439 would be very simple. In reality was necessary to program my own set class based on red-black trees. And N-th element to realize was very simple(hard work was balancing). It,s not well that tree-structures of standard containers are hidden for uses in C++. What did you store in red-black tree? I've tried 4 self-invented algorithms and no of them Accepted (MLE, TLE most common). In standard container's descriptions operation take_N-th_element with O(log N) complexity presence but in Microsoft Visual C++ dosn't. Using Cormen's book I build myself red-black tree -type with last additional operation. My structure stores: left, right,father, color for each node- this is standard, but additional I support(this means O(log N) renewing in each operation) new characteristics: number of sublines for each node- or subtree nodes count. Using this charactiristics and binary search It's not difficalt to build "take_N-th_element" with O(log N) complexity . Edited by author 19.04.2007 19:43 My Code: #include <bits/stdc++.h> using namespace std; int main() { //freopen("in.txt","r", stdin); unsigned long long int n,k,x,j; cin >> n; for(int i=1; i<=n; i++) { cin >> k; x=1; j=0; while(j<k) { j+=x; if(j<k) x++; } x--; if(j-k < x) cout << 0 << " "; else cout << 1 << " "; } cout << endl; return 0; } Edited by author 26.04.2016 00:41 My program passed all tests, which I found here or invented myself, but I have WA4. Can anybody help me find a mistake or give some special tests? I'd be very grateful... -- P.S. Already solved. This test helped me: 5 RGBRB Edited by author 27.04.2016 20:49 Can anyone tell me the "main idea" of test 4 or type test. I got AV. Need some reason where is my mistake (to my mind - arrays, but need help). Thanks. Try to do login or logout while no user is registered yet. It work's properly. Can u give me 1-2 tests? idk, try 100 register operations? 100 logins? 100 logouts? 1 register, 2 logins and 3 logouts? There aren't many possible cases really. I know that there is a lot of variants (arrays). Thanks. I'll try to solve. 1) First of all you have to calculate maximum of tickets the teacher can buy ((amount of money) div (price of one ticket)) 2) Next step you have to find minimum of tickets. You must pick up one banknote with minimum denomination and calculate amount again: in sample test you get 0 1 0 0 0 0 sequence. Then calculate amonut of money again. So, now you can get minimun of tickets ((new amount of money) div (price of one ticket) + 1). 3) So, now you have minimum and maximum of tickets. Just output the number of possible answers in the first line (tMax - tMin + 1) and the variants in ascending order (for i:=tMin to tMax do write(i,' '); Good luck and get AC! this algo get WA on test #13 Edited by author 25.06.2013 23:54 I think you miss one important step: Think hard about how flexible your tickets is. Not just minimum and maximum tickets, otherwise you will still get WA. When I run the code it gives me a runtime error. Why? Python3 for problem 1011. from math import ceil, floor a = float(input()) b = float(input()) if a>b: a, b = b, a num = 1 while ceil((a/100)*num) != floor((b/100)*num): num += 1 print(num) Edited by author 06.05.2016 11:07 This is my code!!! #include <iostream> #include <vector> #include <algorithm> using namespace std; vector <string> str; vector <string> str1; int main() { char k; string s; vector <string> str; vector <string> str1; int n; cin>>n; for(int i=0; i<n; i++) { cin>>s; str.push_back(s); } cin>>k;
for(int i=0; i<n; i++) { if(str[i][0]==k) str1.push_back(str[i]); }
sort(str1.begin(),str1.end()); for(int i=0; i<str.size(); i++) { cout<<str1[i]<<endl; } return 0; } Do you check your code locally, using some C++ IDE or compiler? Why no? You MUST try. You can, using VS or QTCreator: - run task sample using debugger; - catch "vector index out of range" exception/failure; - fix it. Edited by author 23.04.2016 20:06 I check it on c++ compiler and it do right You definitely didn't check. Your code contains mistakes. Your code must fail with some "vector index out of range" error when you run it locally in debug mode. Probably you use "checked" in meaning "compiled without errors". It is wrong meaning. I check it on c++ compiler and it do right Can somebody give me some test with corect correct output, cause it is rather hard to calculate answer on paper. AC now, however good test will be good for other solvers. Edited by author 28.10.2007 01:48 Edited by author 23.04.2016 19:08 #include <iostream> #include <cstring> #include <string> #include <cmath> using namespace std; int shkaf(string name) { string mas1[9] = { "Alice", "Ariel", "Aurora", "Phil", "Peter", "Olaf", "Phoebus", "Ralph", "Robin" }; string mas2[9] = {"Bambi", "Belle", "Bolt","Mulan", "Mowgle", "Mickey", "Silver", "Simba", "Stitch" }; string mas3[9] = { "Dumbo", "Genie", "Jiminy", "Kuzko", "Kida", "Kenai", "Tarzan", "Tiana","Winnie" }; for (int i = 0; i < 9; i++) { if (name == mas1[i])return 1; if (name == mas2[i])return 2; if (name == mas3[i]) return 3; } } int main() { int N, s=0,a1=1,a2=1; string name; cin >> N; for (int i = 0; i < N; i++) { cin>>name; a2=shkaf(name); s += abs(a2 - a1); a1 = a2; } cout << s; return 0; } /* Le Van Manh Hanoi University Of Science And Technology Viet Nam */ #include <iostream> #include <stdio.h> #include <string> using namespace std; int readAddressToValue(){ int v = 0; string a; cin>>a; if( a=="Alice"||a=="Ariel"||a=="Aurora"|| a=="Phil"||a=="Peter"||a=="Olaf"|| a=="Phoebus"||a=="Ralph"||a=="Robin"){ v = 0; }else if( a=="Bambi"||a=="Belle"||a=="Bolt"|| a=="Mulan"||a=="Mowgli"||a=="Mickey"|| a=="Silver"||a=="Simba"||a=="Stitch"){ v = 1; }else{ v = 2; } return v; } int main(){ int address0; int address1; int steps = 0; int n; cin>>n;
address0 = readAddressToValue(); steps+= address0 - 0; for(int i=1;i<n;i++){ address1 = readAddressToValue(); steps += abs(address1 - address0); address0 = address1; } cout<<steps; return 0; } Edited by author 20.02.2016 13:17 почему ворчит на abs потому что не подключил <cmath> You just need an array with 100 elements so in every position M, you add the ID using a dynamic structure such as "vector" in C++. (Theoretical complexity O(n)) Now you go from 100 to 0 and then print every element from each position M. Edited by author 23.04.2016 15:06 if you have a better algo please tell to me. plz help!!!!!!!!!!!!!!! The data is too weak, I use O(n^3) got an AC in 0.097ms~~ I use O(N^3) and got AC in 0.156 too It's not the problem of tests program which does exacltly n^3 operations work for 0.28, so why you hink tests are weak? #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N,M,a,b; vector <int> t; vector <int> s; vector <int> st; cin>>N; for(int i=0; i<N; i++) { cin>>a; t.push_back(a); } cin>>M; for(int i=0; i<M; i++) { cin>>b; s.push_back(b); } for(int i=0; i<M; i++) { for(int j=0; j<N; j++) { if(s[i]==t[j]) st.push_back(s[i]); } } sort(st.begin(),st.end()); cout<<st.size(); return 0; } "Professor's list is sorted in non-descending order", so duplicates allowed. You are pushing s[i] into out not once but as many times as equal numbers found in t. Break internal cycle when first equality found. But. After I fixed your code I received TLE 8 because of "N*M" complexity. You must optimize perfomance (and should - memory usage). Edited by author 20.04.2016 13:21 Edited by author 20.04.2016 13:21 but what can i do to solve TLE8 C.O. thinks you should optimize your program. 1) Remove s and st arrays. You can - read students value, check it, increase counter if necessary. 2) Speed up checking if date is in professor dates. You can use binary search over sorted array, std::set, std::unordered_set for example. Now I have this code but I have compilation eror why? #include <iostream> #include <vector> #include <set> using namespace std; int main() { int N,M,a,b,counter=0; vector <int> t; set <int> s; cin>>N; for(int i=0; i<N; i++) { cin>>a; t.push_back(a); } cin>>M; for(int i=0; i<M; i++) { cin>>b; s.insert(b); } for(int i=0; i<s.size(); i++) { for(int j=0; j<t.size(); j++) { if(s[i])==t[j]) {counter++; break;} } }
cout<<counter; return 0; } import java.util.Scanner; public class SMSspam2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s =in.nextLine(); char a[] = s.toCharArray();
int count = 0; for(int i=0;i<s.length();i++){ count = count+getCount(a[i]); } System.out.println(count); in.close(); } private static int getCount(char a) { int count =0; if (a == 'a' || a == 'd' || a == 'g' || a== 'j' || a== 'm' || a == 'p' || a == 's' || a== 'v' || a == 'y' || a== '.' || a== ' ') { count++; } if (a== 'b' || a == 'e' || a== 'h' || a == 'k' || a == 'n' || a == 'q' || a== 't' || a== 'w' || a== 'z' || a == ',') { count = count + 2; } if (a== 'c' || a== 'f' || a == 'i' || a == 'l' || a== 'o' || a== 'r' || a == 'u' || a== 'x' || a == '!') { count = count+ 3; } return count; } } |
|