Common BoardKotlin 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? 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) 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? 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; |
|