Common Boardplz help Try this: 6 15 6 10 6 9 7 1 I think your answer is: 4 6 10 6 9 7 1 But answer like: 5 6 10 7 9 6 1 5 22 8 15 6 6 14 answer 4 8 15 14 6 6 I can get user input ...coz when i wanna make the line breaks...Java compiler Intillej starts to get compiled plz i need help to get input use this code ********************************* char[] bytes = new char[256 * 1024]; BufferedInputStream bufferedInputStream = new BufferedInputStream(System.in); InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream); inputStreamReader.read(bytes); String string = new String(bytes); There are all the numbers are greater than 1000000 in the test 6! Please remove this test or fix the description of the problem. P.S. After I discovered it I solved the problem anyway :) Thank you! Russian and English statements had different constraints. Now both have 10^7 for input lengths. I have no solution to this problem, help me Edited by author 27.10.2014 14:27 What's wrong? Here is my solution: var i,j,k,y,fl,s,t,max,min,r:longint; a:array [1..100] of string; b,c:array [1..100] of longint; d,x:string; begin for i:=1 to 6 do begin readln (d); readln (x); readln (y); fl:=0; for j:=1 to k do If a[j]=x then fl:=j; If fl=0 then begin k:=k+1; a[k]:=x; b[k]:=y; c[k]:=1;end; If fl>0 then If y<b[fl] then begin b[fl]:=y; c[fl]:=c[fl]+1;end else c[fl]:=c[fl]+1; end; max:=-2000000000; for i:=1 to k do if c[i]>max then max:=c[i]; for i:=1 to k do if c[i]=max then begin s:=s+1; r:=i;end; If s=1 then writeln (a[r]) else begin min:=2000000000; for i:=1 to k do If b[i]<min then min:=b[i]; i:=0; repeat i:=i+1; if b[i]=min then writeln (a[i]); until (b[i]=min) or (i=k); end; end. How does the "5" coming? That's how the pointer moves: x = 0; y = 0 x = 1; y = 0 x = 2; y = 0 x = 3; y = 0 x = 3; y = 1 x = 4; y = 1 x = 4; y = 2 x = 4; y = 3 x = 3; y = 3 x = 2; y = 3 x = 1; y = 3 x = 0; y = 3 x = 0; y = 2 x = 0; y = 1 x = 1; y = 1 x = 2; y = 1 x = 3; y = 1 x = 4; y = 1 x = 4; y = 2 x = 4; y = 3 x = 3; y = 3 x = 2; y = 3 x = 1; y = 3 x = 0; y = 3 x = 0; y = 2 x = 0; y = 1 x = 1; y = 1 x = 2; y = 1 x = 3; y = 1 x = 4; y = 1 x = 4; y = 2 x = 4; y = 3 x = 3; y = 3 x = 2; y = 3 x = 1; y = 3 x = 0; y = 3 x = 0; y = 2 x = 0; y = 1 x = 1; y = 1 x = 2; y = 1 x = 3; y = 1 x = 4; y = 1 x = 4; y = 0 x = 5; y = 0 x = 5; y = 1 x = 5; y = 2 x = 5; y = 3 hi I need some test to check my code it works ok when I enter numbers but not accepted in "test 2" Edited by author 25.10.2014 15:23 Если k>n что будет выводить? The following test helped me a lot: Input: 10 2 0 0 0 0 1 0 3 0 4 0 5 0 6 0 7 0 1 5 1 2 3 4 5 2 5 6 7 8 9 10 Output: 2 right 2 left 5 right 3 left 3 right 6 left 4 left 4 right 6 right 5 left Hi! I have time limit exceeded in test 22. What's wrong with my code? What is the best approach for this task? Maybe use stack? Thanks; My code: #include <iostream> #include <vector> #include <algorithm> using namespace std; //Get all possible spell power variants vector<int> GetVariants(vector<int> vct) { vector<int> answ; for(unsigned int i=0;i<vct.size();i++) { if(find(answ.begin(),answ.end(),vct[i])==answ.end()) answ.push_back(vct[i]); } sort(answ.begin(), answ.begin()+answ.size()); reverse(answ.begin(),answ.end()); return answ; } //Get number or values, in vct which <= num (here it is destructable coins) int GetNumberOfLowerOrEqual(vector<int> vct,int num) { int number = 0; for(unsigned int i=0;i<vct.size();i++) { if(vct[i]<=num)number++; } return number; } int main() { int n,p,curr; scanf("%d",&n);//initial size of coins vector scanf("%d",&p);// health vector<int> coins; vector<int> variants; int deleted = 0; int spells = 0; for(int i=0;i<n;i++) { scanf("%d",&curr); coins.push_back(curr); } variants = GetVariants(coins); bool dChanged = false;
for(int times = 0; times<n && !coins.empty();times++) { for(int i=0;i<variants.size() && !coins.empty();i++) { if(GetNumberOfLowerOrEqual(coins,variants[i])*variants[i]<=p)//surrvived { dChanged = false; //here we remove all elements from coins, which <= variants[i] for(int t=0;t<coins.size();t++) { if(coins[t] <= variants[i]) { coins.erase(coins.begin()+t); t--; deleted++; dChanged = true; } } if(dChanged) { spells++;//if anything changed, add spell break; } } } } printf("%d ", deleted); printf("%d",spells); //system("pause"); return 0; } sorry, didn't notice that the letter I is omitted!! )) use correct alphabet A B C D E F G H K I don't have permissons to this page. BTW, there was a bug in checker: std::unique wants sorted array. It's fixed on Timus. Thanks, I always forget about that sorting I want to use bool dp[N][N][3] array, where dp[i][j][0] is true if it is possible to solve the problem for last 'i' characters of string1 and for last 'j' characters of string2 and in current state it is necessary to take 0 and place into the stack of '0's. dp[i][j][1] is true if it is possible to solve the problem for last 'i' characters of string1 and for last 'j' characters of string2 and in current state it is necessary to take 1 and place into the stack of '1's. dp[i][j][2] is true if it is possible to solve the problem for last 'i' characters of string1 and for last 'j' characters of string2 and in current state stacks of '0's and '1's have the same size. Obviously (dp[N][N][2] == true) means that it is possible to solve the problem. My problem is that I can't fill this 3D array. Please help. you just need dp[i][j] and store there form were you get the last piece, if it was from 1 you can rebuild the path looking dp[i-1][j], if it was from 2 you llok dp[i][j-1]. If dp[i][j] = 0 you can't solve the problem. Is it possible to solve this problem with Python 3 or Scala? I got TLE with same linear algo on both langauges (tried mutable and immutable collections in Scala and so I think there are problems with slow input) but got AC (in 0.171 sec) only with pure Java solution. Edited by author 21.10.2014 13:07 I am using priority queue. Priority is determined by the number of elements. The top element is one without parents. When it's removed from the queue, it's being deleted as a parent from all the other elements. I get WA#4. Any idea why? This is my code. Can someone tell me why do I get WA#4? import java.util.Collection; import java.util.Comparator; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.PriorityQueue; import java.util.Scanner; public class MartianCouncil { public static void main(String[] args) { Hashtable<Integer, TreeNode> global = new Hashtable<Integer, TreeNode>(); Scanner s = new Scanner(System.in); int n = s.nextInt(); if (n == 0) { s.close(); return; } s.nextLine(); for (int i = 1; i <= n; i++) { TreeNode node = global.get(i); if (node == null) { node = new TreeNode(i); global.put(i, node); } String line = s.nextLine(); String[] nums = line.split(" "); for (int j = 0; j < nums.length - 1; j++) { int num = Integer.parseInt(nums[j]); TreeNode nd = global.get(num); if (nd == null) { nd = new TreeNode(num); global.put(num, nd); } node.addChild(nd); } } s.close(); print(global); }
public static void print(Hashtable<Integer, TreeNode> global) { PriorityQueue<TreeNode> q = new PriorityQueue<TreeNode>(); q.addAll(global.values()); StringBuilder res = new StringBuilder(""); while (!q.isEmpty()) { TreeNode nd = q.poll(); res = res.append((res.length() == 0) ? "" : " "); res = res.append(nd.getNum()); } System.out.print(res); }
public static class TreeNode implements Comparable<MartianCouncil.TreeNode> {
private int num = 0; private Collection<MartianCouncil.TreeNode> children = new HashSet<MartianCouncil.TreeNode>(); private Collection<MartianCouncil.TreeNode> parents = new HashSet<MartianCouncil.TreeNode>();
public TreeNode(int num) { this.num = num; }
public int hashCode() { return num; }
public boolean equals(Object o) { if (o == null) { return false; } else if (!(o instanceof MartianCouncil.TreeNode)) { return false; } else { return (this.hashCode() == o.hashCode()); } }
public boolean addChild(MartianCouncil.TreeNode child) { child.parents.add(this); return children.add(child); }
public boolean removeChild(MartianCouncil.TreeNode child) { child.parents.remove(this); return children.remove(child); }
public boolean addParent(TreeNode parent) { parent.children.add(this); return parents.add(parent); }
public boolean removeParent(TreeNode parent) { parent.children.remove(this); return parents.remove(parent); }
public void removeAllChildren() { Iterator<TreeNode> it = children.iterator(); while (it.hasNext()) { TreeNode child = it.next(); child.parents.remove(this); } children.clear(); }
public int getNum() { return num; }
public Collection<TreeNode> getChildren() { return children; }
public int getParentsNum() { return parents.size(); } @Override public int compareTo(MartianCouncil.TreeNode nd) { return Math.round(Math.signum(this.getParentsNum() - nd.getParentsNum())); } }
public class TreeNodeComp implements Comparator<MartianCouncil.TreeNode> { @Override public int compare(MartianCouncil.TreeNode nd1, MartianCouncil.TreeNode nd2) { return Math.round(Math.signum(nd1.getParentsNum() - nd2.getParentsNum())); } }
} Take a look at the numbers 11010010001 "1".So we have 1 at position 1,2,4,7,and so on and because every time one 0 is addesd this numbers increses like sum of the numbers from 1,2,3,4,..+n +1; so sum of this sequance 1+2+3+...n=n(n+1)/2. And look at our sequence 2+4+7+...n=n(n+1); And from here you can get this formula (sqrt(8*n-7)-1)/2
Edited by author 10.08.2009 16:54 Excuse me.. But how could you found out that formula? just use this logic if (Math.sqrt((8*(num))-7)-1)/2))%1==0 print "1" else print "0" and watch the range of the input ;D please help me, Me programm 3 test time limit exceeded.. Program is this package acm.timus.ru; import java.util.Scanner; public class N_1209 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int i=1,a=0,k=0; for (int j=1; j<=n; j++) { a=sc.nextInt(); if (i<a) { while(i<a){ k++; i+=k; } }else{ while(i>a){ i-=k; k--; } } if (i==a) { System.out.print("1"+" "); } else{System.out.print("0"+" ");}
} } } Hi Here is the correct formula: (sqr(n)+n+2)/2 (for n from 0 to ...) If one needs I'll explain it. Edited by author 06.05.2014 21:12 Thank you very much!I got AC! Excuse me, but how sum of (1+2+3+...+n) can be less than (2+4+7+...+n)? Excuse me, but how I can use your formula? I have another way to explain this. That's not a correct one, but I got A anyway: The indexes of the "1" are 1,2,4,7,11,..., which are 1+(0), 1+(1), 1+(1+2), 1+(1+2+3), ... , 1+((k+1)*k/2), ... Those are "partial sums" of the sequence (1,2,3,4,5,...,k,...) We have an input index N and we are not sure, if there is some X for which N == 1+((X+1)*X/2). You'll have to find this X via some arithmetical operations (you can even ignore some numbers in formulas). As if X is an index in the sequence (1,2,3,4,5,...,k,...), you should find out, if it is an Integer number. Too many info, i think. Thank u for your clever explanation! try this test wydh smdb dmks piwo 3 dhbdmy wsmydd dhbdmy ans: dhbdmy: YES wsmydd: YES dhbdmy: YES Can you give some tests? abcda 5 palindrome? 1 5 palindrome? 1 1 change 4 b palindrome? 1 5 palindrome? 2 4 |
|