Общий форум#include <iostream> #include <cmath> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #define f first #define s second #define ll long long #define ld long double #define ull unsigned long long #define pb push_back #define ppb pop_back #define mp make_pair #define sz(x) (int) x.size() #define all(x) x.begin(), x.end() #define bit(x) __builtin_popcountll(x) #define sqr(x) ((x) * 1LL * (x)) #define nl '\n' #define ioi exit(0); #define NeedForSpeed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define _7day "IOI2017" using namespace std; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair <ld, ld> pdd; typedef pair <ll, ull> hack; const int N = 1e5 + 7, MxN = 1e6 + 7, mod = 1e9 + 7, inf = 1e9 + 7; const long long linf = (ll)1e18 + 7; const long double eps = 1e-15, pi = 3.141592; const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; int n, x; priority_queue <unsigned int> q; int main(){ #ifndef _7day freopen (_7day".in", "r", stdin); freopen (_7day".out", "w", stdout); #endif cin >> n; for (int i = 1; i <= n / 2 + 1; ++i) cin >> x, q.push(x); for (int i = n / 2 + 2; i <= n; ++i){ cin >> x; if (x < q.top()) q.pop(), q.push(x); } if (n % 2) printf ("%d.0", q.top()); else{ unsigned int top1 = q.top(); q.pop(); if ((top1 + q.top()) % 2 == 0) cout << top1 / 2 + q.top() / 2 << ".0"; else cout << top1 / 2 + q.top() / 2 << ".5"; } ioi } Edited by author 01.12.2016 20:51 Edited by author 01.12.2016 20:51 Priority queue is based on vector. When vector resizes, 2 data blocks can be allocated in one time. Let it resizes at n/2 size/capacity. Next data block capacity (assuming resize factor 1.5) is 3n/4. Even second block can cause MLE. Both blocks size is 5*n/4 - MLE. You should avoid resizing. You should better set fixed vector size (approx n/2+2) and use heap functions over it. Please can you show, how it can be done. I can't do it Edited by author 03.12.2016 14:16 I tried to do like this but whatever it's got MLE 7 vector <unsigned int> vec; vec.reserve(n / 2 + 2); priority_queue <unsigned int> q (less<unsigned int>(), vec); Google std::make_heap / std::push_heap / std::pop_heap Don't use priority queue at all std::nth_element. but need a little tricky for n <= 2*10^5 --> read all numbers and use direct nth_element (or your own implementation of Kth order statistics). for n > 2*10^5 --> read only 2*10^5 numbers and use nth_element for n - 2*10^5, and remove first n - 2*10^5, i.e. rewrite remain numbers to first n-2*10^5, and again use nth_element for n/2 - (n- 2*10^5) Good Luck!!! I see you got AC, but this description looks suspicious anyway. Let we need to process 9 elements [1,2,3,4,5,6,7,8,9] in any order (so expected answer is 5), and have 5 elements array. As I understand steps should be: for n > 5 --> (1)read only 5 numbers and use nth_element for n - 5, (2) remove first n - 5, i.e. rewrite remain numbers to 5, (3) again use nth_element for n/2 - (n- 5) Am I correct? Let see numbers in order [1,2,5,8,9,3,4,6,7] Read first 5, find (9-5) = 4th: [1,2,5,8,9] => 9 (or 8), rest removed. 5 removed, wont be answer, fail Could you please show your code? #include<stdio.h> int main(void){ int a, b; scanf("%d %d", &a, &b); if(a%2==0 || b%2!=0) puts("yes"); else if(a%2!=0 || b%2==0) puts("no"); return 0; } else puts("no"); is enough Who know O(H*W) or, O(H*W*5) algo ??? С 20 не проходит 15 тест!!! PS. У меня тесты проходит PSS. базовые) #include<iostream> #include<vector> using namespace std; int test(int a); vector<int> list; //vector<int> answers; int main() { list.push_back(0); list.push_back(1); int N; cin >> N; while (N != 0) { cout << test(N); // answers.push_back(test(N)); cin >> N; } //for (int i = 0; i < answers.size(); i++) // cout << answers[i] << endl; return 0; } int test(int a) { int i = 2; if (a < list.size() && a % 2 == 0) return list[a - 1]; else if (a < list.size() && a % 2 != 0) return list[a]; else if (a >= list.size()) i = list.size(); for (i; i <= a; i++) { if (i % 2 == 0) list.push_back(list[i / 2]); else list.push_back(list[i / 2] + list[i / 2 + 1]); } if (a % 2 == 0) return list[a - 1]; else return list[a]; } Edited by author 11.01.2017 16:19 Edited by author 11.01.2017 16:21 the same c++ code was accepted when I used visual studio, but gave TLE 8 with g++ 9 (or g++ 9, C11) Same happened to me. I've removed everything from the code except scanf("%d", &t); and it took 1.9 seconds just to read test #8 input using G++. Same code runs in 0.25 seconds under Visual C++ 2010. Thank you. I also scaned with scanf("%d", &t) and got ~1.5+/- sec using G++ and ~0.3 sec using VC++. Another idea to optimize is to scan with gets() and hex values. Edited by author 28.09.2016 00:02 Edited by author 28.09.2016 00:02 i try to solve it with kruskal and it gives me wa6 can anyone help me? struct edge { int len; int a; int b; }; int rank[1024]; int parent[1024]; edge e[15008]; int n,m; int get_parent(int x) { if (x != parent[x])parent[x] = get_parent(parent[x]); return parent[x]; } bool join_dsu(int x, int y){ x = get_parent(x); y = get_parent(y); if (x == y) return false; if (rank[x] < rank[y]) parent[x] = y; else if (rank[x] > rank[y]) parent[y] = x; else parent[y] = x, ++rank[x]; return true; } // read edges // sort edges by len // p[i] = i, for i = 1..n // let l_min = 0; // i = 1..m // if ( join_dsu( e[i].a, e[i].b) ){ l_min = e[i].len; } // print l_min and all edges where len <= l_min, GOOD LUCK !! type Ta=record x,y:extended; end; var a,b,c:Ta; ab,ac,bc,p,s,h,m:extended; l:extended; function max(a,b:extended):extended; begin if a-b>0 then max:=a else max:=b; end; begin readln(a.x,a.y,b.x,b.y); readln(c.x,c.y,l); ac:=sqrt(sqr(a.x-c.x)+sqr(a.y-c.y)); ab:=sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)); bc:=sqrt(sqr(b.x-c.x)+sqr(b.y-c.y)); p:=(ab+ac+bc)/2; s:=sqrt(p*(p-ac)*(p-ab)*(p-bc)); if ab=0 then h:=ac else h:=2*s/ab; if h-l>0 then writeln(h-l:0:2) else writeln('0.00'); m:=max(ac,bc); if m-l>0 then writeln(m-l:0:2) else writeln('0.00'); end. Edited by author 03.08.2008 16:37 Try this test: -1 1 1 1 -4 -3 0 Correct answere is 5.00 6.40 I have: var ab,ca,cb,p,s1,s2,Streyg,cosA,cosB:real; Xa,Ya,Xb,Yb,Xc,Yc,L:integer; Begin Readln(Xa,Ya,Xb,Yb,Xc,Yc,L); ab:=sqrt(sqr(Xa-Xb)+sqr(Ya-Yb)); ca:=sqrt(sqr(Xa-Xc)+sqr(Ya-Yc)); cb:=sqrt(sqr(Xb-Xc)+sqr(Yb-Yc)); p:=(ab+ca+cb)/2; Streyg:=(sqrt(p*(p-ab)*(p-ca)*(p-cb)));
if ((ca>cb) and (L>=ca)) or ((ca<cb) and (L>=cb)) or ((Xa=Xb) and (Ya=Yb) and (L>=ca)) then begin s1:=0; s2:=s1; end else if (Xa=Xb) and (Ya=Yb) then begin s1:=ca; s2:=s1; end else begin if (Streyg=0) or (L>=ca) or (L>=cb) or (L>=2*Streyg/ab) then s1:=0 else begin cosA:=(ca*ca+ab*ab-cb*cb)/(2*ca*ab); cosB:=(ab*ab+cb*cb-ca*ca)/(2*ab*cb); if (cosA>=0) and (cosB>=0) then s1:=2*Streyg/ab-L else if ca>cb then s1:=cb-L else s1:=ca-L; end; if ca>cb then s2:=ca-L else s2:=cb-L; end;
writeln(s1:3:2); write(s2:3:2);
End. give me some test Why 5. Why not 4? Основание перпендикуляра не попадёт в отрезок. А по теореме Пифагора получаем для катетов 3 и 4 гипотенузу 5 import java.util.Scanner; public class Problem_1348 { public static void main(String[] args) { Scanner in=new Scanner(System.in); int ax,ay,bx,by,cx,cy,l; double ac,bc,ab,x,min,A,B,C,h; x=h=0; ax=in.nextInt(); ay=in.nextInt(); bx=in.nextInt(); by=in.nextInt(); cx=in.nextInt(); cy=in.nextInt(); l=in.nextInt();
ab= Math.sqrt( (bx-ax)*(bx-ax)+(by-ay)*(by-ay) ); ac =Math.sqrt( (cx-ax)*(cx-ax)+ (cy-ay)*(cy-ay) ); bc= Math.sqrt( (cx-bx)*(cx-bx) + (cy-by)*(cy-by) ); A=ac; B=bc; C=ab;
x=(A*A-B*B+C*C)/(2*C); h= Math.sqrt((A*A-x*x)); min=h;
if(A>B){ if(min-l<0)System.out.printf("%.2f",0.00); else System.out.printf("%.2f",min-l); System.out.println(); if(A-l<0)System.out.printf("%.2f",0.00);else System.out.printf("%.2f",A-l); }else{ if(min-l<0)System.out.printf("%.2f",0.00); else System.out.printf("%.2f",min-l); System.out.println(); if(A-l<0)System.out.printf("%.2f",0.00);else System.out.printf("%.2f",B-l); }
} } Edited by author 06.05.2014 16:57 Если основание высоты не попадает в отрезок AB, то такого расстояния не хватит #include <iostream> #include <string.h> int main() { char grif[1001][202],sliz[1001][202],huff[1001][202],rave[1001][202],name[202],group[20]; int n,i,gcur=0,scur=0,hcur=0,rcur=0; std:: cin >> n; while( n>=0 ) { std:: cin.getline(name,202); std:: cin.getline(group,20);
if(!strcmp(group,"Slytherin")) { strcpy(sliz[scur],name); scur++; } if(!strcmp(group,"Hufflepuff")) { strcpy(huff[hcur],name); hcur++; } if(!strcmp(group,"Gryffindor")) { strcpy(grif[gcur],name); gcur++; } if(!strcmp(group,"Ravenclaw")) { strcpy(rave[rcur],name); rcur++; }
--n; }
std::cout<<"Slytherin:\n"; for( i=0; i<scur; ++i ) std:: cout << sliz[i] << '\n'; std::cout<<'\n';
std::cout<<"Hufflepuff:\n"; for( i=0; i<hcur; ++i ) std:: cout << huff[i] << '\n'; std::cout<<'\n';
std::cout<<"Gryffindor:\n"; for( i=0; i<gcur; ++i ) std:: cout << grif[i] << '\n'; std::cout<<'\n';
std::cout<<"Ravenclaw:\n"; for( i=0; i<rcur; ++i ) std:: cout << rave[i] << '\n'; std::cout<<'\n';
return 0; } This is madness! Don't write such code =) int main() { int students; cin >> students; map< string, vector<string> > house_to_students; for (int student = 0; student < students; student++) { cin.ignore();
string student_name; getline(cin, student_name); string house; cin >> house; house_to_students[house].push_back(student_name); } for (auto house : { "Slytherin", "Hufflepuff", "Gryffindor", "Ravenclaw" }) { cout << house << ":" << endl;
for (auto student : house_to_students[house]) cout << student << endl; cout << endl; } } It happened bcs of cin function In the input they give n and endl after n; so, somehow getline reads this endl as a string, so you can do this insted of "cin>>n" use "scanf("%d\n", &n); #include <iostream> using namespace std; int main() { int n, res = 0; cin >> n; if (n < 0) { for (int i = -2; i >= n; i--) { res = res + i; } cout << res; } else { for (int i = 1; i <= n; i++) { res = res + i; } cout << res; } return 0; } int i = -2; i >= n; i-- i = 1 Optimization trick with initial i=-2 is funny. But now you aren't processing case "n == 0" correctly. if(n == 0) --> ans = ? bro.. i don't understand... i'm from vietnam Lets read task: > sum of all integer numbers lying between 1 and N inclusive If N==0 then set of integers to sum is [0..1], sum is 1. Algo which I wrote is O(n*logn) but also I know O(n) one. Try to choose one point which will be on the circle. O(1) Than try to choose another one point which will be on the circle. O(n*logn) or O(n) Finally build circle by these two points. O(n) PS: I used such two points that all other points are at one side of the line which contains these two points. I not understand why your algo is right. Maybe we'll discuss some questions by e-mail. Mine is victorbarinov@ua.fm Thanks! My simple linear solution uses Inversive geometry. 1. Inverse plane with respect to one of points (let it is A). O(n). 2. Chose another (not A) most left point (let it is B). O(n). 3. Sort all points without A and B with respect to B by polar angle and chose middle (let it is C). We want to get exactly one element on middle place, so I used std::nth_element which work O(n). 4. Output A, B, C. O(1). Thx @Ripatti [Ufa]. A solve with O(n) , got AC 0.001s. Hint for others: 1. did not need any math functions, like acos, atan. use simple vector multiplications. 2. calc cos(v[i]) , i>2. values before sorting. 3. use std::nth_element 4. if need more speed, use buffered i/o. Good Luck!! I can guarantee that the above algorithm is working. I came up with a similar algorithm :) Very short,AC in 0.484s. Not so fast as I expected. C/C++ Karatsuba gives 0.015s!!! Please someone tell me where is the wrong in my code. #include <iostream> #include <queue> #include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; struct dott{ int x; int y; }; int main() { char str[8]; gets(str); if(strlen(str) <= 2){ int a, b, n; n = atoi(str); bool XY[200][200]; memset(XY, 0, sizeof(XY)); struct dott center; cin>>a>>b; center.x = a; center.y = b; for(int i=2; i<=n; i++){ cin>>a>>b; XY[a][b] = 1; } queue<dott> qu; qu.push(center); cout<<center.x<<" "<<center.y<<"\n"; while(!qu.empty()){ dott pix = qu.front(); qu.pop(); if(XY[pix.x + 1][pix.y] == 1){ cout<<"R"; center.x = pix.x + 1; center.y = pix.y; qu.push(center); XY[pix.x + 1][pix.y] = 0; } if(XY[pix.x][pix.y + 1] == 1){ cout<<"T"; center.x = pix.x; center.y = pix.y + 1; qu.push(center); XY[pix.x][pix.y + 1] = 0; } if(XY[pix.x - 1][pix.y] == 1){ cout<<"L"; center.x = pix.x - 1; center.y = pix.y; qu.push(center); XY[pix.x - 1][pix.y] = 0; } if(XY[pix.x][pix.y - 1] == 1){ cout<<"B"; center.x = pix.x; center.y = pix.y - 1; qu.push(center); XY[pix.x][pix.y - 1] = 0; } if(!qu.empty()){ cout<<",\n"; } else{ cout<<"."; } } } else{ int a, b, count=0; bool XY[200][200]; memset(XY, 0, sizeof(XY)); struct dott centre; char *s; s = strtok(str," "); a = atoi(s); s = strtok(NULL," "); b = atoi(s); XY[a][b] = 1; centre.x = a; centre.y = b; queue<dott> qu; qu.push(centre); string str[200]; for(int i=0; ;i++){ cin>>str[i]; if(str[i] == "."){ break; } count++; } for(int i=0; i<count; i++){ dott S = qu.front(); qu.pop(); for(int j=0; j<str[i].size(); j++){ if(str[i][j] == 'R'){ XY[S.x+1][S.y] = 1; centre.x = S.x+1; centre.y = S.y; qu.push(centre); } if(str[i][j] == 'T'){ XY[S.x][S.y+1] = 1; centre.x = S.x; centre.y = S.y+1; qu.push(centre); } if(str[i][j] == 'L'){ XY[S.x-1][S.y] = 1; centre.x = S.x-1; centre.y = S.y; qu.push(centre); } if(str[i][j] == 'B'){ XY[S.x][S.y-1] = 1; centre.x = S.x; centre.x = S.y-1; qu.push(centre); } } } cout<<count+1<<"\n"; for(int i=0; i<100; i++){ for(int j=0; j<100; j++){ if(XY[i][j] == 1){ cout<<i<<" "<<j<<"\n"; } } } } return 0; } Edited by author 09.01.2017 17:37 Please someone tell me where maximum people make mistake in this problem. Or please provide some hints. #include<stdio.h> #include<string.h> char s[100]; bool map[200][200],bo; int n,m,x,y,xx,yy,a[11000],b[11000],c,zy; void find(int xx,int yy,int xy) { bool l,r,s,x; l=r=s=x=false; map[xx][yy]=false; if (map[xx+1][yy]) {printf("R");map[xx+1][yy]=false;c++;a[c]=xx+1;b[c]=yy;} if (map[xx][yy+1]) {printf("T");map[xx][yy+1]=false;c++;a[c]=xx;b[c]=yy+1;} if (map[xx-1][yy]) {printf("L");map[xx-1][yy]=false;c++;a[c]=xx-1;b[c]=yy;} if (map[xx][yy-1]) {printf("B");map[xx][yy-1]=false;c++;a[c]=xx;b[c]=yy-1;} if (xy==c) return; printf(",\n"); if (xy<c) find(a[xy+1],b[xy+1],xy+1);
} int main() { memset(map,false,sizeof(map)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); gets(s); if (strlen(s)==1) { n=s[0]-48; for (int i=1;i<=n;i++) { scanf("%d%d",&x,&y); if (i==1) { xx=x; yy=y; } map[x][y]=true; } a[1]=xx; b[1]=xx; c=1; printf("%d %d\n",xx,yy); find(xx,yy,1); printf(".\n"); } else { x=s[0]-48; y=s[2]-48; map[x][y]=true; zy=1; c=1; while (true) { gets(s); if (s[0]=='.') break; for (int i=0;i<=strlen(s);i++) { if (s[i]=='R') { c++;a[c]=x+1;b[c]=y; map[x+1][y]=true; } else if (s[i]=='T') {map[x][y+1]=true;c++;a[c]=x;b[c]=y+1;} else if (s[i]=='L') {map[x-1][y]=true;c++;a[c]=x-1;b[c]=y;} else if (s[i]=='B') {map[x][y-1]=true;c++;a[c]=x;b[c]=y-1;} } zy++; x=a[zy]; y=b[zy]; } printf("%d\n",zy); for (int i=1;i<=10;i++) for (int j=1;j<=10;j++) if (map[i][j]) printf("%d %d\n",i,j); } } Edited by author 09.01.2017 16:46 I feel wonderful.:) Hi Clever Can you tell me your Email Address??? I've written a mail to you, but the chinese e-mail server is all borken...(there is some virus..) So I can't send the mail to you :( My mail address:vhappy@netease.com mine is 0.015 or better IT's not difficult to do it in 0.015s.=) Can you just say what's the idia of these fast solutions :? I want to know too...how does it do it so fast? wow, can you mail me my id is shlakshmanan@inautix.co.in There exists O(256*16*9) solution with 65536 byte dp[] memory..... a little hint: separate flipping cells in the 1- and 2- rows with flipping cells in the 3 - and 4- rows . Edited by author 09.01.2017 12:52 Can anybody explain me why one program can have different execution time with the same code? My friend with my code got 0.015 but I got 0.031. Oops =) I send it one more time and get 0.015. Maybe it because of server... Edited by author 22.11.2010 21:59 I had 0.031 sec, but now I finally got 0.015 sec! I've just replaced four loops with one loop. Probably depends on server current loading. |
|