Common Boardмогут ли сигареты накладываться друг на друга? например: 0 0 0 2 0 1 0 3 спасибо конечно за совет, но я знаю что эта задача есть и в Кормене и на емаксе. Я просто уточнял могут ли отрезки накладываться po4emu u menia Time Limit Exceeded? ( Edited by author 12.06.2012 08:19 #include <iostream> using namespace std; int main() { int a[4000],a2[4000],a3[4000],i,j,l,n,n2,n3,k=0; cin>>n; for(i=1;i<=n;i++) cin>>a[i]; cin>>n2; for(j=1;j<=n2;j++) cin>>a2[j]; cin>>n3; for(l=1;l<=n3;l++) cin>>a3[l]; for(i=1;i<=n;i++) { for(j=1;j<=n2;j++) { for(l=1;l<=n3;l++) { if(a[i]==a2[j] && a[i]==a3[l]) k++; } } } cout<<k; return 0; } Edited by author 12.06.2012 08:25 Your algorithm has complexity O(N^3) while N = 4000. It is very long. Try to use binary search, then you will find solution with complexity O(N * logN * logN). This problem can be solved by O(N) time... If you have a good hash function, of course And O(N * log N) instead of yours O(N * log N * log N) using map. Edited by author 25.03.2013 22:24 My program(LANG c)is in the following: #include<stdio.h> #include<stdlib.h> #include<cmath> int k[60000],value,i,n; int main() { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&k[i]); for(i=1;i<=n;i++) { if(k[i]==0) printf("0 "); else { value=(int)sqrt((k[i]-1)*2); if((value*(value+1))==2*(k[i]-1)) if(i==n) printf("1\n"); else printf("1 "); else if(i==n) printf("0\n"); else printf("0 "); } } return 0; } BUT it can't compile,said that " d9b0f699-b537-45eb-b286-ce7765096c87d9b0f699-b537-45eb-b286-ce7765096c87(16) : error C2668: 'sqrt' : ambiguous call to overloaded functionS:\checker\compile\vc10\include\math.h(589): could be 'long double sqrt(long double)' S:\checker\compile\vc10\include\math.h(541): or 'float sqrt(float)' S:\checker\compile\vc10\include\math.h(127): or 'double sqrt(double)' while trying to match the argument list '(int)' " BUT in my computer(Windows 7),I can compile and run it,and the answer is right. So I don't know where is wrong?And I don't know how to modify it.Help me,please! Edited by author 22.02.2013 15:09 length array int k[65535] becouse (1 ≤ N ≤ 65535) My program(LANG c)is in the following: ................... int k[60000],value,i,n; ................... value=(int)sqrt((k[i]-1)*2); // sqrt(double((k[i]-1)*2)) ................... Edited by author 22.02.2013 15:09 your wrong is use (int) with sqrt. template for SQRT is sqrt(double). you must to give for sqrt double variable, and if you need int variable, you must to appropriate value in int variable. Signs after a comma will be removed. I am sorry for my bad English. Edited by author 25.03.2013 20:14There is some trash in the end of file. hello, I wrote the code for this problem in python. I sure my logic is right, but do not know why i get a wrong answer. I have pasted my code below. If someone could help me find where the bug is .. it would be of great use #timus 1005 import sys,math class Timus1005: def __init__(self): pass
def minimumDiffrence(self,weights): weights.sort() if (len(weights) == 1): return weights[0]
if (len(weights) == 2): return (weights[1] - weights[0])
weights.reverse()
pile1 = 0 pile2 = 0
for stone in weights: added = False if (pile1 < pile2) and added == False: pile1 = pile1 + stone added = True
if (pile1 > pile2) and added == False: pile2 = pile2 + stone added = True
if (pile1 == pile2) and added == False: pile1 = pile1 + stone added = True
return int(math.fabs(pile1 - pile2))
if __name__ == "__main__": w = [] for line in sys.stdin: for word in line.split(' '): w.append(int(word))
p = Timus1005() print p.minimumDiffrence(w[1:])
My greedy algorithm have 'accepted', but gives wrong answer for this test: 8 a b c d e f g i k b l m l m i i k c d e c e f k The number of ways to tile an MxN rectangle with 1x2 dominos is 2^(M*N/2) times the product of { cos^2(m*pi/(M+1)) + cos^2(n*pi/(N+1)) } ^ (1/4) over all m,n in the range 0<m<M+1, 0<n<N+1. I have found this formula in internet and I would like to know is there a way to use it for solving this problem? There is a solution of this problem which doesn't use this formula. But it uses something related to the problem name :) Don't you mean Aztec Diamond? And how is it connected then? =) Your guess is correct. The connection, however, is quite unobvious. The answer can be calculated as number of perfect matching in corresponding planar graph using Kasteleyn theorem. But it uses determinant of matrix (nm/2)x(mn/2) (symmetric and with zeroes) and complex numbers, so now i don't know, can or cannot it to be applied to this problem. Hm, maybe it gives the formula posted by Al.Cash :-)))) Edited by author 21.06.2009 20:59 Yes, it gives exactly this formula =) But you can't use it due to precision reasons. We need integral-numbers formula... Very interesting from theoretical and practical point of view to use Java.BigDecimal with 1000-10000 digits and catch right answer. Connection with Aztec Diamond! Something interest, but only for (2n)x(2n) squares: Consider the 2n-by-2n matrix View the MathML source with mi,j=1 for i,j satisfying |2i−2n−1|+|2j−2n−1|less-than-or-equals, slant2n and mi,j=0 for all other i,j, consisting of a central diamond of 1's surrounded by 0's. When ngreater-or-equal, slanted4, the λ-determinant of the matrix M (as introduced by Robbins and Rumsey [Adv. Math. 62 (1986) 169–184]) is not well defined. However, if we replace the 0's by t's, we get a matrix whose λ-determinant is well defined and is a polynomial in λ and t. The limit of this polynomial as t→0 is a polynomial in λ whose value at λ=1 is the number of domino-tilings of a 2n-by-2n square. Nice problem! Finally got accepted after trying for 2 hours. best, Josh Bao Edited by author 25.06.2009 12:34 2zbao: I think this gave us no useful information! Edited by author 25.06.2009 15:27 Your submissions looks like some binary search or other cheatings... Why you get different wrongs many times for tests 29,33,34,36,38,40,42? P.S. Sorry, if I am wrong. Edited by author 25.06.2009 19:39 Wolfram Mathematica 8 allows you to use this formula with trigonometry. He is a genius! It's a wonderful package!!! :) My first solution printed 2, if first player wins and the number of rocks is even and printed 1 if it's odd. And this solution got WA9. After printing "some variable" mod 3 the solution got AC. But why did my first idea fail? I realize my idea on Java and Pascal. On java i got TLe on 22 test, but on Pascal i got AC with time 0.069. Not good when problem depends on the language. It's not me posted if you can remove it? Thanks! Edited by author 24.03.2013 19:47 I test my code at my computer,I got a test which is filled by period string "abcde" and its length is 4000,and my program run only 0.25s. My computer is: Memory:3GB System:Windows XP CPU:1.66GHZ But I got tle at #32...Why??? Here is my code. var i,j,xx,ox,t,l:longint; s:ansistring; f,x:array [1..4010] of longint; o:array [0..4010] of boolean; function pdhw(l,r:longint):boolean; var i:longint; begin if l=r then exit(true); for i:=l to (l+r)>>1+1 do if s[i]<>s[r+l-i] then exit(false); exit(true); end; begin readln(s); l:=length(s); for i:=1 to l do begin f[i]:=maxlongint; if pdhw(1,i) then f[i]:=0 else for j:=1 to i-1 do if (f[i]>f[j]+1)and(pdhw(j+1,i)) then begin f[i]:=f[j]+1; x[i]:=j; end; end; writeln(f[l]+1); xx:=l; repeat ox:=xx; xx:=x[xx]; if ox=l then o[xx+1]:=true else o[xx+1]:=true; until xx=0; o[1]:=false; for i:=1 to length(s) do if o[i] then write(' ',s[i]) else write(s[i]); end. AC at 0.015 145 KB :) AC @ .015 and 112KB :) The same, AC 0.015 and 112 KB How come i can write very simple counter to be AC this without any algorithm? what the algo of this problem? please^_^ bottom-up DP, where DP[i] show will the first player win or lose the game with i rocks left, if everybody plays right and it's 1st player turn. Obviously, if i rocks left, and 1st player will lose, than i + k[j] is winning position for him (because if he throws away k[j] rocks, 2nd player will have i-th position, which is losing position for him an winning position for the first player). It's almost ready solution, just write a programm. Good luck! Could anyone help me to programming this problem? 1197. Lonesome Knight If you're outputting C or C++ codes, then the followings might help: 1. Each line in your program can't be too long. (I chose to add a line break every 950 characters.) Having lines with length exceeding the compiler's limit will make you WA on test 1. 2. Lengths of any string constant (or array?) should be shorter (no longer?) than 65536 bytes. This is yet another unreasonable compiler limitation. Going over the length limit will make you WA on test 3. I think the statements of this problem must have a full description of compilers used to compile outputs of solutions, otherwise it's nearly impossible to find out what does "WA1" mean. Not to mention that it's just plain wrong to use some magical non-standard-compatible compiler, that accepts "<iostream.h>" and "cout<<i" without using "std" namespace and so on. Please add to statements hints about code formatting your compiler accepts, don't turn this wonderful problem to boring technical problem "Archiver or Satisfy Our Compiler" You can generate archive-program for a simple text on your machine and submit it to see whether it is compiled on the server. Than you can rewrite your archive-program to compare the output with expected text intead of printing it to console. That's totally not the point. The program submitted might use different compiler from the archive-program being tested. It's also not clear exactly which symbols may appeared in the input. I think, it doesn't make any sense to create a difficult and tricky solution, to use e, etc. At first I thought, that bruteforce algorithm will be TLE, but it's AC 0.187 (a bit long, but not even close to 2 seconds). So, try bruteforce. Edited by author 18.03.2013 20:26 Edited by author 18.03.2013 20:26 Edited by author 18.03.2013 20:26 Yes, it obvious Hint V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V Try each way to play the game on 3 and 4 numbers on paper, than you will find what's the best strategy. #include<iostream> #include<vector> #include<cmath> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif int n,m,y; long double x; long double time; vector< long double >vec;
cin >> n >> m >> y; if( n <= 0 || n >= 999) exit(1); if( m <= 1 || m >= 999) exit(1); if( y <= 0 || y >= 99) exit(1); for( x = 0; x < m; x++) { time = pow(x, n); if( (unsigned long)time % m == y) vec.push_back(x); } if( vec.size() == 0) cout << "-1";
else { for( unsigned int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } } return 0; } Don't use pow~ Actually, the result of pow can exceed your data type(long). tips. (A*A) mod m = (A mod m) * (A mod m) Edited by author 01.10.2009 11:32 (A * A) mod m = ((A mod m) * (A mod m)) mod m |
|