Общий форумI do not argue, just I want to know. Thanks. Because it can be solved using data structure called "stack". Now I got it :) Thank you very much. Stack<char> Stack = new Stack<char>(); foreach (char c in Console.ReadLine()) if (Stack.Count > 0 && Stack.Peek() == c) Stack.Pop(); else Stack.Push(c); foreach (char c in Stack.Reverse()) Console.Write(c); Because of this code. After AC i realized i used stack :) Yes, and because choosing the right data structure makes the problem solvable, and choosing the wrong one results in TLE. I had a brain blockage and couldn't think of stacks here (it's been a long time since I used them). Trying to solve using, for example, STL vector or dequeue and erase() will result in TLE on the big input cases. I realized that since very many erase() operations may be performed, delete performance is critically important, so STL list came to the rescue and got AC. Cuz' String Or a Char array is a data structure and working with data structures actually means changing adding expanding etc. them :) Hope that'll help you Use stack for those who know what stack means. For those who don't , take a look at this: n++; cin>>a[n]; if (a[n]==a[n-1]) n-=2; How input is <wwstdaadierfflitzzz>,but output <stierlitz>? This is wrong,according case 2 of this problem... Your task is to "remove from the message all pairs of identical letters" only. Ok.There are two and two <t> of the word - < stierlitz >. Ok.There are two i and two t of the word - stierlitz. The task is to undo step 3, which involves *repeatedly* inserting letter pairs. This results in more than just aabbcc... #include <stdio.h> #include <conio.h> main() { int n, k, a, b, c, tmp; scanf("%d%d", &n, &k); scanf("%d%d%d", &a, &b, &c); if (k>=1) if(n<=100) if ((a+b+c)<n*k) tmp=0; else tmp=(a+b+c)-n*k;
printf("%d", tmp); getch(); } In test #15 there are radiobeacons which don't have correct location on the plane (i.e. no one of points 1 <= x,y <= 200 is acceptable) but their signals are identified by checkpoints. In the problem statement we can find, that "N radiobeacons are located on the plane" and "we know ... that their coordinates are integers from 1 to 200". So, I believe that such test cases are incorrect. Yes, my assert also falls. "either the previous or the next ticket is lucky." and "one step from happiness" lucky means: previous OR next, sum of first three digits EQUAL sum of last three digits. Edited by author 03.10.2012 20:54 а что на русском темы писать нельзя и задачи Потому что вы безграмотно на нём пишете. #include <stdio.h> #include <stdlib.h> main() { int i,j,k,t; int sum; int max; int mark; int a[22]; int* x; x=(int*)malloc(1048578*sizeof(int)); scanf("%d",&a[0]); for(i=1,sum=0;scanf("%d",&t)!=EOF;i++) { for(j=1;j<=i-1 && t>a[j];j++); for(k=i-1;k>=j;k--){a[k+1]=a[k];} a[j]=t; sum+=t; } x[0]=1;x[1]=0; for(i=1;i<=a[0];i++) { for(j=1,mark=0,max=0;j<=x[0];j++) { x[j+x[0]]=x[j]+a[i]; if(x[j]+a[i]<=sum/2) max=max>x[j]+a[i]?max:x[j]+a[i]; } x[0]*=2; } printf("%d",sum-2*max); } I test it with a large data but can't find error... why WA 6, i search two pair, if pair=2, then count++ #include <queue> #include <iostream> #include <set> #include <cstdio> #include <algorithm> #include <string> #include <map> #include <string> #include <stack> #include <sstream> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; char a[10000][6]; void swap(char &a,char &b) { if(a>b) { char c; c=a; a=b; b=c; } } int amx(int a,int b) { if(a>b) { return(a); } else return (b); } int main() { int n; cin>>n; for(int i=0;i<n;i++) { cin>>a[i][0]; cin>>a[i][2]; cin>>a[i][3]; cin>>a[i][1]; cin>>a[i][4]; cin>>a[i][5]; } /* for(int i=0;i<n;i++) { for(int j=0;j<6;j++) cout<<a[i][j]<<" "; cout<<endl; } */
int max1=0; for(int i=0;i<n;i++) { int coutn1=0,count2=0,count3=0; for(int j=0;j<n;j++) { //if(i!=j) { if( (((a[i][0]==a[j][0] && a[i][1]==a[j][1])|| (a[i][0]==a[j][2] && a[i][1]==a[j][3]) || (a[i][0]==a[j][4] && a[i][1]==a[j][5])) && ((a[i][2]==a[j][0] && a[i][3]==a[j][1])|| (a[i][2]==a[j][2] && a[i][3]==a[j][3]) || (a[i][2]==a[j][4] && a[i][3]==a[j][5]) )) || (((a[i][1]==a[j][0] && a[i][0]==a[j][1])|| (a[i][1]==a[j][2] && a[i][0]==a[j][3]) || (a[i][1]==a[j][4] && a[i][0]==a[j][5])) && ((a[i][2]==a[j][0] && a[i][3]==a[j][1])|| (a[i][2]==a[j][2] && a[i][3]==a[j][3]) || (a[i][2]==a[j][4] && a[i][3]==a[j][5]) )) || (((a[i][1]==a[j][0] && a[i][0]==a[j][1])|| (a[i][1]==a[j][2] && a[i][0]==a[j][3]) || (a[i][1]==a[j][4] && a[i][0]==a[j][5])) && ((a[i][3]==a[j][0] && a[i][2]==a[j][1])|| (a[i][3]==a[j][2] && a[i][2]==a[j][3]) || (a[i][3]==a[j][4] && a[i][2]==a[j][5]) )) || (((a[i][0]==a[j][0] && a[i][1]==a[j][1])|| (a[i][0]==a[j][2] && a[i][1]==a[j][3]) || (a[i][0]==a[j][4] && a[i][1]==a[j][5])) && ((a[i][3]==a[j][0] && a[i][2]==a[j][1])|| (a[i][3]==a[j][2] && a[i][2]==a[j][3]) || (a[i][3]==a[j][4] && a[i][2]==a[j][5]) )) ) { coutn1++; } if( (((a[i][0]==a[j][0] && a[i][1]==a[j][1])|| (a[i][0]==a[j][2] && a[i][1]==a[j][3]) || (a[i][0]==a[j][4] && a[i][1]==a[j][5])) && ((a[i][4]==a[j][0] && a[i][5]==a[j][1])|| (a[i][4]==a[j][2] && a[i][5]==a[j][3]) || (a[i][4]==a[j][4] && a[i][5]==a[j][5]) )) || (((a[i][1]==a[j][0] && a[i][0]==a[j][1])|| (a[i][1]==a[j][2] && a[i][0]==a[j][3]) || (a[i][1]==a[j][4] && a[i][0]==a[j][5])) && ((a[i][4]==a[j][0] && a[i][5]==a[j][1])|| (a[i][4]==a[j][2] && a[i][5]==a[j][3]) || (a[i][4]==a[j][4] && a[i][5]==a[j][5]) )) || (((a[i][0]==a[j][0] && a[i][1]==a[j][1])|| (a[i][0]==a[j][2] && a[i][1]==a[j][3]) || (a[i][0]==a[j][4] && a[i][1]==a[j][5])) && ((a[i][5]==a[j][0] && a[i][4]==a[j][1])|| (a[i][5]==a[j][2] && a[i][4]==a[j][3]) || (a[i][5]==a[j][4] && a[i][4]==a[j][5]) )) || (((a[i][1]==a[j][0] && a[i][0]==a[j][1])|| (a[i][1]==a[j][2] && a[i][0]==a[j][3]) || (a[i][1]==a[j][4] && a[i][0]==a[j][5])) && ((a[i][5]==a[j][0] && a[i][4]==a[j][1])|| (a[i][5]==a[j][2] && a[i][4]==a[j][3]) || (a[i][5]==a[j][4] && a[i][4]==a[j][5]) )) ) { count2++; } if( (( (a[i][2]==a[j][0] && a[i][3]==a[j][1])|| (a[i][2]==a[j][2] && a[i][3]==a[j][3]) || (a[i][2]==a[j][4] && a[i][3]==a[j][5])) && ((a[i][4]==a[j][0] && a[i][5]==a[j][1])|| (a[i][4]==a[j][2] && a[i][5]==a[j][3]) || (a[i][4]==a[j][4] && a[i][5]==a[j][5]) )) || (((a[i][3]==a[j][0] && a[i][2]==a[j][1])|| (a[i][3]==a[j][2] && a[i][2]==a[j][3]) || (a[i][3]==a[j][4] && a[i][2]==a[j][5])) && ((a[i][4]==a[j][0] && a[i][5]==a[j][1])|| (a[i][4]==a[j][2] && a[i][5]==a[j][3]) || (a[i][4]==a[j][4] && a[i][5]==a[j][5]) )) || (((a[i][2]==a[j][0] && a[i][3]==a[j][1])|| (a[i][2]==a[j][2] && a[i][3]==a[j][3]) || (a[i][2]==a[j][4] && a[i][3]==a[j][5])) && ((a[i][5]==a[j][0] && a[i][4]==a[j][1])|| (a[i][5]==a[j][2] && a[i][4]==a[j][3]) || (a[i][5]==a[j][4] && a[i][4]==a[j][5]) )) || (((a[i][3]==a[j][0] && a[i][2]==a[j][1])|| (a[i][3]==a[j][2] && a[i][2]==a[j][3]) || (a[i][3]==a[j][4] && a[i][2]==a[j][5])) && ((a[i][5]==a[j][0] && a[i][4]==a[j][1])|| (a[i][5]==a[j][2] && a[i][4]==a[j][3]) || (a[i][5]==a[j][4] && a[i][4]==a[j][5]) )) ) { count3++; }
} } max1=amx(amx(coutn1,count2),(max1,count3)); } cout<<max1;
return 0; } Edited by author 03.10.2012 09:29 Pleas Assist=) #include <iostream> #include <math.h> #include <iomanip> using namespace std; int main() { double arr[32768]; int i=0; double value=0; while(cin>>value) { if (value>0) { arr[i++]=sqrt(value); value=0; } else break;
} cout<<setprecision(4)<<fixed; while(i--) { cout <<arr[i]<<endl; } system("pause"); } Edited by author 15.09.2012 23:36 First of all the amount of numbers are about 129000 but not 32768 as you have written. Right, but if you're using C++ why not use a dynamic container like std::vector. If you don't know STL it's worth learning at least the STL containers. vector, map, and set in particular are immensely useful and bring C++ up to "higher" level language status than C++ without them. They're especially useful when you don't know the input limits beforehand because they grow automatically. Edited by author 18.09.2012 13:41 #include<iostream> #include<math.h> using namespace std; #pragma comment(linker, "/STACK:2000000") void worc(){ double k; if(scanf("%lf",&k)!=EOF){ worc(); printf("%.4lf\n",sqrt(k)); } } int main() { worc(); return 0; } Give me test 3? please... Give me your code, I shall try to find a mistake. Edited by author 02.10.2012 21:39 Wat for? This problem is very easy to solve, belive me=))) If it's easy, give me 3 test please)) #include <iostream> #include <cmath> using namespace std; int main() { int a, sum = 0; cin >> a; if( a > 0 ) { for ( int i = 1; i <= a; i++) sum += i; } else { for (int i = 1; i <= abs(a); i++) sum += i; sum = 1 - sum; } cout << sum; return 0; } hell whats problm with mine . #include <iostream> using namespace std; int main() { int A=0; double Ans=0; cin>>A; if(A<=10000) { if(A>=1) Ans=A*(A+1)/2; if(A<0) Ans=(A*-1)*(A-1)/2+1; cout<<Ans; } return 0; } What about less then 10000? WA 9? Floating types can return answer like 123e+10. So you have to do "int Ans" before got answ. In add: Just use formula s(a1,an)=(a1,an)/2*n like that
int(((1+n)/2)*(abs(1-n)+1))); Edited by author 24.08.2012 00:47 hello, whats problm with mine . #include <iostream> bool flag=true;//the number more than 0 int num=0; bool GetNum() { scanf("%d",&num); return num>=0; } int GetResoults() { //Sn=(a1+an)n/2 if (flag)//>0 { return (1+num)*num/2; } else//<0 { return (num+1)*((-num)+2)/2; } } int main() { flag=GetNum(); printf("%d\n",GetResoults()); return 0; } #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct Date { int day;
int month;
int year; }; struct person { int num;
int live;
Date die; }; bool comp(person a, person b) { if (a.live != b.live) return a.live > b.live; if (a.die.year != b.die.year) return a.die.year < b.die.year; if (a.die.month != b.die.month) return a.die.month < b.die.month; if (a.die.day != b.die.day) return a.die.day < b.die.day; } bool leap(int year) { if (year%400==0) return 1; if (year%100==0) return 0; if (year%4==0) return 1; else return 0; } int monthDay(int year, int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31;
else if (month == 4 || month == 6|| month == 9 || month == 11) return 30;
else if (leap(year))return 29;
else return 28; } int Distance(Date a, Date b)//a??¨®¨²b o??e¨º?¨®????1¨¨??¨² { int dis = 0;
for (int i = a.year+1; i < b.year; ++i) { if (leap(i)) dis += 366;
else dis += 365; }
if (a.year != b.year) { for (int i = a.month+1; i <= 12; ++i) { dis += monthDay(a.year, i); }
dis += (monthDay(a.year, a.month) - a.day + 1); for (int i = 1; i < b.month; ++i) { dis += monthDay(b.year, i); }
dis += b.day; } else { if (a.month != b.month) { dis += (monthDay(a.year, a.month) - a.day + 1);cout << monthDay(a.year, a.month) << endl;
for (int i = a.month+1; i < b.month; ++i) { dis += monthDay(b.year, i); }
dis += b.day; } else { dis += (b.day - a.day + 1); } }
return dis; } int convert(char a, char b) { return (a - '0')*10 + b - '0'; } int convert(char a, char b, char c, char d) { return (a - '0')*1000 + (b - '0')*100 + (c - '0')*10 + d - '0'; } void print(Date a) { cout << a.year << " " << a.month << " " << a.day << endl; } int main() { int m;
cin >> m;
char useless[11];
Date a, b;
person p[101];
for (int i = 0; i < m; ++i) { scanf("%d.%d.%d", &a.day, &a.month, &a.year);
cin >> useless;
scanf("%d.%d.%d", &b.day, &b.month, &b.year);
p[i].num = i+1; p[i].live = Distance(a, b);
p[i].die = b; }
sort(p, p+m, comp);
cout << p[0].num << endl;
system("pause"); } I don't know why it always gets wrong answer; i know………… it's my fault... because i cout more sth. Time limit decreased from 1 second to 0.5 second (it corresponds better to the original TL in 2004). The problem was rejudged. Many old solutions with TLE verdict got AC, while all new solutions working in time from 0.5 to 1 second got TLE. n , k 1. if ( n = 0 ) , then answer = ( 1 ). 2. if ( n = 1 ) , then answer = ( k-1 ). 3. else answer(n) = ( k-1 ) * ( answer(n-1,k) + answer(n-2,k) ). Edited by author 20.06.2010 04:04 n>=2 !!! Okay, I'll join to Captains Obvious. You can find an answer with O(1) memory and O(logN) time. Can you explain this algo? Really interesting to know better one. Thx. Okay, I'll join to Captains Obvious. You can find an answer with O(1) memory and O(logN) time. Sure, you're welcome. Okay, we're already know, that the answer can be found reccurently: F(N) = (K-1)*(F(K-1)+F(N-2)), where F(0) = 1, F(1) = K-1. We see, that the {F(N)} sequence is very similar to Fibonacci's one and can assume some facts. I see two different approaches to solve the problem. 1) First way is to build characteristic equation, solve it and get an exact formula for F(N). I didn't try this way, because I foresaw some problems with precision and float calculations. If you're interested, you can turn to this article as a manual: http://www.intuit.ru/department/algorithms/algocombi/8/2.html 2) I assumed, that method with fast matrix exponentiation will work as well as for Fibonacci's numbers. Just consider a matrix: L(N) = {{F(N+1), F(N)}, {F(N), F(N-1)}}. Let's try to find such R, that L(N)*R = L(N+1). If you write down this information, you'll easily see, that R = {{K-1, 1}, {K-1, 0}}. So all you need is to find L(1)*R.pow(N-2). Hope this information was helpful, I'm not sure it is the best way to describe an approach though. :-) Edited by author 16.08.2010 13:34Actually, I implemented only O(N)-solution before. Today I wrote a code for a fast exponentiation, but (it seems I don't know java well) my time increased in #1013, where BigInteger is needed. Strange. :-) Artem Khizha [DNU] Thx for explanations and idea! I got it! Really nice approach with matrixes! #include <iostream> using namespace std; unsigned long stepen(unsigned long K,unsigned long N) { unsigned long i, result=1; if (N<0) return 0; else { i=1; for(; i<=N; i++) result*=K; return result; } } int main() { unsigned long N, K, i, result; cin>>N; cin>>K; result=stepen(K, N)-(stepen(K,N-2)+stepen(K, N-1)-1); cout<<result<<endl; //system("Pause"); return 0;
} WA #2. Why? >>dima11221122 Firstly, I came up with something like that and got WA2 too. N = (k-1)^n + n(k-1)^(n-1) - that formula looks pretty alright; but it isn't. It assumes there can't be more than one zero in the number, while there obviously can. The actual answer is something like that - N = (k-1)^n + n * (П(i=1 to min(k,n)) of (k-i)^(n-1)) and i think it's anyways easier to solve that like original poster did. Btw, your code is sooooo bad; u might wanna do something about that. Edited by author 26.11.2011 06:42 n , k 1. if ( n = 0 ) , then answer = ( 1 ). 2. if ( n = 1 ) , then answer = ( k-1 ). 3. else answer(n) = ( k-1 ) * ( answer(n-1,k) + answer(n-2,k) ). Edited by author 20.06.2010 04:04 I am not sure, how the number of zero digit numbers (of course, without two zeros in it) become 1. Edited by author 29.03.2012 02:38Can anybody tell me where can i get some info about this topic(k-based numbers), I really don't get from where cames this recurrent formula? Greetings How can we get valid n-digit number? Trivially, we can append each of (k-1) possible digits (k digits except for 0) to the front of each of (n-1)-digit valid numbers. But we can also form valid number this way: append 0 to the front of (n-2)-digit valid number, and then append (k-1) possible digits, as in the first case. This gives us F(n) = (k-1)*F(n-1)+(k-1)*F(n-2), where F is the function to yield the amount of valid n-digit numbers. I'd recommend you to ensure this yields only the valid numbers, think why can't it produce duplicates, and whether other means to construct valid numbers are possible. Then you follow Artem Khizha's [DNU] recipe and get an AC (= Edited by author 08.08.2012 19:05 > Amir Aupov [MIPT] I like your explanation. I approached the construction of N digit numbers from N-1 digit numbers in the typical way of constructing numbers: multiply N-1 digit number by base, and add in the new digits to the units position. This has the benefit of being clear and natural, so we can be sure that we're not missing any numbers. It has the disadvantage that you must track numbers that end in zero separately from those that don't. But this is still quite simple: Z(n): # of valid numbers ending in zero NZ(n): # of valid numbers not ending in zero Z(2) = k - 1 NZ(2) = (k - 1) * k - (k - 1) (total number of valid 2-digit numbers, minus those that end in 0) then, Z(n) = NZ(n - 1) NZ(n) = (Z(n-1) + NZ(n-1)) * (k - 1) Compute Z(n) and NZ(n) iteratively (DP), and then output F(n) at the end F(n) = Z(n) + NZ(n) HI David, The problem statement language is confusing. A "k-based number" is a number represented in base "k", that's all. There are a lot of tests (i know two: 4 and 9), in which r <= 0. Please, change it! Tests with r = 0 are fixed. Thank you. I do not understand what is wrong with my code it works ok on my PC: public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1")); char[] buffer = new char[262144]; br.read(buffer); Scanner in = new Scanner(new String(buffer)); Stack<String> lifo = new Stack<String>(); DecimalFormat format = new DecimalFormat("0.00000", new DecimalFormatSymbols(Locale.US)); while (in.hasNextLong()) { lifo.push(format.format(Math.sqrt(in.nextLong()))); } while(!lifo.empty()){ System.out.println(lifo.pop()); } } Could someone explain it to me ? Edited by author 30.09.2012 19:33 1: be careful of float point... 2: if there are K guys paid more than they drinked, the answer shouldn't be 100.0 / k, you should consider how much they paid... |
|