Common BoardInput: 3 '╣' like '[╣-I]' '╣' like '╣' '╣' like '_' Output: YES YES YES My AC solution gives NO to the first one; there should be no tests where the start character in a range is greater than the end character. 24 12 12 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Right Answer is 6 . - empty cage 1 - chess knight 2 - cells under attack 8 | . . . . . . . . 7 | . . . . . . . . 6 | . . 2 . 2 . . . 5 | . 2 . . . 2 . . 4 | . . . 1 . . . . 3 | . 2 . . . 2 . . 2 | . . 2 . 2 . . . 1 | . . . . . . . . ___________________ a b c d e f g h 1 2 3 4 5 6 7 8 is it possible to solve this problem with "smart" backtracking algo using a lot of restrictions ? As I know YES. But there easy and beautiful math solution. give me a hint how to solve it without using backtracking algorithm thank you ur e-mail ... Please send it to me too. eros_89@yandex.ru About math solution.. I am trying to it by the following way: Assume a connection between stops A and B. Then connection from B to A exists too. Then, to satisfy the problem's conditions, we have to introduce (2*n-1)*n connections (ordered in such a way that each tram route has 2*n-1 connections). All the connections are distinct. Next, each tram stop has exactly 2*n-1 connections to other distinct stops. It is always odd integer, so every tram stop must appear exactly once at the beginning of single route, and next time it must appear (2*n-2)/2=n-1 times in the middle of other routes. It is true for all the routes. So first we should build the following answer matrix if we consider n = 3: 1 x x x x 4 2 x x x x 5 3 x x x x 6 Then I am trying to place other elements to the matrix according to some placement rules from the thoughts above, but I am still unable to come up with a good solution. Could someone point out, am I going correct in my math thoughts or such approach leads to naive backtracking solution? Thanks! Sorry I was so verbose :D No more math is needed. Now you can use backtracking. I resisted the idea of writing a backtracking algo, but decided to implement it with rule of placing elements leftmost and outermost as you suggested and got AC :) Thank you. PS. I wonder if it is possible to write a backtracking solution without any initial placements or not. 2 Chmel_Tolstiy: Could you please give me small hint about a math solution? master.wsd064 (no spam) gmail.com Edited by author 03.04.2008 18:27 your Idea is a 2/3 of all way. just try to find a regularity between this paths. I think it's easy. naxart[at]yandex[dot]ru it's my email. I've found simple maths solution, here is my solution for n=3 1 2 3 4 5 6 2 4 6 1 3 5 3 6 2 5 1 4 Edited by author 02.06.2008 05:28 Edited by author 02.06.2008 05:28 There is no need for _smart_ backtracking. I placed 1..n on the left side and n..2n on the right side. Then I launched recursion over all cells row-by-row, column-by-column, taking every edge (i;j) exactly once, and it's AC in 0.078 sec. For n=100 there occurs only 562 returns. So, it's almost greedy algo. The only problem was expanding that recursion into a loop 'cuz I had stack overflow with 200x100 calls. Edited by author 25.07.2008 17:31 Continue filling the matrix from both left and right. Keep each pair of opposite columns contain every tram stops. I want to know too. pkkj@tom.com Thanks. Only math solution acceptable because this is structure design problem. I spent a lot of time but was unable to find it. Solution easy to find in internet with key words "decomposing complete graph in hamilton paths" Use diameters and walk left-rigth- down with respect to them. Edited by author 14.08.2009 11:02 no subject Edited by author 14.08.2009 11:03 Sir Isaac Newton is important to this problem. In TWO different ways. Why WA 28?I don't understand. Here is my code: program gjkh; var a,b,ch,n,i,kol,sh,fir,sec,thi:longint; m:array[1..100001]of longint; flag:boolean; begin read(a);readln(b); n:=0; while not seekeof do begin read(ch); if (n=0)or(m[n]<>ch)then begin n:=n+1; m[n]:=ch; end; end; flag:=true; kol:=1; for i:=1 to n do begin if (i=1)then fir:=m[i] else begin if (flag=true)then begin sec:=m[i]; flag:=false; end else begin thi:=m[i]; if ((thi-sec)*(sec-fir)>0)then begin fir:=sec;sec:=thi; end else begin fir:=thi; kol:=kol+1; flag:=true; end; end; end; end; writeln(kol); end. 1 5 -100000 500000 -70000 800000 -80000 answ: 3 Edited by author 01.02.2010 18:44 Input numbers must not exceed 100000 by absolute value. why is 3, because there are 4 intervals? Edited by author 16.06.2021 15:21 Edited by author 16.06.2021 15:22 It means, each employee (except the director) has only one immediate superior. If employee A is superior to employee B, and employee C is superior to employee A, then C->A->B. Both C and A are superior to B, but only A is B's immediate superior. #include<algorithm> #include<cstdio> using namespace std; #define MAX_N 250000 int n, i, t1, t2; int *heap; int main(){ scanf("%d", &n); heap = new int[n/2+2]; for(i = 0; i < n/2+1; ++i){ scanf("%d", heap+i); push_heap(heap, heap+i+1); }
for(i = 0; i < n/2-(n+1)%2; ++i){ scanf("%d", heap+n/2+1); push_heap(heap, heap+n/2+2); pop_heap(heap, heap+n/2+2); } if(n%2){ printf("%d.0\n", heap[0]); }else{ t1 = heap[0]; pop_heap(heap, heap+n/2+1); t2 = heap[0]; if((t1+t2)%2){ printf("%d.5\n", t1/2+t2/2); }else{ printf("%d.0\n", t1/2+t2/2+t1%2); } } delete[] heap; return 0; } #include <iostream> #include <algorithm> using namespace std; int main() { int i,j,k,l,ii; int m,n; int girls[101]; int boys[101]; int count = 0; int upsets = 0, girlUpsets = 0, boyUpsets = 0, currentUpsets = 0; int happyGirls = 0; int chooseGirl = 0, chooseBoy = 0; for (i = 0; i <= 100; i ++) { girls[i] = 0; boys[i] = 0; }
cin >> n >> m; //n girls m boys for (i = 1; i <= n; i ++) { cin >> girls[i]; } for (j = 1; j <= m; j ++) { cin >> boys[j]; } count = min (m, n); //计算无配对upset值 for (i = 1; i <= n; i ++) upsets += girls[i]; happyGirls = 0;
for (i = 1; i <= count; i ++)
{ for (j = 1; j <= n; j ++) for ( k = 1; k <= m; k ++) { girlUpsets = 0; boyUpsets = 0; //计算girl upset值 for (l = 1; l <= n; l ++) { if (l != j) { girlUpsets += girls[l]; } } //计算boy upset值 for (l = 1; l <= m; l ++) { if (l != k) boyUpsets += boys[l] * (happyGirls + 1); //cout << "werqe: " << boys[l] << '\t' << boyUpsets << endl; } currentUpsets = girlUpsets + boyUpsets; if (currentUpsets < upsets) { upsets = currentUpsets; chooseGirl = j; chooseBoy = k; } } if (chooseBoy != 0 && chooseGirl != 0) { happyGirls ++;
//删除第chooseGirl个girl for (ii = chooseGirl; ii <= n - 1; ii ++) girls[ii] = girls[ii + 1]; n --;
//删除第chooseBoy个boy for (ii = chooseBoy; ii <= m - 1; ii ++) boys[ii] = boys[ii + 1]; m --; } } cout << upsets << endl; return 0; } Find a robust feature to distinguish different bones with same total dots. This really is an easy problem once you find that feature. Try this tests INPUT tram OUTPUT Tram driver INPUT tram.tram trolleybus OUTPUT Tram driver Edited by author 10.03.2010 23:01 Test 6 is <tram,> or <trolleybus,>. (whitout <>) ;-) Edited by author 28.02.2014 17:38 i need some test... i guess the mistake is about "tram", i don't know which pattern of "tram*" should be counted I had a WA25 when didn't count last word. I assume there are empty lines there My solution is fast! valery@uni.lg.ua Ha! My solution: only 0.001 sec and 70 Kb! And it's normal solution (no cheating). I sent it some minutes ago. PS: It's very good that your solution works fast but Always there is better one :) Ha-Ha! My solution works sometimes too 0.001, but how many iterations of your cycle on N=500? Ha-ha-ha! My solution is O(n^2) but I know O(sqrt(n)*n) one. My solution is O(1) :) sorry to bother u on such an old post...but can u help me on a better approach to this question my solution is O(0) :) Jokes on you! My solution is O(-∞) So, your solution travels back in time or make the time in the Universe acting backwards?:) Why is here the runtime error on the 6 test? What test is 6? #include <iostream> #include <cmath> using namespace std; void ghjkl(long double p) {cout << fixed; cout.precision(4); cout << p << endl;} int main() { long double a[1000]={-1.0}; long long i=0; while(cin) {cin >> a[i]; i++;} i=i-2; long double b[1000]; for(int k=0; k<1000; k++) {b[k]=sqrt(a[k]);} for(int m=i; m>=0; m--) {ghjkl(b[m]);} return 0; } Edited by author 24.03.2021 16:53 Edited by author 24.03.2021 16:54 i don't know Edited by author 31.03.2021 19:27 Edited by author 31.03.2021 19:27 Edited by author 31.03.2021 19:27 Oh, I have understood. The array isn't the needed collection for this task. Stack is better. #include <iostream> #include <stack> #include <cmath> using namespace std; int main() { stack<double> a; double b; int c=0; while(cin) {cin >> b; a.push(b); c++;} c-=1; a.pop(); for(int i=0; i<c; i++) {cout << fixed; cout.precision(4); cout << sqrt(a.top()) << endl; a.pop();} return 0; } Edited by author 10.06.2021 03:35 Or you can use deque: #include <iostream> #include <queue> #include <cmath> using namespace std; int main() { deque<double> a; double b; int c=0; while(cin) {cin >> b; a.push_back(b); c++;} c-=1; a.pop_back(); for(int i=0; i<c; i++) {cout << fixed; cout.precision(4); cout << sqrt(a.back()) << endl; a.pop_back();} return 0; } Hi, Well it took me a while to figure out as to how to approach this problem but as it turned out it is pretty straightforward. I divided the problem into two parts. First I printed out the elements up until the middle diagonal. And then the elements after that. Here is the format of the numbers for 4 X 4 array 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 We can see a pattern here. First print the element 0,0. Then -> 1,0 to 0,1 (i = 1 to i = 0 and j = 0 to j = 1) Then -> 2,0 to 0,2 (i = 2 to i = 0 and j = 0 to j = 2) Then -> 3,0 to 0,3 (i = 3 to i = 0 and j = 0 to j = 3) So, basically as we are heading towards the main diagonal, we are increasing the value of i and at the same time running j from 0 to i. Implementing this will get you the first 10 out of 16. For the rest of them, we have the same pattern again but this time its reverse Then -> 3,1 to 1,3 (i = 3 to i = 1 and j = 1 to j = 3) Then -> 3,2 to 2,3 (i = 3 to i = 2 and j = 2 to j = 3) Then the last element 3,3 So, this just requires some clever manipulation of the for loops or while loops. In order to achieve absolute safety providing electricity to the camps, besides an electric supplying system, the host organization set up a path from a reserved electricity generator (which is placed in one of the camps) to every camp once, this means that, thare is only and only one path from Pi camp to Pj camp. The answer should be a path (successive line segments) NOT a tree (minimal spanning tree) NOT a Steiner tree (minimal spanning tree with additional points) Можете дать подсказку? Я понял что количество всех пар всегда три И число дней как посчитать Но не могу подобрать алгоритм чтобы вывести в каком порядке будут солдаты выходить на дежурство Edited by author 02.06.2021 13:24 |
|