Common BoardMy code in python 3.6: def is_palindrom(x): if x==x[::-1]: return True return False s=input() s=s[::-1] n=len(s) a=[] for x in range(2,n+1): for i in range(0,n-x+1): if is_palindrom(s[i:i+x]): a.append(s[i:i+x]) if len(a)==0: b=s[-1] else: b=a[-1] print(b) Memory is 70160 Kb, where so much it? Please, help, I would be grateful! I see you AC now What was the issue? Oh, sorry, it is other task Can somebody tell me why i get memory limit exceeded in test case #9... ???? i make an k-d tree and use nearest neighbor search in my solution and still have memory limit Edited by author 03.05.2013 07:52 Have you done it? i have memory limit in test 10 too (c++) Have you done it? i have memory limit in test 9 too (c++) Edited by author 05.09.2017 19:58 Edited by author 05.09.2017 19:58 Give me test#5? please!!! I tried all test from forum Kotlin compiler version 1.1.4 was added. See http://acm.timus.ru/help.aspx?topic=kotlin for more details. Here is a sample solution for A+B Problem: fun main(args: Array<String>) { val (x, y) = readLine()!!.split(' ').map(String::toInt) println(x + y) } Edited by author 05.09.2017 02:57On my PC it runs perfectly correct, the output is exactly the same(i have checked it using equals method so a mistake is impossible) but i get WA1 somehow. If somebody has an idea, please write it here. BTW I know that the first test is the sample test because a simple program that prints sample output gets WA2. PS here is my code if someone's interested: https://pastebin.com/SYiQTbMt Edited by author 04.09.2017 23:49 Edited by author 04.09.2017 23:49Do you think test case number one is the sample? Just try over and over again. My AC-program has wa when I try to submit it again. The same code has wa-1, wa-5, wa-7, wa-22, wa-30, ac, again wa... 5 10 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5 -> Unlucky Petr I have solved it only after reading Felix Mate's comment about dynamic programming approach Of course you can do precalculation and AC with Python But it is really hard to AC with Python with "online" calculation What may be the test 54 ?? i try, but it isn't enough. give a hint, please (test #6) Edited by author 01.09.2017 07:46 done! What about test #9? every time Runtime error. what can be there? Edited by author 02.09.2017 05:39 #include <iostream> #include <cmath> #include <iomanip> #include <vector> #include<algorithm> using namespace std; int main() { long long a, c, d, min, max, n, x;
long double b, e, sum; cin >> a >> n; sum = 0; d = 0; vector<long long> z(n); for (long long i = 0; i < n ; ++i) { cin >> z[i]; } for (long long i = 0; i < n; ++i) {
b = z[i] / 3; sum = sum + b; d = d + 1; if (a < sum) { break; }
} if (a > sum) { cout << "Team.GOV!" << endl; } else { cout << "Free after " << d << " times." << endl; } return 0; } Thank you admins for nice new feature I am about additional information about task in forum Namely task name in addition to task number ... 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 Guys can anyone give any idea about how to solve this dynamically . 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; } 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. 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? |
|