Common Board#include <iostream> using namespace std; int main() { int a,b; cin >>a; if (a>=1){ for (int i=1;i<=a;i++){ b=b+i; } } else if (a<1){ for (int i=1;i>=a;i--){ b=b+i; } } cout<<b; } i tested with test case #1 it work, but after submit the judge says wrong answer test 1, why? b initial value is? Do you use gcc? Probably "-Wuninitialized" option can help. Make sure that: Ki (the second input line) is given in reverse order: eg. Kn, K(n-1), ... K2, K1. But, Ci (the third input line) is not: eg. C1, C2, ..., Cn. In the sample test, this will not make you sence... This is not declared in statement. Admin: will you add this? Edited by author 02.10.2010 21:20 Edited by author 02.10.2010 21:20 No, Ki are given in the correct order. 8m3edk-w4h74s:1:9: fatal error: 'iostream.h' file not found #include<iostream.h> ^ 1 error generated. #include <iostream> Also better use not iostream, but rather STDIO. Just because? My opinion: C++ streams and C printf/scanf are equal. For GCC compiler, "std::ios::sync_with_stdio(false);" method call is required at main() very beginning. Significant input speed-up can be reached by using "getchar()" and/or "read()" functions and manual string->int conversion. This way is tricky, dangerous, I haven't seen task requires it. Edited by author 07.06.2017 14:49 Is first test from problem description? I can't find any issue Find maximal matching using Kuhn algo. Then greedily add missing connections. That is slow way. My C++ solution without any STL use runs in 230-240 ms. I found how to speed it up. Just use any stupid greedy algorithm to build some initial matching. After that apply Kuhn algo. Running time dramatically dropped to 31 ms in my case. Edited by author 05.06.2017 00:17 #include <iostream> #include <iomanip> #include <cmath> using std::cin; using std::cout; using std::endl; using std::setprecision; using std::fixed; int main() { int n; double r,s=0,x,y,x0,y0,xn,yn; cin >> n >> r; if (n==1) { cin >> x >> y; } if (n>1) {
cin >> x >> y; for (int i=1;i<=n-1;i++) { cin >> x0 >> y0; s=s+sqrt((x0-x)*(x0-x)+(y0-y)*(y0-y)); x=x0;y=y0; } s=s+sqrt((xn-x)*(xn-x)+(yn-y)*(yn-y)); } s=s+2*3.1415*r; cout << fixed << setprecision(2) << s << endl; return 0; } DEV-C++ 5.11 Edited by author 04.06.2017 12:53 Thank you. I feel very sorry for such a stupid mistake... any hints plzzzz Lamest O(2^K) is enough here. Act greedy. At each iteration select such team X, that if X in contest, then the number of teams can't attend contest is minimal. Remove such teams that have common members with X from the list. Can someone tell me what the test#13 example please? Something like AB and CD are orthogonal and complanar and there is no right such point E, that E lies on AB-line, E lies on CD line and the angle AED is ninety degrees. Я долго бился с этой проблемой несмотря на то,что сложность <200. Я пытался решить с помощью ДП,но там очень много случаев и сложная реализация. Задачу можно решить логически: во-первых,первые символы "<" и последние символы ">" ни на что не влияют,поэтому удалим их; во-вторых,если слева есть хоть один ">",то он в ЛЮБОМ случае будет меняться с КАЖДЫМ "<" РОВНО один раз(если "<" существует). Т.е. нас не интересует конструкция строки. Нас интересуют числа Z[1],E[1],Z[2],E[2],...Z[p],E[p],где Z[i] -количество ">" в последовательности 00000000...00000,а E[i]- количество "<" в последовательности 11111....11111111. Т.е. нашу строку делаем в виде 00...0011..1100..0011..11........00..0011..11,где P чередований (я ">" заменил на "0",а "<" на "1") . Тогда ответ есть Z[1]*(E[1]+...E[p])+Z[2]*(E[2]+..+E[p])+...+Z[p]*E[p]. Сумму можно посчитать через частичные с помощью ДП. Асимптотика O(N). madness :D Imho, it can be solved by easier way. Let's create the array, where put 0 instead of '<' and 1 instead of '>'. Then answer is number of swaps in bubblesorting of this array. I went exactly this way and my solution looks very easy! >><<>< 110010 | counter = 0 Now we go from the right and move every 1 all the way to the right and count the moves (we don't need to actually move anything but you can see everything better this way) 110001 | counter = 1[4->5] 100011 | counter = 1 + 3[1->4] 000111 | counter = 1 + 3 + 3[0->3] = 7 I got AC with this O(n) Thank all of you) Very helpful! Get my AC using this solution Moving? Overkill. Just count how many inversions are there. I don't remember exactly, which symbol, '<' or '>', should be considered as 1 and which as 0. Don't even try to memorize the sequence. Just have a counter and increment it each time when encountered 1 and add counter value to answer each time when encountered 0. The problem is quite evil. The problem is actually very easy. It is hard to understand that the problem is that easy. Possible solution. First , eliminate all such segments I=[l;r], that l>M or r<0, because of such I's can't appear in the answer. Second, build your dp[]. Your dp[x] contains rightmost right end of all such segments [l;r], that l<=x. Finally, just start from segment whose right end is dp[0], jump to a segment whose right end is dp[dp[0]] and so on. How to build dp[x]? I have used an additional array right_end[y]. Where right_end[y] is the right end of a segment with maximal length from all segments whose left end is y. dp[x] =max(dp[x-1], right_end[x]). What does it mean? Runtime error (access violation) This is my code on c++ #include <iostream> using namespace std; int n,ans; string s; int main() { cin>>n; ans=n+2; for(int i=0; i<n; i++) { cin>>s; for(int j=0; j<s.size()-3; j++) { if(s[j]=='+' && s[j+1]=='o' && s[j+2]=='n' && s[j+3]=='e') { ans++; break; } } } if(ans==13) ans++; cout<<100*ans; return 0; } Edited by author 31.05.2017 21:40 for(int j=0; j<(int) s.size()-3; j++)
Edited by author 31.05.2017 23:05 Edited by author 31.05.2017 23:05 The problem is "s. size() -3". The type of s. size() is SIZE_T. It is UNSIGNED. So, if s. length <=2,then SIZE_T overflows. The result is a HUGE number. And j goes out of range. "Access violation" means that you are trying to access some memory you are NOT SUPPOSED to access. Edited by author 31.05.2017 23:04 I used the same code Your question is incorrect without code. In C++ 11, there are a lot of new features. Maybe just performance improvement of C++11 over earlier versions gives you AC. Look for example for string class. In C++98, strings are much slower, than modern strings. Your AC code runs in 800ms. Mine just in 240. http://acm.timus.ru/status.aspx?space=1&num=1613&author=201928You can made a lot of optimizations. After those optimizations, you will get no TLE on G++4.9. The answer is either one or zero. The answer is one when all the children tastes are lying on the same straight line. And the best friends are two children with maximal possible distance between their tastes. You can just brute force all possible points on the grid. For every point check whether that point is under attack. So, your grid is just a SZ*SZ square matrix. SZ=200 is accurate enough, and even somewhat smaller values are OK. You can solve it using : (1) canonical straight line formula for perpendicular length ; (2) cosine theorem to know whether your perpendicular length is the answer to the first question or not; (3) Just formula for Euclidian distance and maximum function to answer the second question. For those who couldn't do straightforward O(n^2) with tricks. Think of < as 0 and > as 1. Every time you see >< (10) you replace (swap) it with <> (01). Which means, you stop as soon as you get sorted stuff, 0*1*. Doesn't it look like count of swaps in bubblesort? :) this solution wont pass TL on 15 with small tricks on wont pass 16 I originally read the problem statement as stating that the radius could only be up to 1000 in length. Hence, I was really confused when everyone was saying the problem was really easy. But that nightmare is behind me now. I never figured out how to do it with that kind of restriction of the radius in under 1 second. What would be the optimal algorithm to use for that case? I used the following thing : (1) calculate median_X=sum(X[i] )/n, do the same for Y. (2) Teleport and find max distance from demon to Sandro. Мне кажется, что моя формула верна. Пусть f(n)=кол-во последовательностей длины 2*n,содержащие числа 1,1,2,2,...,n,n, причём любые два соседние числа различны; g(n)=кол-во последовательностей длины 2*n,содержащие числа 1,1,2,2,...,n,n, причём существует только одна пара соседних равных чисел. Тогда f(n)=n*(2*n-2)*f(n-1)+n*g(n-1), g(n)=n*(2*n-1)*f(n-1)+n*g(n-1) => f(n)=g(n)-n*f(n-1) => g(n-1)=f(n-1)+(n-1)*f(n-2) => f(n)=n*((2*n-1)*f(n-1)+n*(n-1)*f(n-2)). f(1)=0, f(2)=2 (mod p). Ответ на задачу f(n-1) mod p. Хотелось бы увидеть ответы на следующие тесты: 5 10000000 6 10000000 7 10000000 Edited by author 29.05.2017 13:01 Я неправильно записал формулу-рассуждения верные. Ошибка в 3 выводе. Нужно так: => f(n)=n*(2*n-1)*f(n-1)+n*(n-1)*f(n-2). |
|