Common Board| Show all threads Hide all threads Show all messages Hide all messages | | No Need To Use Loop, Only If can solve it | Sherxon WIUT | 2011. Long Statement | 30 Aug 2017 18:43 | 10 | ... Edited by author 06.12.2014 19:30 I solve without loop but is return WA#4 There is my algorithm : if in list has 1 , 2 , 3 together always "Yes" otherwise "No" am I right or any case is there ? Edited by author 09.08.2017 09:41 Edited by author 09.08.2017 09:42 You can get six combinations with two a's and two b's You can get six combinations with one a and five b's Sorry but I don't understand what do you mean ! Give me Test Case #4 0011 0101 0110 1001 1010 1100 That is six 000001 000010 000100 001000 010000 100000 That is also six 012 021 102 120 201 210 And that is six Answer is No , Correct ? 0011 0101 0110 1001 1010 1100 That is six | | idea | srevarun | 1385. Interesting Number | 30 Aug 2017 14:51 | 1 | idea srevarun 30 Aug 2017 14:51 Guys can anyone give any idea about how to solve this dynamically . | | WA 15 | LastOne | 1651. Shortest Subchain | 30 Aug 2017 08:51 | 2 | WA 15 LastOne 30 Aug 2017 07:49 Please help #include <bits/stdc++.h> using namespace std; const int infInt = 1<<30; vector<int>adj[100010]; pair<int,int> edges[100010]; int parent[100010],dist[100010]; map< pair<int,int> ,int> Map; bool mark[100010]; void path(int u){ if(parent[u]==u){ cout<<edges[u].first<<" "<<edges[u].second; return; } path(parent[u]); cout<<" "<<edges[u].second; } int main(){ int ant,curr,root,n; cin>>n; for(int i=0;i<n;i++){ cin>>curr; if(i>0){ edges[i].first=ant; edges[i].second=curr; adj[ant].push_back(curr); Map[make_pair(ant,curr)]=i; } if(i==0) root=curr; ant=curr; } if(root==curr){ cout<<root<<endl; return 0; } queue<int> q; for(int i=1;i<n;i++){ parent[i]=i; dist[i]=infInt; if(edges[i].first==root){ q.push(i); dist[i]=0; } if(edges[i].second==curr) mark[i]=true; }
while(!q.empty()){ int u=q.front(); q.pop(); int x=edges[u].first; int y=edges[u].second; for(auto z: adj[y]){ int v=Map[make_pair(y,z)]; if(v>u and dist[v]>dist[u]+1){ dist[v]=dist[u]+1; parent[v]=u; q.push(v); } } } int best=infInt,bestInd=-1; for(int i=1;i<n;i++){ if(dist[i]<best and mark[i]){ bestInd=i; best=dist[i]; } } path(bestInd); cout<<endl; return 0; } Or this, aldo wa 15 What´s the problem?? #include <bits/stdc++.h> using namespace std; const int infInt = 1e9; vector<int>adj[100010]; pair<int,int> edges[100010]; int final; map< pair<int,int> ,int> Map; bool mark[100010]; bool vis[100010]; int dp[100010]; int f(int u){ if(edges[u].second==final)return 0; if(vis[u])return dp[u]; vis[u]=true; int x=edges[u].first; int y=edges[u].second; int ans=infInt; for(auto z:adj[y]){ int v=Map[make_pair(y,z)]; if(v>u) ans=min(ans,1+f(v)); } return dp[u]=ans; } void rec(int u){ if(edges[u].second==final){ cout<<" "<<final<<endl; return; } int x=edges[u].first; int y=edges[u].second; for(auto z:adj[y]){ int v=Map[make_pair(y,z)]; if(v>u and dp[u]==1+f(v)){ cout<<" "<<y; rec(v); break; } } } int main(){ int ant,curr,root,n; cin>>n; for(int i=0;i<n;i++){ cin>>curr; if(i>0){ edges[i].first=ant; edges[i].second=curr; adj[ant].push_back(curr); Map[make_pair(ant,curr)]=i; } if(i==0) root=curr; ant=curr; if(i==n-1) final=curr; } if(root==curr){ cout<<root<<endl; return 0; } int best=infInt,bestInd=-1; for(int i=1;i<n;i++){ if(edges[i].first==root){ int ans=f(i); if(ans < best){ best=ans; bestInd=i; } } } cout<<edges[bestInd].first; rec(bestInd); } Please help #include <bits/stdc++.h> using namespace std; const int infInt = 1<<30; vector<int>adj[100010]; pair<int,int> edges[100010]; int parent[100010],dist[100010]; map< pair<int,int> ,int> Map; bool mark[100010]; void path(int u){ if(parent[u]==u){ cout<<edges[u].first<<" "<<edges[u].second; return; } path(parent[u]); cout<<" "<<edges[u].second; } int main(){ int ant,curr,root,n; cin>>n; for(int i=0;i<n;i++){ cin>>curr; if(i>0){ edges[i].first=ant; edges[i].second=curr; adj[ant].push_back(curr); Map[make_pair(ant,curr)]=i; } if(i==0) root=curr; ant=curr; } if(root==curr){ cout<<root<<endl; return 0; } queue<int> q; for(int i=1;i<n;i++){ parent[i]=i; dist[i]=infInt; if(edges[i].first==root){ q.push(i); dist[i]=0; } if(edges[i].second==curr) mark[i]=true; }
while(!q.empty()){ int u=q.front(); q.pop(); int x=edges[u].first; int y=edges[u].second; for(auto z: adj[y]){ int v=Map[make_pair(y,z)]; if(v>u and dist[v]>dist[u]+1){ dist[v]=dist[u]+1; parent[v]=u; q.push(v); } } } int best=infInt,bestInd=-1; for(int i=1;i<n;i++){ if(dist[i]<best and mark[i]){ bestInd=i; best=dist[i]; } } path(bestInd); cout<<endl; return 0; } | | Congratulations | Nikita Mogilevets | | 30 Aug 2017 02:02 | 2 | | | How to solve this problem?I used Djkstra,but I've got TLE. | Mingfei Li | 1325. Dirt | 16 Aug 2017 01:32 | 8 | Lee algorithm. Runs in O(N * M). If you don't know it, here is a short description: you start from starting position (sl, sc) and now you expand this node; in a queue you put all its neighbours. You do this only if you optimize by going to that neighbour, i.e. the current cost is shorter than the previous one. let a[i][j] be a matrix with two fields: boots and length. a[i][j] means: minimum number of boots and minimum length to reach (i, j) from start position. You expand from (x, y) to a neighbour (c, d) when: a[x][y].boots + 1 (if you change boots, 0 else) < a[c][d].boots or a[x][y].boots + 1 = a[c][d].boots and a[x][y].length + 1 < a[c][d].length. Obvious, you take the valid neighbours (not 0s or outside ones) hope it's clear... Can someone explain where is the problem in this solution? I just used the algorithm as you described. But it got tle. Anyway thank you very much. Now I've got it,using heap will help me. Edited by author 19.04.2004 20:55 I wrote the solution with Lee's algorithm, as it was discribed above by Gheorghe Stefan, and got TL on test 16. I doubt it works in O(MN). I test the solution at my local computer - it gaves the correct answers (obviously), but works about 10 times slower, then my solution with Dijkstra! I BFS on number of boots and Dijkstra on walk-length inside odd/even slices. Dijkstra is linear-time here because of same-length edges (front line is kept as bidirectional list in non-descending order of walk-length, insertion position goes only forwards). AC in 0.062 sec :) Edited by author 21.08.2008 21:13 It could be proved that in Dijkstra's algorithm the choice K = E /V is assimptotically optimal. So, applying to our problem, E / V equals to 8. | | To Admins | Jia Wang | 1358. Cables | 15 Aug 2017 23:41 | 3 | Can the coordinates be negative integers? yes, coordinates may be negative and this problem is very very easy :) This makes the problem much easier. Can anyone share the solution without this condition? | | Tests for WA7 | Filip Franik | 1878. Rubinchik's Cube | 14 Aug 2017 19:48 | 1 | 3 1 2 4 1 3 4 2 4 2 1 3 2 4 3 1 (4) 4 1 2 1 1 4 1 2 4 3 2 3 3 4 3 2 (2) | | Code don't works with big numbers, Python 3.4 | Andrew1703 | 1017. Staircases | 14 Aug 2017 18:39 | 10 | k=int(input()) a=[] a.append(0) a.append(1) a.append(1) a.append(2) a.append(2) s=0 for j in range(5,k+1): if j%2==1: for i in range(1,j//2+1): s+=a[i] a.append(s)
else: for i in range(1,j//2): s+=a[i] s=s+a[j//2]-1 a.append(s) s=0 print(a) You are printing a, print(a). A is a LIST. FUUUCK, thanks, its so stupid error( but code still not works, starting with the second test, and in example program gives WA, but I check code at least 3 times, I cant find errors(( I don't understand how does your algorithm work. My AC - program is calculating values sequentially for different ladders. Time complexity is O(N*N*N). So, it is basically two dimensional dynamic programming. And your algorithm has time complexity just O(N*N). It is not surprising that I can't understand it without explanation. It is extremely optimized solution, with heavy math behind. Edited by author 23.06.2017 23:28 you don't need heavy math you can in O(n^2) time and O(n^2) space i don't know how u got O(n^3) cin >> n; for(int i = 0; i < n; i++) dp[i][0] = 1; for(int i = 1; i < n; i++) for(int j = 1; j <= n; j++) { dp[i][j] = dp[i - 1][j]; if(j >= i) dp[i][j] += dp[i - 1][j - i]; } cout << dp[n - 1][n]; Edited by author 11.08.2017 05:18 Edited by author 11.08.2017 05:19 When I was solving it I thought There is a staircase consists of X cubes and it's height is Y Then just add some new stair with height Z Z is strictly greater than Y Get new staircase with X+Z cubes and height Z But also I know There is a solution based on generating functions I can't understand generating functions theory So I have AC on task but don't understand generating functions and that guy didn't had AC at the moment So probably generating functions are hard for him just like they are hard for me That is why I have called generating functions "heavy math" tell me more, do you know a good article for this? | | My Code | Imran Yusubov | 1012. K-based Numbers. Version 2 | 14 Aug 2017 17:40 | 2 | My Code Imran Yusubov 13 Apr 2009 10:45 import java.math.BigInteger; import java.util.Scanner; public class bigKnum {
public static void main(String args[]){ Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt();
BigInteger f1,f2,f3; f1=BigInteger.valueOf(k-1); f2=f1.multiply(BigInteger.valueOf(k));
for(int i=2;i<n;i++) { f3=(f1.add(f2)).multiply(BigInteger.valueOf(k-1)); f1=f2; f2=f3;
} System.out.print(f2);
}
} Instead of using three temporary variables f1, f2, f3 make one array of three BigInteger elements f and just index it using modulo like so f[i%3], f[(i-1)%3], f[(i-2)%3]. This works exactly the same, and you don't have to make this awful and confusing data move of f1=f2; f2=f3; | | AC Solve. It's problem is very easy!! | XSpider | 1048. Superlong Sums | 14 Aug 2017 17:38 | 2 | That is my AC solve. #include <stdio.h> char a[1000005], b[1000005]; int Count, min; int main() { scanf("%d", &Count); min=1; for (int i=1; i<=Count; i++) scanf("%d %d", &a[i], &b[i]); // summing for (int i=Count; i>=0; i--) { int temp = a[i] + b[i]; a[i] = temp%10; a[i-1] += temp/10; } if (a[0] == 1) min--; for (int i=min; i<=Count; i++) printf("%d", a[i]); return 0; } I wrote the same code on C#, but it was TLE on test#4! So, i send your code. Thank you. | | What method I have to use? | Neo Nomaly | 1448. Lighting in Hogwarts | 13 Aug 2017 23:23 | 3 | The only method I used to solve it was the intuition. I hope anyone who knows a proof of any solution (I think there are many) will expain it. Пожалуйста, не сдавайте задачу, не понимая своё решение!!! Это хорошая математическая задача из темы на конструктив. Во-первых, покажем, что решение всегда существует. Для k=1 утверждение справедливо. Пусть уже построена последовательность s1...sk, удовлетворяющая условию задачи. Пусть cij=кол-во 1 на [i,j]. Покажем, что можно добавить 0 или 1 в конец, чтобы полученная последовательность также была решением. Предположим противное: при добавлении 0 есть подотрезок [i,k+1], где cik>(k-i+2)b/100+2 (1) или cik<(k-i+2)b/100-2 (2) и при добавлении 1 есть подотрезок [j,k+1], где cjk>(k-j+2)b/100+1 (3) или cjk<(k-j+2)b/100-3 (4). Заметим, что (1) и (4) невозможны (по индукции |cik-(k-i+1)b/100|<=2 => cik<=(k-i+1)b/100+2 и |cjk-(k-j+1)b/100|<=2 => cjk>=(k-j+1)b/100-2). Итого получаем (2) и (3) одновременно: cik<(k-i+2)b/100-2, cjk>(k-j+2)b/100+1 => cjk>cik => j<i. Рассмотрим отрезок [j,i-1]. По индукции cj,i-1=cjk-cik удовлетворяет условию |(cjk-cik)-(i-j)b/100|<=2 => cjk<=cik+(i-j)b/100+2<(k-i+2)b/100+(i-j)b/100=(k-j+2)b/100. => cjk<(k-j+2)b/100. С другой стороны, выполнено (3)-противоречие => 0 или 1 даёт решение длины к+1. Как сконструировать решение? Из док-ва видно, в каких местах возникают проблемы. Когда можно добавить 0 (если нельзя => нужно добавить 1-это даст решение по док-ву)? 0 можно добавить <=> для 1<=i<=k выполнено (k-i+2)b/100-2<=cik<=(k-i+2)b/100+2. Второе неравенство выполнено (т.к. cik<=(k-i+1)b/100+2). Поэтому нужно (k-i+2)b/100-2<=cik. Когда это условие нарушается? Оно нарушается <=> существует такое 1<=i<=k, что cik<(k-i+2)b/100-2 <=> cik<(-b/100)i+(2kb/100-2).Заметим что cik неубывает, а (-b/100)i+(2kb/100-2) строго убывает => нарушение происходит <=> i=1 <=> c1k<-b/100+2kb-2=kb/100-2 <=> 100*c1k<kb-200. В этом случае 0 брать нельзя. Edited by author 13.08.2017 23:28 | | if you have wa#7 | Лукьянчиков Владимир Игоревич | 1713. Key Substrings | 13 Aug 2017 20:07 | 2 | try this test 2 abab baa answer: ab aa | | Time limit exit! | Ayaz | 1086. Cryptography | 13 Aug 2017 12:47 | 1 | I have used sieve theorem but i am still getting TLE! i need my code optimization .here is my code: #include<iostream> #include<cmath> using namespace std; #define TOTAL 163841 int main() { int ara[TOTAL]; for(int i=2; i<TOTAL; i++) { ara[i]=1; } int root = sqrt(TOTAL); for(int i=2; i<=root;i++) { for(int j=2; i*j<=TOTAL; j++) \\ Using sieve theorem { ara[i*j]=0; } } int T; int n; cin >> T; int r,k; for(int l=1; l<=T; l++) { cin >> n; if(n<=15000) { int ara2[n]; for(k=2,r=1;r<=n;k++) { if(ara[k]==1) { ara2[r]=k; \\ copying the prime numbers to the ara2[] r++; } } cout << ara2[n] << "\n"; // cout the last num of ara2[] } } return 0; } Edited by author 13.08.2017 12:49 | | How many tests are there, lol? | Nikita Mogilevets | 2011. Long Statement | 12 Aug 2017 16:21 | 1 | There are at LEAST, 70 tests. | | Stupid hint | __Andrewy__ | 2059. Not common palindromes | 12 Aug 2017 01:51 | 1 | If you got TL you can try send same code and to get AC. When I delete 2 string (they are comments) i got AC. | | weak tests | falicos | 1592. Chinese Watches | 11 Aug 2017 21:18 | 1 | my program as many others in net got AC but failed on test 3 1:00:00 12:59:58 12:59:59 Correct answer is 1:00:00 but not 13:00:00. | | #test 11 | liudy | 1781. Clean Code | 11 Aug 2017 16:43 | 1 | Can someone give me some hints about what #test 11 is? | | WA 7, WA 8 | 💻Evgeny Nemtsev [UrFU FT-17] | 1608. Lucky Tickets 2008 | 10 Aug 2017 22:53 | 1 | WA 7, WA 8 💻Evgeny Nemtsev [UrFU FT-17] 10 Aug 2017 22:53 | | How to solve this problem without long arithmetic? | __Andrewy__ | 1103. Pencils and Circles | 10 Aug 2017 16:38 | 1 | If i use double -> big error If i use long long -> overflow now i got Runtime error on java. Where I am wrong? I think my function Less is bad but I can't find mistake. My code: import java.math.*; import java.util.*; public class BigNumbers { final int tmax=100; MathContext mc = new MathContext(tmax);
final int nmax=5005;
static int Pox[], Poy[], id[]; static BigDecimal Px[], Py[]; static int n, left;
public static boolean Less(BigDecimal x1, BigDecimal y1, BigDecimal x2, BigDecimal y2) { return (y1.compareTo(BigDecimal.ZERO)!=-1) && (y2.compareTo(BigDecimal.ZERO)==1) && ((y1.multiply(x2)).compareTo(x1.multiply(y2))==-1) || (y1.compareTo(BigDecimal.ZERO)==-1) && ((y2.compareTo(BigDecimal.ZERO)!=-1)) || ((x1.multiply(y2)).compareTo(x2.multiply(y1))==1); }
public static BigDecimal modul2(BigDecimal x, BigDecimal y) { return (x.multiply(x)).add(y.multiply(y)); }
public static void QSort(int L, int R) { int m=(L+R)/2; int i=L; int j=R;
while(i<=j) { while(Less(Px[i],Py[i],Px[m],Py[m])) i++; while(!Less(Px[j],Py[j],Px[m],Py[m]) && j!=m) j--; if(i<=j) { BigDecimal Y=Px[i]; Px[i]=Px[j]; Px[j]=Y; Y=Py[i]; Py[i]=Py[j]; Py[j]=Y; int y=id[i]; id[i]=id[j]; id[j]=y; i++; j--; } } if(L<j) QSort(L, j); if(i<R) QSort(i, R); }
public static void main(String args[]) { Scanner sc=new Scanner(System.in); n=sc.nextInt();
Pox=new int[n+1]; Poy=new int[n+1]; id=new int[n+1]; Px=new BigDecimal[n+1]; Py=new BigDecimal[n+1];
for(int i=1;i<=n;i++) { int x, y; x=sc.nextInt(); y=sc.nextInt(); Pox[i]=x; Poy[i]=y; }
System.out.println(Pox[1]+" "+Poy[1]);
for(int i=1;i<=n-1;i++) { BigDecimal x=BigDecimal.valueOf(Pox[i+1]-Pox[1]); BigDecimal y=BigDecimal.valueOf(Poy[i+1]-Poy[1]); Px[i]=x.divide(modul2(x,y)); Py[i]=y.divide(modul2(x,y)); id[i]=i+1; }
left=1; for(int i=2;i<=n-1;i++) if(Px[left].compareTo(Px[i])==1 || Px[left].compareTo(Px[i])==0 && Py[left].compareTo(Py[i])==1) left=i;
System.out.println(Pox[id[left]]+" "+Poy[id[left]]);
for(int i=1;i<=n-1;i++) { Px[i]=Px[i].subtract(Px[left]); Py[i]=Py[i].subtract(Py[left]); }
for(int i=left;i<=n-2;i++) { Px[i]=Px[i+1]; Py[i]=Py[i+1]; }
QSort(1,n-2); System.out.print(Pox[id[(n-1)/2]]+" "+Poy[id[(n-1)/2]]); } } | | Try to consider vertices with the same distance to f from s | Nikita Mogilevets | 2034. Caravans | 10 Aug 2017 10:54 | 1 | |
|
|