Общий форумYour memory limits are too tight for 64-bit compilers and using 32-bit Visual C++ instead is not such a good idea because of its weird behavior. Rust releases new version every 6 weeks. Even if this is too frequent please update it at least every half year Test #10 contains some non utf-8 characters. That could be the reason of panic if you use Go or Rust languages. Just pay attention when Conviviality rating < 0. can you be more precise? Thank you! 11 5 4 3 2 7 1 1 1 1 1 1 6 4 7 4 8 4 9 5 10 5 11 5 4 3 5 3 3 2 2 1 0 0 Answer:15 15 Edited by author 02.08.2013 07:51 Edited by author 02.08.2013 08:04 Edited by author 02.08.2013 08:04 Edited by author 02.08.2013 08:04 спасибо тебе, это реально помогло мне I got WA#13 .But my dfs was a little wrong and I got ac after correcting it. I think all of subtrees of the current node should be calculated before the current node is calculated. Edited by author 24.10.2017 20:03 Edited by author 24.10.2017 20:03 floor(x) or round(x) - wa7 (int)x - OK Thank you. It helped me pass WA7 Edited by author 16.01.2022 03:01 Can anybody describe the solution please ? Нужно по индукции показать, что если у нас одна куча из 2*N+1 камней (N>=0),то ходов всегда будет ровно N. А далее игра идёт параллельно в каждой из куч(неважно какое разбиение в каждой из куч,т.к. кол-во ходов одно и тоже). Побеждает первый игрок, если кол-во ходов нечётно, иначе-второй Если я правильно понимаю, то тут нужно применять метод полной математической индукции. Итак, нужно доказать, что 2*N + 1 разыгрывается за N ходов. 1. Базис. При N = 0. В этом случае 1 куча из 1 камня. Чтобы её разыграть нужно 0 ходов. 2. Допустим, для всех n = 1 .. k утверждение верно. Докажем для n = k + 1. 2*(k + 1) + 1 = 2*k + 1 + 2. Эту кучу можно разложить на кучи (1, 1, 2*k+1), т.е. затратить на разбор кучи итого k + 1 ход (1 ход чтобы разложить на (1, 1, 2*k + 1) и k ходов из предоположения индукции на 3-ю кучу). кучу 2*k + 1 + 2 можно разложить и другими способами, т.е. это будет набор (2*k1 + 1, 2*k2 + 1, 2*k3+1), при этом 2*k1 + 1 + 2*k2 + 1 + 2*k3 + 1 = 2*k + 3 => k1 + k2 + k3 = k, т.е. доказали и без того очевидное, что ki < k, т.е. попадает под предположение индукции, кроме того, что после первого хода нам потребуется ещё k ходов и итого будет k+1. куеде. My intuition... First, why not try to reduce the numbers as less as possible? That would seem like playing more turns. Well, for a pile of size n, this would mean making piles 1, 1, n-2, and the first two won't be used anymore so we could say we reduced n by 2. Is this useful? How many times can we do this? Well, floor(n/2) times, or as n is odd, (n-1)/2 times. Now keep this number. Let's try to split into arbitrary sizes, n = n1+n2+n3. The last expression is (n1+n2+n3-1)/2. How many times can we reduce by 2 with the split piles? All of them are odd, so this is (n1-1)/2 + (n2-1)/2 + (n3-1)/2 = (n1+n2+n3-1)/2 - 1. So no matter what we do for splitting, this number is always reduced by 1! And this number essentially measures how many turns we can still play, so that's all, if this number starts odd, Daenerys wins. Edited by author 16.01.2022 02:52 I noticed a lot of people complain about WA2. Just wanted to share my experience, since I got WA2 several times. Don't forget that after you read the number, you haven't read the newline after it. So cin.getchar + cin.ignore and getline(cin, ...) get that exact newline. So we must try to make teams have equal size. Why? Consider two teams with x and y members respectively, and consider x > y. Sending a member from first team to second team will only affect matches between these two teams. At first we have x*y different matches between the teams. If we send a player to the second team, there are now x-1 and y+1 members in each team. The matches are now (x-1)*(y+1) = x*y + (x-y) - 1 As we said x > y, x-y > 0, so x-y-1 >= 0 and our change can't reduce the total number of matches, so it is an optimal change. This also shows that nothing changes when x = y+1, so if we can't make all teams equal size, just add each of the ones remaining into a different team, making a difference of at most 1 in the sizes of teams. Now it's your job to compute the number of matches, I won't show that, but it can be done with a one line formula. Good luck :) I have solved this problem by using an easy greedy algorithm. MAIN: you need to use an idea of 2 pointers(of course you need to sort array) and take the rightest segment. Left part of segment need to be <= x(another pointer) The solution below: void solve() { int m; cin >> m; vector< pair<int, int> > pi; int a, b; cin >> a >> b; while (a || b) { pi.pb({a, b}); cin >> a >> b; } sort(all(pi)); int cnt = 0, j = 0; vector< pair<int, int> > ans; j = 0; int x = 0; for (; j < sz(pi) && x < m; j++) { int k = j; int mx = x; pair<int, int> par = {-INF, -INF}; while (k < sz(pi) && pi[k].ff <= x) { if (mx < pi[k].ss) { mx = pi[k].ss; par = pi[k]; } k++; } if (par.ff == -INF) { cout << "No solution\n"; return; } x = mx; ans.pb(par); if (k > j) j = k - 1; } if (x < m) { cout << "No solution\n"; return; } cout << sz(ans) << '\n'; for (auto &i : ans) { cout << i.ff << ' ' << i.ss << '\n'; } } always choose segments with rightest right points I dont understand why difficulty is 121, i think it's easy problem Not everyone is as super smart as you are, Anton. Us plebeians can barely walk upright, much less understand Western Arabic numerals. Congrats on your good fortune! I had a hard time with this, so hopefully I will save your time. Read input like this: int n; cin >> n; string t; getline(cin, t); // Necessary to process end of first line (nothing after n) .... // Now keep getting each line with getline(cin, t) Hints: 1. No sorting. It's useless. Quiqsort - too much memory, Bublesort - too long time. 2. We just need a number that is more than n/2. It always exists. 3. Read values step by step, remember the 'candidate' answer and counter increase or decrease it. e.g: 3->3 cnt=0+1=1 3->3 cnt=1+1=2 2->3 cnt=2-1=1 2->3 cnt=1-1=0 3->3 cnt=0+1=1 Note 1: e.g. 5 3 3 2 2 6 My program's answer is 6, but such a case never occurs (!). "He knows exactly that more than half banknotes have the same value." Note 2: C# - "Memory limit exceeded 21" use this: if (i % 1000 == 0) GC.Collect(); Regards Edited by author 12.01.2022 20:54 using System; namespace Timus { class Program { static void Main() { string nums = Console.ReadLine(); int a = Convert.ToInt32(nums.Substring(0, 1)); int b = Convert.ToInt32(nums.Substring(2, 1)); int c = a + b; Console.WriteLine(c); } } } #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main () { long long n; while (cin>>n) { cout<<setprecision(4)<<fixed<<sqrt(n)<<endl; } return 0; } Edited by author 12.11.2017 17:07 we have to print in reverse order. ТЫ ПРОСТО ЛОХ Тяжело Edited by author 12.01.2022 09:52 here you have to print the answer in the reverse order. This is my code!!! #include<iostream> #include<string> using namespace std; int main() { char s; int count = 0; while (cin >> s) { if ((s=='a')|| (s=='d')|| (s=='g')|| (s=='j')|| (s=='m')|| (s=='p')|| (s=='s')|| (s=='v')|| (s=='y')|| (s=='.')|| (s==' ')) {count+=1;} if ((s=='b')|| (s=='e')|| (s=='h')|| (s=='k')|| (s=='n')|| (s=='q')|| (s=='t')|| (s=='w')|| (s=='z')|| (s==',')) {count+=2;} if ((s=='c')|| (s=='f')|| (s=='i')|| (s=='l')|| (s=='o')|| (s=='r')|| (s=='u')|| (s=='x')|| (s=='!')) {count+=3;} } cout << count; system("pause"); return 0; } Possibly this part (s==' ')) If anyone is confused about this, note that cin ignores spaces. You should read the line as a whole with getline. string s; getline(cin, s); Soln accepted but time is .156 how to reduce to atleast 0.078 and also how to reduce memory usuage? import java.util.*; import java.io.*; public class prob1264{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); int m=sc.nextInt(); int result=n*(m+1); out.print(result); out.close(); } } Just use such construction, I have time 0.093: StreamTokenizer in = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); PrintWriter out = new PrintWriter( new OutputStreamWriter(System.out), true);
in.nextToken(); int n = (int)in.nval; in.nextToken(); int m = (int)in.nval;
out.println(n * (m + 1)); I know the problem! You're using Java. program p_e; const maxn=20000+1; type tgroup=record father,members:integer;end; ttree=array[1..maxn]of tgroup; tstack=array[1..maxn]of integer; pnode=^tnode; tnode=record node:integer; next:pnode; end; plist=array[1..maxn]of pnode; var n,k:longint; hands:longint; groups:ttree; ngroups:integer; stack:tstack; startstack,endstack:integer; list:plist; procedure Add(x,y:integer); var p:pnode; begin new(p); p^.node:=y; p^.next:=list[x]; list[x]:=p; end; procedure read_data; var ng:integer; index:integer; i:integer; son,mem:integer; begin readln(n,k); ngroups:=1; groups[1].father:=0; groups[1].members:=n; fillchar(list,sizeof(list),0); while not eof do begin read(index); read(ng); for i:=1 to ng do begin read(son,mem); Add(index,son); inc(ng); groups[son].father:=index; groups[son].members:=mem; end; end; end; procedure EliminateOneCouple(node:integer); begin if node=0 then exit; dec(groups[node].members); EliminateOneCouple(groups[node].father); end; procedure eliminate_couples; var p:pnode; begin startstack:=1; endstack:=1; stack[1]:=1; while startstack<=endstack do begin p:=list[stack[startstack]]; if (p=nil) and (p^.node=2) then EliminateOneCouple(stack[startstack]) else while p<>nil do begin inc(endstack); stack[endstack]:=p^.node; p:=p^.next; end; inc(startstack); end; end; procedure get_handshackes; var p:pnode; current_shackes:longint; begin fillchar(stack,sizeof(stack),0); startstack:=1; endstack:=1; stack[1]:=1; while startstack<=endstack do begin p:=list[stack[startstack]]; current_shackes:=1; if p=nil then current_shackes:=0; while p<>nil do begin inc(endstack); stack[endstack]:=p^.node; current_shackes:=current_shackes*groups[p^.node].members; p:=p^.next; end; inc(startstack); inc(hands,current_shackes); end; end; procedure write_hands; begin writeln(hands); end; begin read_data; eliminate_couples; get_handshackes; write_hands; end. I don't know why ur program got CRASH but it seems u r fooled by problem description ;) The remain datas are not useful at all. If you're solving it that way: There's more groups than what you're allocating for. Specifically, 20000 hobbits in group #1 split into #2 (with 1) and #3 (with 19999). Then #3 splits into #4 (with 1) and #5 (19998) - eventually that hits the limit before the hobbits finish splitting. Find out the correct amount to allocate to avoid the crash. Also, there's solutions that don't need to track groups (especially the big shortcut). Try to reread the task. I've been adding up wrong numbers together because of the misunderstanding. Also, try tests from "If you have WA6" Why i get RuntimeError? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Diagnostics; using System.Globalization; namespace AlgorytmyInternetowe { public class Program { public static void Main(string[] args) { //string[] tmp = Console.In.ReadToEnd().Split(new char[] { ' ' }); string[] tmp = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Console.Write(int.Parse(tmp[0])-int.Parse(tmp[4])+" "+ (int.Parse(tmp[1]) - int.Parse(tmp[3]))); } } } A could do the simple solution there I have 4 points of square and simple calculations of dist to this square. But i found this very easy and i figure out difficult formula for equations of the lines which bounding this square. I calculates dist for this lines and analyzing them. This is initialization of square: /* void init(Point p1, Point p2){ int64_t dx = p1.x-p2.x, dy = p1.y-p2.y; a = dx + dy; // some coof for line-equation b = dx - dy; c1 = dx*(p1.x-p1.y) + dy*(p1.x+p1.y); c2 = dy*(p2.x-p2.y) - dx*(p2.x+p2.y); dc = dx*dx + dy*dy; // delta between c-coof two parallel lines if (dc == 0) { // if square is dot c1 = p1.x; c2 = p1.y; } } */ There i calculate dists to lines /* if (dc == 0){ return std::hypot((p0.x - c1), (p0.y - c2)); } else { double d1 = (b*(p0.y) - a*(p0.x) + c1)/sqrt(2*dc), // signed dist to first line d2 = (b*(p0.x) + a*(p0.y) + c2)/sqrt(2*dc), // signed dist to perpendicular to first line d = sqrt(dc/2.0); // delta between two parallel lines (a side of square) ...... */ "d" always is positive (if square isn't dot), bcs "(d1 - d) < d1" (dist to nearest line less than dist to farthest line) there is some conditionals for calculate the dist to square /* if (d1 < 0) { if (d2 < 0) { // 1 corner x1 = (c1*a - c2*b)/(2.0*dc), y1 = (-c1*b - c2*a)/(2.0*dc) } else if(d2 <= d){ // 1 side return fabs(d1); } else { // 2 corner x2 = (c1*a - c2*b)/(2.0*dc) + (b/2.0), y2 = (-c1*b - c2*a)/(2.0*dc) + (a/2.0) } } else if (d1 <= d) { if (d2 < 0){ // 2 side return fabs(d2); } else if(d2 <= d){ // inside square return 0; } else { // 3 side return fabs(d2 - d); } } else { if (d2 < 0){ // 3 corner x3 = (c1*a - c2*b)/(2.0*dc) - (a/2.0), y3 = (-c1*b - c2*a)/(2.0*dc) + (b/2.0) } else if(d2 <= d){ // 4 side return fabs(d1 - d); } else { // 4 corner x4 = (c1*a-c2*b)/(2.0*dc) + (b-a)/2.0, y4 = (p0.y + (c1*b+c2*a)/(2.0*dc) + (a+b)/2.0 } } */ after this we sorting dist with num, and print the answer tests: 8 0 0 2 2 2 4 0 6 -8 -3 -8 -7 7 11 4 15 5 -5 9 -6 -2 -2 -2 -4 -4 6 -8 10 6 6 10 6 ..... 6 2 : ans is "8 1 2 5 6 4 7 3" 0 0 : ans is "1 6 2 5 7 3 8 4" Edited by author 26.08.2020 02:41 I got acc but for test case "0 0" it's not "1 6 2 5 7 3 8 4" and i got "1 8 6 2 5 7 3 4" |
|