problem
stringlengths
716
12.9k
answer
stringlengths
41
999
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ancient Berland Circus</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different. In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.</div><h2>Input Format</h2><div>The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.</div><h2>Output Format</h2><div>Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.</div><h2>Example</h2><h3>Input</h3><div>0.000000 0.000000 1.000000 1.000000 0.000000 1.000000</div><h3>Output</h3><div>1.00000000</div></body></html>
#include <cstdio> #include <cmath> const double eps=1e-4; const double PI=acos(-1.0); double gcd(double x,double y) { return y>eps? gcd(y,x-floor(x/y)*y):x; } double bcos(double a,double b,double c) { return acos((a*a+b*b-c*c)/(2*a*b)); } int main() { double ax,ay,bx,by,cx,cy; scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&cx,&cy); double a=sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)); double b=sqrt((ax-cx)*(ax-cx)+(ay-cy)*(ay-cy)); double c=sqrt((bx-cx)*(bx-cx)+(by-cy)*(by-cy)); double p=(a+b+c)/2; double s=sqrt(p*(p-a)*(p-b)*(p-c)); double R=(a*b*c)/(4*s); double A=bcos(b,c,a); double B=bcos(a,c,b); double C=bcos(a,b,c); double n=PI/gcd(A,gcd(B,C)); printf("%.11lf\n",R*R*sin(2*PI/n)*n/2); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ancient Berland Circus</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different. In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.</div><h2>Input Format</h2><div>The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.</div><h2>Output Format</h2><div>Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.</div><h2>Example</h2><h3>Input</h3><div>0.000000 0.000000 1.000000 1.000000 0.000000 1.000000</div><h3>Output</h3><div>1.00000000</div></body></html>
#include <stdio.h> #include <math.h> const double eps=1e-4; const double PI=acos(-1.0); double gcd(double x,double y) { return y>eps? gcd(y,x-floor(x/y)*y):x; } double bcos(double a,double b,double c) { return acos((a*a+b*b-c*c)/(2*a*b)); } int main() { double ax,ay,bx,by,cx,cy; scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&cx,&cy); double a=sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)); double b=sqrt((ax-cx)*(ax-cx)+(ay-cy)*(ay-cy)); double c=sqrt((bx-cx)*(bx-cx)+(by-cy)*(by-cy)); double p=(a+b+c)/2; double s=sqrt(p*(p-a)*(p-b)*(p-c)); double R=(a*b*c)/(4*s); double A=bcos(b,c,a); double B=bcos(a,c,b); double C=bcos(a,b,c); double n=PI/gcd(A,gcd(B,C)); printf("%.11lf\n",R*R*sin(2*PI/n)*n/2); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ancient Berland Circus</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different. In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.</div><h2>Input Format</h2><div>The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.</div><h2>Output Format</h2><div>Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.</div><h2>Example</h2><h3>Input</h3><div>0.000000 0.000000 1.000000 1.000000 0.000000 1.000000</div><h3>Output</h3><div>1.00000000</div></body></html>
#include<iostream> #include<cmath> #include<cstdio> using namespace std; double xa,ya,xb,yb,xc,yc; double a,b,c,A,B,C,x,y; double length(double x,double y){return x*x+y*y; } double gcd(double a,double b){ return fabs(b)<1E-4?a:gcd(b,fmod(a,b));} int main() { scanf("%lf%lf%lf%lf%lf%lf",&xa,&ya,&xb,&yb,&xc,&yc); y=length(xc,yc); a=xa-xc,b=ya-yc,c=(length(xa,ya)-y)*0.5; A=xb-xc,B=yb-yc,C=(length(xb,yb)-y)*0.5; y=1/(a*B-A*b); x=(c*B-C*b)*y,y*=a*C-A*c; a=atan2(ya-=y,xa-=x); b=atan2(yb-=y,xb-=x); c=atan2(yc-=y,xc-=x); a=gcd(gcd(fabs(b-a),fabs(c-a)),2*M_PI); printf("%.8lf",length(xa,ya)*sin(a)*M_PI/a); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ancient Berland Circus</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different. In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.</div><h2>Input Format</h2><div>The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.</div><h2>Output Format</h2><div>Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.</div><h2>Example</h2><h3>Input</h3><div>0.000000 0.000000 1.000000 1.000000 0.000000 1.000000</div><h3>Output</h3><div>1.00000000</div></body></html>
#include<cstdio> #include<cmath> #define D double #define S(x) ((x)*(x)) #define G(t) a[t]=x[t]-x[2];b[t]=y[t]-y[2];c[t]=(S(x[t])+S(y[t])-S(x[2])-S(y[2]))/2; #define M(p,q) (p[0]*q[1]-p[1]*q[0]) D g(D a,D b){return fabs(b)<1e-4?a:g(b,fmod(a,b));} D x[3],y[3],a[3],b[2],c[2],A,X,Y; int main() { for(int i=0;i<3;++i)scanf("%lf%lf",x+i,y+i); G(0);G(1); X=M(c,b)/M(a,b); Y=M(a,c)/M(a,b); for(int i=0;i<3;++i)a[i]=atan2(x[i]-=X,y[i]-=Y); A=g(M_PI*2,g(fabs(a[1]-a[0]),fabs(a[2]-a[0]))); printf("%lf\n",(S(x[0])+S(y[0]))*sin(A)*M_PI/A); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ancient Berland Circus</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different. In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.</div><h2>Input Format</h2><div>The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.</div><h2>Output Format</h2><div>Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.</div><h2>Example</h2><h3>Input</h3><div>0.000000 0.000000 1.000000 1.000000 0.000000 1.000000</div><h3>Output</h3><div>1.00000000</div></body></html>
#include<cstdio> #include<cmath> #define S(x) ((x)*(x)) #define G(t) a[t]=x[t]-x[2];b[t]=y[t]-y[2];c[t]=(S(x[t])+S(y[t])-S(x[2])-S(y[2]))/2; #define M(p,q) (p[0]*q[1]-p[1]*q[0]) double g(double a,double b){return fabs(b)<1e-4?a:g(b,fmod(a,b));} double x[3],y[3],a[3],b[2],c[2],A,X,Y; int main(){ for(int i=0;i<3;++i)scanf("%lf%lf",x+i,y+i); G(0);G(1); X=M(c,b)/M(a,b); Y=M(a,c)/M(a,b); for(int i=0;i<3;++i)a[i]=atan2(x[i]-=X,y[i]-=Y); A=g(M_PI*2,g(fabs(a[1]-a[0]),fabs(a[2]-a[0]))); printf("%lf\n",(S(x[0])+S(y[0]))*sin(A)*M_PI/A); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>LCIS</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.</div><h2>Input Format</h2><div>The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.</div><h2>Output Format</h2><div>In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.</div><h2>Example</h2><h3>Input</h3><div>7 2 3 1 6 5 4 6 4 1 3 5 6</div><h3>Output</h3><div>3 3 5 6</div><h2>Example</h2><h3>Input</h3><div>5 1 2 0 2 1 3 1 0 1</div><h3>Output</h3><div>2 0 1</div></body></html>
n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) lenth = [0] * m elem = [[] for i in range(m)] for i in range(n): c = 0 p = -1 for j in range(m): if a[i] == b[j] and c + 1 > lenth[j]: lenth[j] = c + 1 elem[j] = (elem[p] if p != -1 else []) + [a[i]] if a[i] > b[j] and c < lenth[j]: c = lenth[j] p = j max_ = max(lenth) print(max_) if max_ > 0: ar = [] for i in elem: if len(i) > len(ar): ar = i print(*ar)
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>LCIS</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.</div><h2>Input Format</h2><div>The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.</div><h2>Output Format</h2><div>In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.</div><h2>Example</h2><h3>Input</h3><div>7 2 3 1 6 5 4 6 4 1 3 5 6</div><h3>Output</h3><div>3 3 5 6</div><h2>Example</h2><h3>Input</h3><div>5 1 2 0 2 1 3 1 0 1</div><h3>Output</h3><div>2 0 1</div></body></html>
def lcis(a, b): n, m = len(a), len(b) dp = [0]*m best_seq, max_len = [], 0 for i in range(n): current = 0 seq = [] for j in range(m): if a[i] == b[j]: f = seq + [a[i]] if len(f) > max_len: max_len = len(f) best_seq = f dp[j] = max(current+1, dp[j]) if a[i] > b[j]: if dp[j] > current: current = dp[j] seq.append(b[j]) return max_len, best_seq n = int(input()) a = input().split(' ') a = [int(x) for x in a] m = int(input()) b = input().split(' ') b = [int(x) for x in b] x, y = lcis(a, b) print(x) y = [str(x) for x in y] print(' '.join(y))
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>LCIS</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.</div><h2>Input Format</h2><div>The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.</div><h2>Output Format</h2><div>In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.</div><h2>Example</h2><h3>Input</h3><div>7 2 3 1 6 5 4 6 4 1 3 5 6</div><h3>Output</h3><div>3 3 5 6</div><h2>Example</h2><h3>Input</h3><div>5 1 2 0 2 1 3 1 0 1</div><h3>Output</h3><div>2 0 1</div></body></html>
n=int(input()) ar=list(map(int,input().split())) m=int(input()) br=list(map(int,input().split())) t=[0]*m A=[[] for i in range(m)] for i in range(n): c=0 p=-1 for j in range(m): if(ar[i]==br[j] and c+1>t[j]): t[j]=c+1 A[j]=(A[p] if p!=-1 else []) +[ar[i]] if(ar[i]>br[j] and c<t[j]): c=t[j] p=j #print(*A) mx=max(t) print(mx) if(mx>0): #print(*t) ar=[] for el in A: if(len(el)>len(ar)): ar=el print(*ar)
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Binary String Constructing</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given three integers $$$a$$$, $$$b$$$ and $$$x$$$. Your task is to construct a binary string $$$s$$$ of length $$$n = a + b$$$ such that there are exactly $$$a$$$ zeroes, exactly $$$b$$$ ones and exactly $$$x$$$ indices $$$i$$$ (where $$$1 \le i < n$$$) such that $$$s_i \ne s_{i + 1}$$$. It is guaranteed that the answer always exists. For example, for the string "01010" there are four indices $$$i$$$ such that $$$1 \le i < n$$$ and $$$s_i \ne s_{i + 1}$$$ ($$$i = 1, 2, 3, 4$$$). For the string "111001" there are two such indices $$$i$$$ ($$$i = 3, 5$$$). Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.</div><h2>Input Format</h2><div>The first line of the input contains three integers $$$a$$$, $$$b$$$ and $$$x$$$ ($$$1 \le a, b \le 100, 1 \le x < a + b)$$$.</div><h2>Output Format</h2><div>Print only one string $$$s$$$, where $$$s$$$ is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.</div><h2>Example</h2><h3>Input</h3><div>2 2 1</div><h3>Output</h3><div>1100</div><h2>Example</h2><h3>Input</h3><div>3 3 3</div><h3>Output</h3><div>101100</div><h2>Example</h2><h3>Input</h3><div>5 3 6</div><h3>Output</h3><div>01010100</div><h2>Note</h2><div>All possible answers for the first example: - 1100; - 0011. All possible answers for the second example: - 110100; - 101100; - 110010; - 100110; - 011001; - 001101; - 010011; - 001011.</div><h2>Editorial</h2><div>This problem has several general cases: • $$$x$$$ is even and $$$a > b$$$, then the answer is 01 repeated $$$\frac{x}{2}$$$ times, then $$$b - \frac{x}{2}$$$ ones and $$$a - \frac{x}{2}$$$ zeroes; • $$$x$$$ is even and $$$a \le b$$$, then the answer is 10 repeated $$$\frac{x}{2}$$$ times, then $$$a - \frac{x}{2}$$$ zeroes and $$$b - \frac{x}{2}$$$ ones; • $$$x$$$ is odd and $$$a > b$$$, then the answer is 01 repeated $$$\lfloor\frac{x}{2}\rfloor$$$ times, then $$$a - \lfloor\frac{x}{2}\rfloor$$$ zeroes and $$$b - \lfloor\frac{x}{2}\rfloor$$$ ones; • $$$x$$$ is odd and $$$a \le b$$$, then the answer is 10 repeated $$$\lfloor\frac{x}{2}\rfloor$$$ times, then $$$b - \lfloor\frac{x}{2}\rfloor$$$ ones and $$$a - \lfloor\frac{x}{2}\rfloor$$$ zeroes. I am sure that there are other more beautiful solution, but for me the easiest way to solve this problem is to extract general cases and handle it. Overall complexity is $$$O(a + b)$$$.</div></body></html>
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; cin >> a >> b >> c; string ans; if(c == 1) { for(int i=0;i<a;i++) cout<<"0"; for(int i=0;i<b;i++) cout << "1"; } else if(a>b) { for(int i=0;i<a-(c-1)/2-(c-1)%2;i++) cout << "0"; for(int i=0;i<b-(c-1)/2;i++) cout << "1"; int tag = 0; for(int i=0;i<c-1;i++) { cout << tag; tag = (tag + 1)%2; } } else { int temp = a; a = b; b= temp; for(int i=0;i<a-(c-1)/2-(c-1)%2;i++) cout << "1"; for(int i=0;i<b-(c-1)/2;i++) cout << "0"; int tag = 1; for(int i=0;i<c-1;i++) { cout << tag; tag = (tag + 1)%2; } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Binary String Constructing</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given three integers $$$a$$$, $$$b$$$ and $$$x$$$. Your task is to construct a binary string $$$s$$$ of length $$$n = a + b$$$ such that there are exactly $$$a$$$ zeroes, exactly $$$b$$$ ones and exactly $$$x$$$ indices $$$i$$$ (where $$$1 \le i < n$$$) such that $$$s_i \ne s_{i + 1}$$$. It is guaranteed that the answer always exists. For example, for the string "01010" there are four indices $$$i$$$ such that $$$1 \le i < n$$$ and $$$s_i \ne s_{i + 1}$$$ ($$$i = 1, 2, 3, 4$$$). For the string "111001" there are two such indices $$$i$$$ ($$$i = 3, 5$$$). Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.</div><h2>Input Format</h2><div>The first line of the input contains three integers $$$a$$$, $$$b$$$ and $$$x$$$ ($$$1 \le a, b \le 100, 1 \le x < a + b)$$$.</div><h2>Output Format</h2><div>Print only one string $$$s$$$, where $$$s$$$ is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.</div><h2>Example</h2><h3>Input</h3><div>2 2 1</div><h3>Output</h3><div>1100</div><h2>Example</h2><h3>Input</h3><div>3 3 3</div><h3>Output</h3><div>101100</div><h2>Example</h2><h3>Input</h3><div>5 3 6</div><h3>Output</h3><div>01010100</div><h2>Note</h2><div>All possible answers for the first example: - 1100; - 0011. All possible answers for the second example: - 110100; - 101100; - 110010; - 100110; - 011001; - 001101; - 010011; - 001011.</div><h2>Editorial</h2><div>This problem has several general cases: • $$$x$$$ is even and $$$a > b$$$, then the answer is 01 repeated $$$\frac{x}{2}$$$ times, then $$$b - \frac{x}{2}$$$ ones and $$$a - \frac{x}{2}$$$ zeroes; • $$$x$$$ is even and $$$a \le b$$$, then the answer is 10 repeated $$$\frac{x}{2}$$$ times, then $$$a - \frac{x}{2}$$$ zeroes and $$$b - \frac{x}{2}$$$ ones; • $$$x$$$ is odd and $$$a > b$$$, then the answer is 01 repeated $$$\lfloor\frac{x}{2}\rfloor$$$ times, then $$$a - \lfloor\frac{x}{2}\rfloor$$$ zeroes and $$$b - \lfloor\frac{x}{2}\rfloor$$$ ones; • $$$x$$$ is odd and $$$a \le b$$$, then the answer is 10 repeated $$$\lfloor\frac{x}{2}\rfloor$$$ times, then $$$b - \lfloor\frac{x}{2}\rfloor$$$ ones and $$$a - \lfloor\frac{x}{2}\rfloor$$$ zeroes. I am sure that there are other more beautiful solution, but for me the easiest way to solve this problem is to extract general cases and handle it. Overall complexity is $$$O(a + b)$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; #define ll long long #define debug(x) cerr << fixed << #x << " is " << x << endl; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int a, b, x; string s; cin >> a >> b >> x; if(a < b) { for(int i = 1; x != 1; i++, x--) { if(i % 2 == 0) { s += "0"; a--; } else { s += "1"; b--; } } } else { for(int i = 0; x != 1; i++, x--) { if(i % 2 == 0) { s += "0"; a--; } else { s += "1"; b--; } } } if(s.back() == '1') { for(int i = 0; i < a; i++) s += "0"; for(int i = 0; i < b; i++) s += "1"; } else { for(int i = 0; i < b; i++) s += "1"; for(int i = 0; i < a; i++) s += "0"; } cout << s << endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Intense Heat</h1><div>Time limit per test: 4.0 s</div><h2>Description</h2><div>The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are. Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows: Suppose we want to analyze the segment of $$$n$$$ consecutive days. We have measured the temperatures during these $$$n$$$ days; the temperature during $$$i$$$-th day equals $$$a_i$$$. We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $$$x$$$ to day $$$y$$$, we calculate it as $$$\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$$$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. For example, if analyzing the measures $$$[3, 4, 1, 2]$$$ and $$$k = 3$$$, we are interested in segments $$$[3, 4, 1]$$$, $$$[4, 1, 2]$$$ and $$$[3, 4, 1, 2]$$$ (we want to find the maximum value of average temperature over these segments). You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 5000$$$) — the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 5000$$$) — the temperature measures during given $$$n$$$ days.</div><h2>Output Format</h2><div>Print one real number — the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. Your answer will be considered correct if the following condition holds: $$$|res - res_0| < 10^{-6}$$$, where $$$res$$$ is your answer, and $$$res_0$$$ is the answer given by the jury's solution.</div><h2>Example</h2><h3>Input</h3><div>4 3 3 4 1 2</div><h3>Output</h3><div>2.666666666666667</div><h2>Editorial</h2><div>This task is very straight-forward implementation problem. So we can iterate over all segments of the given array, calculate their sum, and if the length of the current segment is not less than $$$k$$$, try to update the answer with the mean of this segment. Overall complexity is $$$O(n^2)$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,n) for(int i=a;i<n;i++) #define sf(n) scanf("%d",&n) #define pii pair<int,int> #define p2ii pair<pii,int> #define ff first #define ss second int main() { int n,k;sf(n);sf(k);int a[n]; rep(i,0,n)sf(a[i]);int sum[n]; sum[0]=a[0];double avg=0; rep(i,1,n)sum[i]=sum[i-1]+a[i]; rep(range_element,k,n+1){ int mx=sum[range_element-1]; rep(j,range_element,n) { mx=max(mx,sum[j]-sum[j-range_element]); } avg=max(avg,(mx*1.0)/range_element); } cout<<fixed<<setprecision(9)<<avg<<"\n"; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Intense Heat</h1><div>Time limit per test: 4.0 s</div><h2>Description</h2><div>The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are. Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows: Suppose we want to analyze the segment of $$$n$$$ consecutive days. We have measured the temperatures during these $$$n$$$ days; the temperature during $$$i$$$-th day equals $$$a_i$$$. We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $$$x$$$ to day $$$y$$$, we calculate it as $$$\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$$$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. For example, if analyzing the measures $$$[3, 4, 1, 2]$$$ and $$$k = 3$$$, we are interested in segments $$$[3, 4, 1]$$$, $$$[4, 1, 2]$$$ and $$$[3, 4, 1, 2]$$$ (we want to find the maximum value of average temperature over these segments). You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 5000$$$) — the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 5000$$$) — the temperature measures during given $$$n$$$ days.</div><h2>Output Format</h2><div>Print one real number — the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. Your answer will be considered correct if the following condition holds: $$$|res - res_0| < 10^{-6}$$$, where $$$res$$$ is your answer, and $$$res_0$$$ is the answer given by the jury's solution.</div><h2>Example</h2><h3>Input</h3><div>4 3 3 4 1 2</div><h3>Output</h3><div>2.666666666666667</div><h2>Editorial</h2><div>This task is very straight-forward implementation problem. So we can iterate over all segments of the given array, calculate their sum, and if the length of the current segment is not less than $$$k$$$, try to update the answer with the mean of this segment. Overall complexity is $$$O(n^2)$$$.</div></body></html>
#include <iostream> #include <vector> #include <algorithm> #include <iomanip> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> temper(n); for (auto& i : temper) cin >> i; double avg = 0; for (int dist = k; dist <= n; dist++) { int sum = 0; for (int i = 0; i < dist; i++) sum += temper[i]; int max_sum = sum; for (int i = dist; i < n; i++) { sum += temper[i] - temper[i-dist]; max_sum = max(sum, max_sum); } avg = max(avg, (double)max_sum/dist); } cout << setprecision(15) << avg << endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Intense Heat</h1><div>Time limit per test: 4.0 s</div><h2>Description</h2><div>The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are. Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows: Suppose we want to analyze the segment of $$$n$$$ consecutive days. We have measured the temperatures during these $$$n$$$ days; the temperature during $$$i$$$-th day equals $$$a_i$$$. We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $$$x$$$ to day $$$y$$$, we calculate it as $$$\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$$$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. For example, if analyzing the measures $$$[3, 4, 1, 2]$$$ and $$$k = 3$$$, we are interested in segments $$$[3, 4, 1]$$$, $$$[4, 1, 2]$$$ and $$$[3, 4, 1, 2]$$$ (we want to find the maximum value of average temperature over these segments). You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 5000$$$) — the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 5000$$$) — the temperature measures during given $$$n$$$ days.</div><h2>Output Format</h2><div>Print one real number — the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. Your answer will be considered correct if the following condition holds: $$$|res - res_0| < 10^{-6}$$$, where $$$res$$$ is your answer, and $$$res_0$$$ is the answer given by the jury's solution.</div><h2>Example</h2><h3>Input</h3><div>4 3 3 4 1 2</div><h3>Output</h3><div>2.666666666666667</div><h2>Editorial</h2><div>This task is very straight-forward implementation problem. So we can iterate over all segments of the given array, calculate their sum, and if the length of the current segment is not less than $$$k$$$, try to update the answer with the mean of this segment. Overall complexity is $$$O(n^2)$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; double sr,srm; int n,k,a[5009],i,j; int s,smax; int main() { cin>>n>>k; for(i=1;i<=n;i++) cin>>a[i]; for(i=k;i<=n;i++) { smax=0,s=0; for(j=1;j<=i;j++) s+=a[j]; smax=s; for(j=i+1;j<=n;j++) { s-=a[j-i]; s+=a[j]; if(s>smax) smax=s; } sr=double(smax)/double(i); srm=max(srm,sr); } cout<<fixed<<setprecision(10)<<srm; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Tree Constructing</h1><div>Time limit per test: 4.0 s</div><h2>Description</h2><div>You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$. Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible. An undirected tree is a connected undirected graph with $$$n - 1$$$ edges. Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree. Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).</div><h2>Input Format</h2><div>The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).</div><h2>Output Format</h2><div>If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1</div><h2>Example</h2><h3>Input</h3><div>6 3 3</div><h3>Output</h3><div>YES 3 1 4 1 1 2 5 2 2 6</div><h2>Example</h2><h3>Input</h3><div>6 2 3</div><h3>Output</h3><div>NO</div><h2>Example</h2><h3>Input</h3><div>10 4 3</div><h3>Output</h3><div>YES 2 9 2 10 10 3 3 1 6 10 8 2 4 3 5 6 6 7</div><h2>Example</h2><h3>Input</h3><div>8 5 3</div><h3>Output</h3><div>YES 2 5 7 2 3 7 3 1 1 6 8 7 4 3</div><h2>Editorial</h2><div>Let's construct a tree by the following algorithm: if $$$d \ge n$$$, let's print "NO" and terminate the program. Otherwise let's keep the array $$$deg$$$ of the length $$$n$$$ which will represent degrees of vertices. The first step is to construct the diameter of the tree. Let first $$$d + 1$$$ vertices form it. Let's add $$$d$$$ edges to the answer, increase degrees of vertices corresponding to this edges, and if some vertex has degree greater than $$$k$$$, print "NO" and terminate the program. The second (and the last) step is to attach the remaining $$$n - d - 1$$$ vertices to the tree. Let's call the vertex free if its degree is less than $$$k$$$. Also let's keep all free vertices forming the diameter in some data structure which allows us to take the vertex with the minimum maximal distance to any other vertex and remove such vertices. It can be done by, for example, set of pairs ($$$dist_v, v$$$), where $$$dist_v$$$ is a maximum distance from the vertex $$$v$$$ to any other vertex. Now let's add all vertices from starting from the vertex $$$d + 1$$$ (0-indexed) to the vertex $$$n - 1$$$, let the current vertex be $$$u$$$. We get the vertex with the minimum maximal distance to any other vertex, let it be $$$v$$$. Now we increase the degree of vertices $$$u$$$ and $$$v$$$, add the edge between they, and if $$$v$$$ still be free, return it to the data structure, otherwise remove it. The same with the vertex $$$u$$$ (it is obvious that its maximal distance to any other vertex will be equals $$$dist_v + 1$$$). If at any step our data structure will be empty or the minimum maximal distance will be equals $$$d$$$, the answer is "NO". Otherwise we can print the answer. See my solution to better understanding. Overall complexity: $$$O(n \log n)$$$ or $$$O(n)$$$ (depends on implementation).</div></body></html>
#include <bits/stdc++.h> #define MP make_pair #define PB push_back using namespace std; typedef long long ll; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } const int N=4e5+10; int n,d,k,i,j,in[N]; vector<int>G[N]; void dfs(int dis,int pre){ if (j>n||dis==0) return; while (j<=n&&in[pre]<k){ G[j].push_back(pre); in[j]++,in[pre]++; j++; dfs(dis-1,j-1); } } int main(){ read(n),read(d),read(k); if (d+1>n) return puts("NO"),0; for (i=1,j=d+2;i<=d;i++){ G[i].push_back(i+1); in[i]++,in[i+1]++; if (i==1||j>n) continue; if (in[i]>k) return puts("NO"),0; int dis=min(abs(i-1),abs(d+1-i)); int tmp=k-in[i]; dfs(dis,i); } for (i=1;i<=n;i++){ if (in[i]>k||in[i]==0) return puts("NO"),0; } puts("YES"); for (i=1;i<=n;i++){ for (j=0;j<(int)G[i].size();j++){ printf("%d %d\n",i,G[i][j]); } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Tree Constructing</h1><div>Time limit per test: 4.0 s</div><h2>Description</h2><div>You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$. Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible. An undirected tree is a connected undirected graph with $$$n - 1$$$ edges. Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree. Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).</div><h2>Input Format</h2><div>The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).</div><h2>Output Format</h2><div>If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1</div><h2>Example</h2><h3>Input</h3><div>6 3 3</div><h3>Output</h3><div>YES 3 1 4 1 1 2 5 2 2 6</div><h2>Example</h2><h3>Input</h3><div>6 2 3</div><h3>Output</h3><div>NO</div><h2>Example</h2><h3>Input</h3><div>10 4 3</div><h3>Output</h3><div>YES 2 9 2 10 10 3 3 1 6 10 8 2 4 3 5 6 6 7</div><h2>Example</h2><h3>Input</h3><div>8 5 3</div><h3>Output</h3><div>YES 2 5 7 2 3 7 3 1 1 6 8 7 4 3</div><h2>Editorial</h2><div>Let's construct a tree by the following algorithm: if $$$d \ge n$$$, let's print "NO" and terminate the program. Otherwise let's keep the array $$$deg$$$ of the length $$$n$$$ which will represent degrees of vertices. The first step is to construct the diameter of the tree. Let first $$$d + 1$$$ vertices form it. Let's add $$$d$$$ edges to the answer, increase degrees of vertices corresponding to this edges, and if some vertex has degree greater than $$$k$$$, print "NO" and terminate the program. The second (and the last) step is to attach the remaining $$$n - d - 1$$$ vertices to the tree. Let's call the vertex free if its degree is less than $$$k$$$. Also let's keep all free vertices forming the diameter in some data structure which allows us to take the vertex with the minimum maximal distance to any other vertex and remove such vertices. It can be done by, for example, set of pairs ($$$dist_v, v$$$), where $$$dist_v$$$ is a maximum distance from the vertex $$$v$$$ to any other vertex. Now let's add all vertices from starting from the vertex $$$d + 1$$$ (0-indexed) to the vertex $$$n - 1$$$, let the current vertex be $$$u$$$. We get the vertex with the minimum maximal distance to any other vertex, let it be $$$v$$$. Now we increase the degree of vertices $$$u$$$ and $$$v$$$, add the edge between they, and if $$$v$$$ still be free, return it to the data structure, otherwise remove it. The same with the vertex $$$u$$$ (it is obvious that its maximal distance to any other vertex will be equals $$$dist_v + 1$$$). If at any step our data structure will be empty or the minimum maximal distance will be equals $$$d$$$, the answer is "NO". Otherwise we can print the answer. See my solution to better understanding. Overall complexity: $$$O(n \log n)$$$ or $$$O(n)$$$ (depends on implementation).</div></body></html>
#include <bits/stdc++.h> #define left lef using namespace std; const int N = 400001; int n,d,k,cnt,left; vector <int> g[N]; vector <pair<int,int> > ans; void go(int node,int levels){ while (g[node].size() < k && left > 0 && levels > 0){ g[node].push_back(cnt); g[cnt].push_back(node); ans.push_back(make_pair(node,cnt)); left--; cnt++; go(cnt-1,levels-1); } } int main() { scanf("%d%d%d",&n,&d,&k); if (d > n-1){ puts("NO"); return 0; } for (int i = 1;i<=d;i++){ g[i].push_back(i+1); g[i+1].push_back(i); ans.push_back(make_pair(i,i+1)); } left = n - d - 1,cnt = d+2; for (int i = 2;i<=d;i++){ int lev = min(i - 1,d-i+1); go(i,lev); } if (left > 0){ puts("NO"); return 0; } int mx = 0; for (int i = 1;i<=n;i++){ mx = max(mx,(int)g[i].size()); } if (mx > k){ puts("NO"); return 0; } puts("YES"); for (int i = 0;i<ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Sonya and Exhibition</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.</div><h2>Output Format</h2><div>Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>5 3 1 3 2 4 2 5</div><h3>Output</h3><div>01100</div><h2>Example</h2><h3>Input</h3><div>6 3 5 6 1 4 4 6</div><h3>Output</h3><div>110010</div><h2>Note</h2><div>In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; - in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; - in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; - in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; - in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.</div><h2>Editorial</h2><div>Note, that it is always optimal to use roses in even positions and lilies in odd positions. That is, the string $$$01010101010\ldots$$$ is always optimal.</div></body></html>
#include"bits/stdc++.h" #define endl '\n' #define int long long int #define maxi 100000+5 #define pii pair<int,int> #define stat first #define num second #define mod 1e9+7 using namespace std; signed main(){ int n,k; cin >> n >> k; while(k--){ int a; cin >> a >> a; } int a = 1; while(n--){ cout <<a; a = 1-a; } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Sonya and Exhibition</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.</div><h2>Output Format</h2><div>Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>5 3 1 3 2 4 2 5</div><h3>Output</h3><div>01100</div><h2>Example</h2><h3>Input</h3><div>6 3 5 6 1 4 4 6</div><h3>Output</h3><div>110010</div><h2>Note</h2><div>In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; - in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; - in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; - in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; - in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.</div><h2>Editorial</h2><div>Note, that it is always optimal to use roses in even positions and lilies in odd positions. That is, the string $$$01010101010\ldots$$$ is always optimal.</div></body></html>
#include <algorithm> #include <cmath> #include <cstdio> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <sstream> #include <string> #include <set> #include <vector> #include <unordered_set> #include <unordered_map> #include <utility> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); #ifdef INPUT_TXT std::ifstream in(INPUT_TXT); std::cin.rdbuf(in.rdbuf()); //std::ofstream out(OUTPUT_TXT); //std::cout.rdbuf(out.rdbuf()); #endif int n; int m; std::cin >> n >> m; for (int i = 0; i < m; ++i) { int l; int r; std::cin >> l >> r; } std::string ans(n, '0'); for (int i = 0; i < n; i += 2) ans[i] = '1'; std::cout << ans; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Sonya and Exhibition</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.</div><h2>Output Format</h2><div>Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>5 3 1 3 2 4 2 5</div><h3>Output</h3><div>01100</div><h2>Example</h2><h3>Input</h3><div>6 3 5 6 1 4 4 6</div><h3>Output</h3><div>110010</div><h2>Note</h2><div>In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; - in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; - in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; - in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; - in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.</div><h2>Editorial</h2><div>Note, that it is always optimal to use roses in even positions and lilies in odd positions. That is, the string $$$01010101010\ldots$$$ is always optimal.</div></body></html>
#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); int main() { fast; int n,m; cin>>n>>m; int i; for(i=0;i<m;i++) { int l,r; cin>>l>>r; } int p=0; for(int j=0;j<n;j++) { if(p%2==0) cout<<1; else cout<<0; p++; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Sonya and Exhibition</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.</div><h2>Output Format</h2><div>Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>5 3 1 3 2 4 2 5</div><h3>Output</h3><div>01100</div><h2>Example</h2><h3>Input</h3><div>6 3 5 6 1 4 4 6</div><h3>Output</h3><div>110010</div><h2>Note</h2><div>In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; - in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; - in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; - in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; - in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.</div><h2>Editorial</h2><div>Note, that it is always optimal to use roses in even positions and lilies in odd positions. That is, the string $$$01010101010\ldots$$$ is always optimal.</div></body></html>
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ll long long int int main() { ll n,m,i,x,y; cin>>n>>m; for(i=1;i<=m;i++) cin>>x>>y; for(i=1;i<=n;i++) { if(i%2==0) cout<<"1"; else cout<<"0"; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Sonya and Exhibition</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.</div><h2>Output Format</h2><div>Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>5 3 1 3 2 4 2 5</div><h3>Output</h3><div>01100</div><h2>Example</h2><h3>Input</h3><div>6 3 5 6 1 4 4 6</div><h3>Output</h3><div>110010</div><h2>Note</h2><div>In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; - in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; - in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; - in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; - in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.</div><h2>Editorial</h2><div>Note, that it is always optimal to use roses in even positions and lilies in odd positions. That is, the string $$$01010101010\ldots$$$ is always optimal.</div></body></html>
#include <bits/stdc++.h> #define FF first #define SS second #define PB push_back #define sz(c) int((c).size()) #define all(c) (c).begin(), (c).end() #define endl '\n' using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; const ll MOD = 1000000007; const int N = 1 * 1000000; int main() { int n, m; cin >> n >> m; vector<ii> v; for (int i = 0; i < m; ++i) { int t1, t2; cin >> t1 >> t2; v.PB({t1, t2}); } string ans = ""; for (int i = 0; i < n / 2; ++i) ans += "01"; if (n & 1) ans += "0"; cout << ans << endl; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Polycarp's Practice</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Polycarp is practicing his problem solving skill. He has a list of $$$n$$$ problems with difficulties $$$a_1, a_2, \dots, a_n$$$, respectively. His plan is to practice for exactly $$$k$$$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all $$$n$$$ problems in exactly $$$k$$$ days. Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in $$$k$$$ days he will solve all the $$$n$$$ problems. The profit of the $$$j$$$-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the $$$j$$$-th day (i.e. if he solves problems with indices from $$$l$$$ to $$$r$$$ during a day, then the profit of the day is $$$\max\limits_{l \le i \le r}a_i$$$). The total profit of his practice is the sum of the profits over all $$$k$$$ days of his practice. You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all $$$n$$$ problems between $$$k$$$ days satisfying the conditions above in such a way, that the total profit is maximum. For example, if $$$n = 8, k = 3$$$ and $$$a = [5, 4, 2, 6, 5, 1, 9, 2]$$$, one of the possible distributions with maximum total profit is: $$$[5, 4, 2], [6, 5], [1, 9, 2]$$$. Here the total profit equals $$$5 + 6 + 9 = 20$$$.</div><h2>Input Format</h2><div>The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2000$$$) — the number of problems and the number of days, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2000$$$) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).</div><h2>Output Format</h2><div>In the first line of the output print the maximum possible total profit. In the second line print exactly $$$k$$$ positive integers $$$t_1, t_2, \dots, t_k$$$ ($$$t_1 + t_2 + \dots + t_k$$$ must equal $$$n$$$), where $$$t_j$$$ means the number of problems Polycarp will solve during the $$$j$$$-th day in order to achieve the maximum possible total profit of his practice. If there are many possible answers, you may print any of them.</div><h2>Example</h2><h3>Input</h3><div>8 3 5 4 2 6 5 1 9 2</div><h3>Output</h3><div>20 3 2 3</div><h2>Example</h2><h3>Input</h3><div>5 1 1 1 1 1 1</div><h3>Output</h3><div>1 5</div><h2>Example</h2><h3>Input</h3><div>4 2 1 2000 2000 2</div><h3>Output</h3><div>4000 2 2</div><h2>Note</h2><div>The first example is described in the problem statement. In the second example there is only one possible distribution. In the third example the best answer is to distribute problems in the following way: $$$[1, 2000], [2000, 2]$$$. The total profit of this distribution is $$$2000 + 2000 = 4000$$$.</div><h2>Editorial</h2><div>The maximum possible total profit you can obtain is the sum of the $$$k$$$ largest values of the given array. This is obvious because we can always separate these $$$k$$$ maximums and then extend the segments corresponding to them to the left or to the right and cover the entire array. I suggest the following: extract $$$k$$$ largest values of the given array and place a separator right after each of them (except the rightmost one). Overall complexity is $$$O(n \log n)$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; int main() { int n,k; cin>>n>>k; vector<int> v; vector<int> temp; for(int i=0; i<n; i++) { int x; cin>>x; v.push_back(x); } temp = v; sort(temp.begin(),temp.end(),greater<int>()); int sum=0; vector<int> maxv; for(int i=0; i<k; i++) { sum+=temp[i]; maxv.push_back(temp[i]); } temp.clear(); int i; int prev = -1; for(i=0; i<n; i++) { if(maxv.empty()) { break; } for(int j=0; j<maxv.size(); j++) { if(v[i]==maxv[j]) { temp.push_back(i-prev); prev = i; maxv.erase(maxv.begin()+j); break; } } } temp[temp.size()-1]+=n-i; cout<<sum<<endl; for(int i=0; i<temp.size(); i++) { cout<<temp[i]<<" "; } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Polycarp's Practice</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Polycarp is practicing his problem solving skill. He has a list of $$$n$$$ problems with difficulties $$$a_1, a_2, \dots, a_n$$$, respectively. His plan is to practice for exactly $$$k$$$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all $$$n$$$ problems in exactly $$$k$$$ days. Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in $$$k$$$ days he will solve all the $$$n$$$ problems. The profit of the $$$j$$$-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the $$$j$$$-th day (i.e. if he solves problems with indices from $$$l$$$ to $$$r$$$ during a day, then the profit of the day is $$$\max\limits_{l \le i \le r}a_i$$$). The total profit of his practice is the sum of the profits over all $$$k$$$ days of his practice. You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all $$$n$$$ problems between $$$k$$$ days satisfying the conditions above in such a way, that the total profit is maximum. For example, if $$$n = 8, k = 3$$$ and $$$a = [5, 4, 2, 6, 5, 1, 9, 2]$$$, one of the possible distributions with maximum total profit is: $$$[5, 4, 2], [6, 5], [1, 9, 2]$$$. Here the total profit equals $$$5 + 6 + 9 = 20$$$.</div><h2>Input Format</h2><div>The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2000$$$) — the number of problems and the number of days, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2000$$$) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).</div><h2>Output Format</h2><div>In the first line of the output print the maximum possible total profit. In the second line print exactly $$$k$$$ positive integers $$$t_1, t_2, \dots, t_k$$$ ($$$t_1 + t_2 + \dots + t_k$$$ must equal $$$n$$$), where $$$t_j$$$ means the number of problems Polycarp will solve during the $$$j$$$-th day in order to achieve the maximum possible total profit of his practice. If there are many possible answers, you may print any of them.</div><h2>Example</h2><h3>Input</h3><div>8 3 5 4 2 6 5 1 9 2</div><h3>Output</h3><div>20 3 2 3</div><h2>Example</h2><h3>Input</h3><div>5 1 1 1 1 1 1</div><h3>Output</h3><div>1 5</div><h2>Example</h2><h3>Input</h3><div>4 2 1 2000 2000 2</div><h3>Output</h3><div>4000 2 2</div><h2>Note</h2><div>The first example is described in the problem statement. In the second example there is only one possible distribution. In the third example the best answer is to distribute problems in the following way: $$$[1, 2000], [2000, 2]$$$. The total profit of this distribution is $$$2000 + 2000 = 4000$$$.</div><h2>Editorial</h2><div>The maximum possible total profit you can obtain is the sum of the $$$k$$$ largest values of the given array. This is obvious because we can always separate these $$$k$$$ maximums and then extend the segments corresponding to them to the left or to the right and cover the entire array. I suggest the following: extract $$$k$$$ largest values of the given array and place a separator right after each of them (except the rightmost one). Overall complexity is $$$O(n \log n)$$$.</div></body></html>
#include<bits/stdc++.h> using namespace std; typedef pair<int, int> ii; bool f(ii a, ii b){ if(a.first==b.first){ return a.second>b.second; } return a.first<b.first; } int main(){ int n, k; cin>>n>>k; pair<int, int> a[n]; for(int i=0;i<n;i++){ cin>>a[i].first; a[i].second = i+1; } sort(a, a+n, f); int ans=0; vector<int> ind; for(int j=0;j<k;j++){ ans+=a[n-j-1].first; ind.push_back(a[n-j-1].second); } sort(ind.begin(), ind.end()); cout<<ans<<endl; if(k>1){ cout<<ind[0]<<" "; for(int i=1;i<k-1;i++){ cout<<ind[i] - ind[i-1]<<" "; } cout<<n-ind[k-2]<<endl; } else cout<<n<<endl; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Annoying Present</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice. Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 \le i \le n$$$) and for every $$$j \in [1, n]$$$ adds $$$x + d \cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = |i - j|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$). For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 \cdot 2,~1 - 1 + 2 \cdot 1,~2 - 1 + 2 \cdot 0,~2 - 1 + 2 \cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$). Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric. What is the maximum arithmetic mean value Bob can achieve?</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 \le x_i, d_i \le 10^3$$$) — the parameters for the $$$i$$$-th change.</div><h2>Output Format</h2><div>Print the maximal average arithmetic mean of the elements Bob can achieve. Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>2 3 -1 3 0 0 -1 -4</div><h3>Output</h3><div>-2.500000000000000</div><h2>Example</h2><h3>Input</h3><div>3 2 0 2 5 0</div><h3>Output</h3><div>7.000000000000000</div><h2>Editorial</h2><div>Judging by constraints, you can guess that the greedy approach is the right one. Firstly, let's transition from maximizing the arithmetic mean to the sum, it's the same thing generally. Secondly, notice that each $$$x$$$ is being added to each element regardless of the chosen position. Finally, take a look at a function $$$f(d, i)$$$ — total sum obtained by applying change with $$$d$$$ to position $$$i$$$ and notice that it is non-strictly convex. Its maximum or minimum values can always be found in one of these positions: $$$\frac{n}{2}$$$ (method of rounding doesn't matter), $$$1$$$ and $$$n$$$. Thus, the solution will look like this: for positive $$$d$$$ you apply the change to position $$$1$$$ and for non-positive $$$d$$$ — to position $$$\lfloor \frac{n}{2} \rfloor$$$. The impact of the change can be calculated with the formula of the sum of arithmetic progression. Also, you should either do all of your calculations in long double (10-byte type) or maintain sum in long long (you can estimate it with $$$m \cdot n^2 \cdot MAXN \le 10^{18}$$$, so it fits) and divide it by $$$n$$$ in the end (then double will work). Overall complexity: $$$O(m)$$$.</div></body></html>
#include<bits/stdc++.h> #define LL long long using namespace std; LL n,m,a,b,c,x,d; long double sum,ans; inline LL read() { char ch=getchar(); LL f=1,x=0; while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return f*x; } int main() { n=read(); m=read(); sum=0; for (int i=1;i<=m;i++) { x=read(); d=read(); if (d>=0) sum+=x*n+((d*(n-1)*n)/2); else { sum+=x*n; LL mid; mid=n/2+1; //for (int j=1;j<=n;j++) sum+=d*abs(mid-j); LL front=mid-1,last=n-mid; sum+=d*((((1+front)*front)/2)+(((1+last)*last)/2)); } } ans=sum/(long double) n; //printf("%.10llf",ans); cout<<fixed<<setprecision(15)<<ans; //system("pause"); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Annoying Present</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice. Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 \le i \le n$$$) and for every $$$j \in [1, n]$$$ adds $$$x + d \cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = |i - j|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$). For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 \cdot 2,~1 - 1 + 2 \cdot 1,~2 - 1 + 2 \cdot 0,~2 - 1 + 2 \cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$). Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric. What is the maximum arithmetic mean value Bob can achieve?</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 \le x_i, d_i \le 10^3$$$) — the parameters for the $$$i$$$-th change.</div><h2>Output Format</h2><div>Print the maximal average arithmetic mean of the elements Bob can achieve. Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>2 3 -1 3 0 0 -1 -4</div><h3>Output</h3><div>-2.500000000000000</div><h2>Example</h2><h3>Input</h3><div>3 2 0 2 5 0</div><h3>Output</h3><div>7.000000000000000</div><h2>Editorial</h2><div>Judging by constraints, you can guess that the greedy approach is the right one. Firstly, let's transition from maximizing the arithmetic mean to the sum, it's the same thing generally. Secondly, notice that each $$$x$$$ is being added to each element regardless of the chosen position. Finally, take a look at a function $$$f(d, i)$$$ — total sum obtained by applying change with $$$d$$$ to position $$$i$$$ and notice that it is non-strictly convex. Its maximum or minimum values can always be found in one of these positions: $$$\frac{n}{2}$$$ (method of rounding doesn't matter), $$$1$$$ and $$$n$$$. Thus, the solution will look like this: for positive $$$d$$$ you apply the change to position $$$1$$$ and for non-positive $$$d$$$ — to position $$$\lfloor \frac{n}{2} \rfloor$$$. The impact of the change can be calculated with the formula of the sum of arithmetic progression. Also, you should either do all of your calculations in long double (10-byte type) or maintain sum in long long (you can estimate it with $$$m \cdot n^2 \cdot MAXN \le 10^{18}$$$, so it fits) and divide it by $$$n$$$ in the end (then double will work). Overall complexity: $$$O(m)$$$.</div></body></html>
#include<bits/stdc++.h> using namespace std; int main() { long long n,m; cin >> n >> m; long long sum = 0; for(int i = 1;i <= m;i++) { int d,x; scanf("%d%d",&x,&d); sum += x*n; // cout << sum << endl; if(d >= 0) { sum += (n-1)*n/2*d; } if(d < 0) { if(n % 2 == 1) sum+=d*(n/2)*(n/2+1); else sum+=d*(n/2)*(n/2+1)/2+(n/2)*d*(n/2-1)/2; } // cout << sum <<" "<< (n/2)*(n/2+1)/2+(n/2)*(n/2-1)/2<<endl; } printf("%.6lf",sum/1.0/n); return 0; } //x+d?*(i,j)
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Annoying Present</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice. Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 \le i \le n$$$) and for every $$$j \in [1, n]$$$ adds $$$x + d \cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = |i - j|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$). For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 \cdot 2,~1 - 1 + 2 \cdot 1,~2 - 1 + 2 \cdot 0,~2 - 1 + 2 \cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$). Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric. What is the maximum arithmetic mean value Bob can achieve?</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 \le x_i, d_i \le 10^3$$$) — the parameters for the $$$i$$$-th change.</div><h2>Output Format</h2><div>Print the maximal average arithmetic mean of the elements Bob can achieve. Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>2 3 -1 3 0 0 -1 -4</div><h3>Output</h3><div>-2.500000000000000</div><h2>Example</h2><h3>Input</h3><div>3 2 0 2 5 0</div><h3>Output</h3><div>7.000000000000000</div><h2>Editorial</h2><div>Judging by constraints, you can guess that the greedy approach is the right one. Firstly, let's transition from maximizing the arithmetic mean to the sum, it's the same thing generally. Secondly, notice that each $$$x$$$ is being added to each element regardless of the chosen position. Finally, take a look at a function $$$f(d, i)$$$ — total sum obtained by applying change with $$$d$$$ to position $$$i$$$ and notice that it is non-strictly convex. Its maximum or minimum values can always be found in one of these positions: $$$\frac{n}{2}$$$ (method of rounding doesn't matter), $$$1$$$ and $$$n$$$. Thus, the solution will look like this: for positive $$$d$$$ you apply the change to position $$$1$$$ and for non-positive $$$d$$$ — to position $$$\lfloor \frac{n}{2} \rfloor$$$. The impact of the change can be calculated with the formula of the sum of arithmetic progression. Also, you should either do all of your calculations in long double (10-byte type) or maintain sum in long long (you can estimate it with $$$m \cdot n^2 \cdot MAXN \le 10^{18}$$$, so it fits) and divide it by $$$n$$$ in the end (then double will work). Overall complexity: $$$O(m)$$$.</div></body></html>
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; int read(){ int x=0,w=1; char ch=0; while (ch<'0' || ch>'9'){ if (ch=='-') w=-1; ch=getchar(); } while (ch<='9' && ch>='0'){ x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); } return x*w; } int n,m; ll ans,t1,t2; ll Abs(ll x){ return max(x,-x); } int main(){ n=read();m=read(); for (int i=1;i<=n;++i){ t1+=Abs(i-1); t2+=Abs(i-(n+1)/2); } while (m--){ int x=read(),d=read(); ans+=x*n; if (d<0) ans+=d*t2; else ans+=d*t1; } printf("%.8lf\n",(double)ans*1.0/n); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Relatively Prime Graph</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Let's call an undirected graph $$$G = (V, E)$$$ relatively prime if and only if for each edge $$$(v, u) \in E$$$  $$$GCD(v, u) = 1$$$ (the greatest common divisor of $$$v$$$ and $$$u$$$ is $$$1$$$). If there is no edge between some pair of vertices $$$v$$$ and $$$u$$$ then the value of $$$GCD(v, u)$$$ doesn't matter. The vertices are numbered from $$$1$$$ to $$$|V|$$$. Construct a relatively prime graph with $$$n$$$ vertices and $$$m$$$ edges such that it is connected and it contains neither self-loops nor multiple edges. If there exists no valid graph with the given number of vertices and edges then output "Impossible". If there are multiple answers then print any of them.</div><h2>Input Format</h2><div>The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of vertices and the number of edges.</div><h2>Output Format</h2><div>If there exists no valid graph with the given number of vertices and edges then output "Impossible". Otherwise print the answer in the following format: The first line should contain the word "Possible". The $$$i$$$-th of the next $$$m$$$ lines should contain the $$$i$$$-th edge $$$(v_i, u_i)$$$ of the resulting graph ($$$1 \le v_i, u_i \le n, v_i \neq u_i$$$). For each pair $$$(v, u)$$$ there can be no more pairs $$$(v, u)$$$ or $$$(u, v)$$$. The vertices are numbered from $$$1$$$ to $$$n$$$. If there are multiple answers then print any of them.</div><h2>Example</h2><h3>Input</h3><div>5 6</div><h3>Output</h3><div>Possible 2 5 3 2 5 1 3 4 4 1 5 4</div><h2>Example</h2><h3>Input</h3><div>6 12</div><h3>Output</h3><div>Impossible</div><h2>Note</h2><div>Here is the representation of the graph from the first example:</div><h2>Editorial</h2><div>Even though $$$n$$$ is up to $$$10^5$$$, straightforward $$$O(n^2 \log n)$$$ solution will work. You iterate for $$$i$$$ from $$$1$$$ to $$$n$$$ in the outer loop, from $$$i + 1$$$ to $$$n$$$ in the inner loop and check $$$GCD$$$ each time. When $$$m$$$ edges are found, you break from both loops. Here is why this work fast enough. The total number of pairs $$$(x, y)$$$ with $$$1 \le x, y \le n, gcd(x, y) = 1$$$ is $$$\varphi(1) + \varphi(2) + \dots + \varphi(n)$$$, where $$$\varphi$$$ is Euler's totient function. We also want to substract a single pair $$$(1, 1)$$$. And this sum grows so fast that after about $$$600$$$ iteratons $$$\varphi(1) + \varphi(2) + \dots + \varphi(600)$$$ will be greater than $$$100000$$$ for any $$$n$$$. The only thing left is to check that $$$m$$$ is big enough to build a connected graph ($$$m \ge n - 1$$$) and small enough to fit all possible edges for given $$$n$$$ (the formula above). Overall complexity: $$$O(n^2 \log n)$$$.</div></body></html>
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<iostream> using namespace std; int n,m,p,t; int gcd(int a,int b){return b?gcd(b,a%b):a;} void imp(){ printf("Impossible"); } int main() { t=0; scanf("%d%d",&n,&m); if(m<(n-1)) { imp(); return 0; } if(n<1000) { for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { p=gcd(i,j); if (p==1) t++; } if(t<m) { imp(); return 0; } } printf("Possible\n"); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { p=gcd(i,j); if(p==1) { printf("%d %d\n",i,j); m--; if(!m) return 0; } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Relatively Prime Graph</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Let's call an undirected graph $$$G = (V, E)$$$ relatively prime if and only if for each edge $$$(v, u) \in E$$$  $$$GCD(v, u) = 1$$$ (the greatest common divisor of $$$v$$$ and $$$u$$$ is $$$1$$$). If there is no edge between some pair of vertices $$$v$$$ and $$$u$$$ then the value of $$$GCD(v, u)$$$ doesn't matter. The vertices are numbered from $$$1$$$ to $$$|V|$$$. Construct a relatively prime graph with $$$n$$$ vertices and $$$m$$$ edges such that it is connected and it contains neither self-loops nor multiple edges. If there exists no valid graph with the given number of vertices and edges then output "Impossible". If there are multiple answers then print any of them.</div><h2>Input Format</h2><div>The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of vertices and the number of edges.</div><h2>Output Format</h2><div>If there exists no valid graph with the given number of vertices and edges then output "Impossible". Otherwise print the answer in the following format: The first line should contain the word "Possible". The $$$i$$$-th of the next $$$m$$$ lines should contain the $$$i$$$-th edge $$$(v_i, u_i)$$$ of the resulting graph ($$$1 \le v_i, u_i \le n, v_i \neq u_i$$$). For each pair $$$(v, u)$$$ there can be no more pairs $$$(v, u)$$$ or $$$(u, v)$$$. The vertices are numbered from $$$1$$$ to $$$n$$$. If there are multiple answers then print any of them.</div><h2>Example</h2><h3>Input</h3><div>5 6</div><h3>Output</h3><div>Possible 2 5 3 2 5 1 3 4 4 1 5 4</div><h2>Example</h2><h3>Input</h3><div>6 12</div><h3>Output</h3><div>Impossible</div><h2>Note</h2><div>Here is the representation of the graph from the first example:</div><h2>Editorial</h2><div>Even though $$$n$$$ is up to $$$10^5$$$, straightforward $$$O(n^2 \log n)$$$ solution will work. You iterate for $$$i$$$ from $$$1$$$ to $$$n$$$ in the outer loop, from $$$i + 1$$$ to $$$n$$$ in the inner loop and check $$$GCD$$$ each time. When $$$m$$$ edges are found, you break from both loops. Here is why this work fast enough. The total number of pairs $$$(x, y)$$$ with $$$1 \le x, y \le n, gcd(x, y) = 1$$$ is $$$\varphi(1) + \varphi(2) + \dots + \varphi(n)$$$, where $$$\varphi$$$ is Euler's totient function. We also want to substract a single pair $$$(1, 1)$$$. And this sum grows so fast that after about $$$600$$$ iteratons $$$\varphi(1) + \varphi(2) + \dots + \varphi(600)$$$ will be greater than $$$100000$$$ for any $$$n$$$. The only thing left is to check that $$$m$$$ is big enough to build a connected graph ($$$m \ge n - 1$$$) and small enough to fit all possible edges for given $$$n$$$ (the formula above). Overall complexity: $$$O(n^2 \log n)$$$.</div></body></html>
#include<bits/stdc++.h> using namespace std; #define ll long long int GG(int a,int b) { return b>0?GG(b,a%b):a; } int A[100010],B[100010]; int main() { int n,m; scanf("%d%d",&n,&m); if(m<n-1) { cout<<"Impossible"<<endl; return 0; } int oop=0; for(int i=1; i<=n; i++) { for(int j=i+1; j<=n; j++) { if(GG(i,j)==1) { A[oop]=i; B[oop++]=j; if(oop==m) { printf("Possible\n"); for(int k=0; k<oop; k++) { printf("%d %d\n",A[k],B[k]); } return 0; } } } } printf("Impossible\n"); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Homework</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.</div><h2>Input Format</h2><div>The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).</div><h2>Output Format</h2><div>Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.</div><h2>Example</h2><h3>Input</h3><div>aaaaa 4</div><h3>Output</h3><div>1 aaaaa</div><h2>Example</h2><h3>Input</h3><div>abacaba 4</div><h3>Output</h3><div>1 aaaa</div><h2>Example</h2><h3>Input</h3><div>abcdefgh 10</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.</div></body></html>
#include <cstdio> #include <cstring> char s[100005]; int n,k,f[200]; int p[200]; int main() { scanf("%s",s); scanf("%d",&k); n=strlen(s); memset(f,0,sizeof(f)); for(int i=0;i<n;i++) f[s[i]]++; memset(p,0,sizeof(p)); int m=n-k; int ans=0; for(int i=0;i<200 &&m>0;i++) { int x=0; for(int i=0;i<200;i++) if(f[i]>f[x])x=i; p[x]=f[x]; m-=p[x]; ans++; f[x]=0; } printf("%d\n",ans); int x=0; m=n-k; for(int i=0;i<n;i++) if(p[s[i]]) { putchar(s[i]); } puts(""); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Homework</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.</div><h2>Input Format</h2><div>The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).</div><h2>Output Format</h2><div>Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.</div><h2>Example</h2><h3>Input</h3><div>aaaaa 4</div><h3>Output</h3><div>1 aaaaa</div><h2>Example</h2><h3>Input</h3><div>abacaba 4</div><h3>Output</h3><div>1 aaaa</div><h2>Example</h2><h3>Input</h3><div>abcdefgh 10</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.</div></body></html>
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 100000 char s[N+5]; int cnt[256],ind[256]; int k; bool cmp(int x,int y){ return cnt[x]<cnt[y]; } int main(){ scanf("%s%d",s,&k); for(int i=0;s[i];++i)++cnt[s[i]]; for(int i=0;i<256;++i)ind[i]=i; sort(ind,ind+256,cmp); for(int i=0,j=0;i<k && j<256;)if(cnt[ind[j]]==0)++j; else --cnt[ind[j]],++i; int ans=0; for(int i=0;i<256;++i)if(cnt[ind[i]]>0)++ans; printf("%d\n",ans); for(int i=0;s[i];++i)if(cnt[s[i]]>0)putchar(s[i]),--cnt[s[i]]; printf("\n"); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Homework</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.</div><h2>Input Format</h2><div>The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).</div><h2>Output Format</h2><div>Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.</div><h2>Example</h2><h3>Input</h3><div>aaaaa 4</div><h3>Output</h3><div>1 aaaaa</div><h2>Example</h2><h3>Input</h3><div>abacaba 4</div><h3>Output</h3><div>1 aaaa</div><h2>Example</h2><h3>Input</h3><div>abcdefgh 10</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.</div></body></html>
#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; int k, f[26], c[26]; char ch[100010]; pair<int,int> a[26]; int main() { fgets(ch, sizeof(ch), stdin); scanf("%d",&k); int n = strlen(ch); memset(f, 0, sizeof(f)); memset(c, 0, sizeof(c)); for(int i = 0;i < n; ++i) c[ch[i]-'a'] ++; for(int i = 0;i < 26; ++i) a[i] = make_pair(c[i], i); sort(a, a + 26); for(int i = 0;i < 26; ++i){ int x = a[i].first, y = a[i].second; if(k < x) break; k -= x; f[y] = 1; } int ret = 0; for(int i = 0;i < 26; ++i) if(!f[i]) ret ++; printf("%d\n",ret); for(int i = 0;i < n; ++i) if(!f[ch[i]-'a']) putchar(ch[i]); puts(""); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Homework</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.</div><h2>Input Format</h2><div>The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).</div><h2>Output Format</h2><div>Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.</div><h2>Example</h2><h3>Input</h3><div>aaaaa 4</div><h3>Output</h3><div>1 aaaaa</div><h2>Example</h2><h3>Input</h3><div>abacaba 4</div><h3>Output</h3><div>1 aaaa</div><h2>Example</h2><h3>Input</h3><div>abcdefgh 10</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.</div></body></html>
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; char str[100005]; int c[26]; pair<int,char> A[26]; int main(){ int k; scanf("%s%d",str,&k); int size=strlen(str); for(char ch='a';ch<='z';ch++) A[ch-'a'].second=ch; for(int i=0;i<size;i++) A[str[i]-'a'].first++; sort(A,A+26); int cont=0,p=-1; for(int i=0;i<26;i++){ cont+=A[i].first; c[A[i].second-'a']=i; if(cont<=k) p=i; } printf("%d\n",25-p); for(int i=0;i<size;i++) if(c[str[i]-'a']>p) putchar(str[i]); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Walking Between Houses</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>There are $$$n$$$ houses in a row. They are numbered from $$$1$$$ to $$$n$$$ in order from left to right. Initially you are in the house $$$1$$$. You have to perform $$$k$$$ moves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e., in each move the new house differs from the current house). If you go from the house $$$x$$$ to the house $$$y$$$, the total distance you walked increases by $$$|x-y|$$$ units of distance, where $$$|a|$$$ is the absolute value of $$$a$$$. It is possible to visit the same house multiple times (but you can't visit the same house in sequence). Your goal is to walk exactly $$$s$$$ units of distance in total. If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly $$$k$$$ moves.</div><h2>Input Format</h2><div>The first line of the input contains three integers $$$n$$$, $$$k$$$, $$$s$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le k \le 2 \cdot 10^5$$$, $$$1 \le s \le 10^{18}$$$) — the number of houses, the number of moves and the total distance you want to walk.</div><h2>Output Format</h2><div>If you cannot perform $$$k$$$ moves with total walking distance equal to $$$s$$$, print "NO". Otherwise print "YES" on the first line and then print exactly $$$k$$$ integers $$$h_i$$$ ($$$1 \le h_i \le n$$$) on the second line, where $$$h_i$$$ is the house you visit on the $$$i$$$-th move. For each $$$j$$$ from $$$1$$$ to $$$k-1$$$ the following condition should be satisfied: $$$h_j \ne h_{j + 1}$$$. Also $$$h_1 \ne 1$$$ should be satisfied.</div><h2>Example</h2><h3>Input</h3><div>10 2 15</div><h3>Output</h3><div>YES 10 4</div><h2>Example</h2><h3>Input</h3><div>10 9 45</div><h3>Output</h3><div>YES 10 1 10 1 2 1 2 1 6</div><h2>Example</h2><h3>Input</h3><div>10 9 81</div><h3>Output</h3><div>YES 10 1 10 1 10 1 10 1 10</div><h2>Example</h2><h3>Input</h3><div>10 9 82</div><h3>Output</h3><div>NO</div><h2>Editorial</h2><div>The solution for this problem is very simple: at first, if $$$k > s$$$ or $$$k \cdot (n - 1) < s$$$ the answer is "NO". Otherwise let's do the following thing $$$k$$$ times: let $$$dist$$$ be $$$min(n - 1, s - k + 1)$$$ (we have to greedily decrease the remaining distance but we also should remember about the number of moves which we need to perform). We have to walk to any possible house which is located at distance $$$dist$$$ from the current house (also don't forget to subtract $$$dist$$$ from $$$s$$$). The proof of the fact that we can always walk to the house at distance $$$dist$$$ is very simple: one of the possible answers (which is obtained by the algorithm above) will looks like several moves of distance $$$n-1$$$, (possibly) one move of random distance less than $$$n-1$$$ and several moves of distance $$$1$$$. The first part of the answer can be obtained if we are stay near the leftmost or the rightmost house, second and third parts always can be obtained because distances we will walk in every of such moves is less than $$$n-1$$$. Time complexity is $$$O(k)$$$.</div></body></html>
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define SG string #define DB double #define LL long long using namespace std; const LL Max=2e5+5; LL N,M,K,Ans[Max]; inline LL Read(){ LL X=0;char CH=getchar();bool F=0; while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();} while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();} return F?-X:X; } inline void Write(LL X){ if(X<0)X=-X,putchar('-'); if(X>9)Write(X/10); putchar(X%10+48); } int main(){ LL I,J; N=Read(),M=Read(),K=Read(); if(K<M||(N-1)*M<K){ puts("NO");return 0; }puts("YES"); LL P=K/M; for(I=1;I<=M;I++){ if(I&1){ Ans[I]=1+P; } else { Ans[I]=1; } } if((K%M)&1){ for(I=1;I<=K%M-1;I++){ if(I&1){ Ans[I]++; } } if(M&1){ Ans[M]++; } else { Ans[M-1]=1+P+1;Ans[M]=2; } } else { for(I=1;I<=K%M;I++){ if(I&1){ Ans[I]++; } } } for(I=1;I<=M;I++){ Write(Ans[I]),putchar(' '); } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Vasya And The Matrix</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed! Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively. Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.</div><h2>Input Format</h2><div>The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix. The second line contains n numbers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i. The third line contains m numbers b1, b2, ..., bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.</div><h2>Output Format</h2><div>If there is no matrix satisfying the given constraints in the first line, output "NO". Otherwise, on the first line output "YES", and then n rows of m numbers in each ci1, ci2, ... , cim (0 ≤ cij ≤ 2·109) — the description of the matrix. If there are several suitable matrices, it is allowed to print any of them.</div><h2>Example</h2><h3>Input</h3><div>2 3 2 9 5 3 13</div><h3>Output</h3><div>YES 3 4 5 6 7 8</div><h2>Example</h2><h3>Input</h3><div>3 3 1 7 6 2 15 12</div><h3>Output</h3><div>NO</div><h2>Editorial</h2><div>If $$a_{1} \oplus a_{2} \oplus \cdots \oplus a_{n} \neq b_{1} \oplus b_{2} \oplus \cdots \oplus b_{m}$$, then there is no suitable matrix. The operation $$\bigcirc$$ means xor. Otherwise, we can always construct a suitable matrix by the following method: the first element of the first line will be equal to $$a_{1} \oplus b_{2} \oplus b_{3} \oplus \cdots \oplus b_{m}$$. The second element of the first line is b2, the third element is b3, the last one is bm. The first element of the second line will be a2, the first element of the third line is a3, the first element of the last line is an. The rest of the elements will be zero. It is not difficult to verify that the matrix obtained satisfies all the restrictions.</div></body></html>
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=150; int ans[maxn][maxn]; int a[maxn],b[maxn],c[maxn]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int tot=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); tot^=a[i]; } for(int i=1;i<=m;i++) { scanf("%d",&b[i]); tot^=b[i]; } if(tot) printf("NO\n"); else { printf("YES\n"); memset(c,0,sizeof(c)); for(int i=1;i<n;i++) { tot=0; for(int j=1;j<m;j++) { ans[i][j]=1; tot^=1; c[j]^=1; } ans[i][m]=tot^a[i]; c[m]^=ans[i][m]; } for(int i=1;i<=m;i++) ans[n][i]=b[i]^c[i]; for(int i=1;i<=n;i++) { for(int j=1;j<m;j++) printf("%d ",ans[i][j]); printf("%d\n",ans[i][m]); } } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Vasya And The Matrix</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed! Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively. Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.</div><h2>Input Format</h2><div>The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix. The second line contains n numbers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i. The third line contains m numbers b1, b2, ..., bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.</div><h2>Output Format</h2><div>If there is no matrix satisfying the given constraints in the first line, output "NO". Otherwise, on the first line output "YES", and then n rows of m numbers in each ci1, ci2, ... , cim (0 ≤ cij ≤ 2·109) — the description of the matrix. If there are several suitable matrices, it is allowed to print any of them.</div><h2>Example</h2><h3>Input</h3><div>2 3 2 9 5 3 13</div><h3>Output</h3><div>YES 3 4 5 6 7 8</div><h2>Example</h2><h3>Input</h3><div>3 3 1 7 6 2 15 12</div><h3>Output</h3><div>NO</div><h2>Editorial</h2><div>If $$a_{1} \oplus a_{2} \oplus \cdots \oplus a_{n} \neq b_{1} \oplus b_{2} \oplus \cdots \oplus b_{m}$$, then there is no suitable matrix. The operation $$\bigcirc$$ means xor. Otherwise, we can always construct a suitable matrix by the following method: the first element of the first line will be equal to $$a_{1} \oplus b_{2} \oplus b_{3} \oplus \cdots \oplus b_{m}$$. The second element of the first line is b2, the third element is b3, the last one is bm. The first element of the second line will be a2, the first element of the third line is a3, the first element of the last line is an. The rest of the elements will be zero. It is not difficult to verify that the matrix obtained satisfies all the restrictions.</div></body></html>
#include <bits/stdc++.h> int N, M; int A[200], B[200]; int main () { std::ios::sync_with_stdio (0); std::cin >> N >> M; for (int i = 0; i < N; ++i) std::cin >> A[i]; for (int i = 0; i < M; ++i) std::cin >> B[i]; int a = 0, b = 0; for (int i = 0; i < N; ++i) a ^= A[i]; for (int i = 0; i < M; ++i) b ^= B[i]; if (a != b) return std::cout << "NO\n", 0; std::cout << "YES\n" << (a ^ A[0] ^ B[0]); for (int i = 1; i < M; ++i) std::cout << " " << B[i]; std::cout << "\n"; for (int i = 1; i < N; ++i) { std::cout << A[i]; for (int j = 1; j < M; ++j) std::cout << " 0"; std::cout << "\n"; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>The Phone Number</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The only thing Mrs. Smith remembered was that any permutation of $$$n$$$ can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband. The sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once. The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence $$$a_{i_1}, a_{i_2}, \ldots, a_{i_k}$$$ where $$$1\leq i_1 < i_2 < \ldots < i_k\leq n$$$ is called increasing if $$$a_{i_1} < a_{i_2} < a_{i_3} < \ldots < a_{i_k}$$$. If $$$a_{i_1} > a_{i_2} > a_{i_3} > \ldots > a_{i_k}$$$, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences. For example, if there is a permutation $$$[6, 4, 1, 7, 2, 3, 5]$$$, LIS of this permutation will be $$$[1, 2, 3, 5]$$$, so the length of LIS is equal to $$$4$$$. LDS can be $$$[6, 4, 1]$$$, $$$[6, 4, 2]$$$, or $$$[6, 4, 3]$$$, so the length of LDS is $$$3$$$. Note, the lengths of LIS and LDS can be different. So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.</div><h2>Input Format</h2><div>The only line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of permutation that you need to build.</div><h2>Output Format</h2><div>Print a permutation that gives a minimum sum of lengths of LIS and LDS. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>4</div><h3>Output</h3><div>3 4 1 2</div><h2>Example</h2><h3>Input</h3><div>2</div><h3>Output</h3><div>2 1</div><h2>Note</h2><div>In the first sample, you can build a permutation $$$[3, 4, 1, 2]$$$. LIS is $$$[3, 4]$$$ (or $$$[1, 2]$$$), so the length of LIS is equal to $$$2$$$. LDS can be ony of $$$[3, 1]$$$, $$$[4, 2]$$$, $$$[3, 2]$$$, or $$$[4, 1]$$$. The length of LDS is also equal to $$$2$$$. The sum is equal to $$$4$$$. Note that $$$[3, 4, 1, 2]$$$ is not the only permutation that is valid. In the second sample, you can build a permutation $$$[2, 1]$$$. LIS is $$$[1]$$$ (or $$$[2]$$$), so the length of LIS is equal to $$$1$$$. LDS is $$$[2, 1]$$$, so the length of LDS is equal to $$$2$$$. The sum is equal to $$$3$$$. Note that permutation $$$[1, 2]$$$ is also valid.</div><h2>Editorial</h2><div>Show an example of $$$n = 22$$$: "' 19 20 21 22 15 16 17 18 11 12 13 14 7 8 9 10 3 4 5 6 1 2 "' You can use [Dilworth's theorem](https://en.wikipedia.org/wiki/Dilworth So assume we've already known that $$$LIS = L$$$, then we can achieve $$$LDS = \big\lceil\frac{n}{L}\big\rceil$$$. So after enumerating all possible $$$L$$$ and find the minimum of function $$$L \big\lceil\frac{n}{L}\big\rceil$$$, we can construct the sequence easily just as the case when $$$n = 22$$$. Actually, $$$L = \big\lfloor \sqrt n \big\rfloor$$$ will always work. Complexity: $$$O(n)$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; #define _USE_MATH_DEFINES typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef complex<ld> pt; typedef vector<pt> pol; const char nl = '\n'; const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const ll MOD = 1e9+7; const ld EPS = 1e-10; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); //#define FILEIO int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); #ifdef FILEIO freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); #endif int n; cin >> n; int m = sqrt(n); int top = n/m; for (int i = m*top+1; i <= n; i++) { cout << i << " "; } for (int s = m*top-m; s >= 0; s-=m) { for (int i = 1; i <= m; i++) { cout << s+i << " "; } } cout << nl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>The Phone Number</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The only thing Mrs. Smith remembered was that any permutation of $$$n$$$ can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband. The sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once. The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence $$$a_{i_1}, a_{i_2}, \ldots, a_{i_k}$$$ where $$$1\leq i_1 < i_2 < \ldots < i_k\leq n$$$ is called increasing if $$$a_{i_1} < a_{i_2} < a_{i_3} < \ldots < a_{i_k}$$$. If $$$a_{i_1} > a_{i_2} > a_{i_3} > \ldots > a_{i_k}$$$, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences. For example, if there is a permutation $$$[6, 4, 1, 7, 2, 3, 5]$$$, LIS of this permutation will be $$$[1, 2, 3, 5]$$$, so the length of LIS is equal to $$$4$$$. LDS can be $$$[6, 4, 1]$$$, $$$[6, 4, 2]$$$, or $$$[6, 4, 3]$$$, so the length of LDS is $$$3$$$. Note, the lengths of LIS and LDS can be different. So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.</div><h2>Input Format</h2><div>The only line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of permutation that you need to build.</div><h2>Output Format</h2><div>Print a permutation that gives a minimum sum of lengths of LIS and LDS. If there are multiple answers, print any.</div><h2>Example</h2><h3>Input</h3><div>4</div><h3>Output</h3><div>3 4 1 2</div><h2>Example</h2><h3>Input</h3><div>2</div><h3>Output</h3><div>2 1</div><h2>Note</h2><div>In the first sample, you can build a permutation $$$[3, 4, 1, 2]$$$. LIS is $$$[3, 4]$$$ (or $$$[1, 2]$$$), so the length of LIS is equal to $$$2$$$. LDS can be ony of $$$[3, 1]$$$, $$$[4, 2]$$$, $$$[3, 2]$$$, or $$$[4, 1]$$$. The length of LDS is also equal to $$$2$$$. The sum is equal to $$$4$$$. Note that $$$[3, 4, 1, 2]$$$ is not the only permutation that is valid. In the second sample, you can build a permutation $$$[2, 1]$$$. LIS is $$$[1]$$$ (or $$$[2]$$$), so the length of LIS is equal to $$$1$$$. LDS is $$$[2, 1]$$$, so the length of LDS is equal to $$$2$$$. The sum is equal to $$$3$$$. Note that permutation $$$[1, 2]$$$ is also valid.</div><h2>Editorial</h2><div>Show an example of $$$n = 22$$$: "' 19 20 21 22 15 16 17 18 11 12 13 14 7 8 9 10 3 4 5 6 1 2 "' You can use [Dilworth's theorem](https://en.wikipedia.org/wiki/Dilworth So assume we've already known that $$$LIS = L$$$, then we can achieve $$$LDS = \big\lceil\frac{n}{L}\big\rceil$$$. So after enumerating all possible $$$L$$$ and find the minimum of function $$$L \big\lceil\frac{n}{L}\big\rceil$$$, we can construct the sequence easily just as the case when $$$n = 22$$$. Actually, $$$L = \big\lfloor \sqrt n \big\rfloor$$$ will always work. Complexity: $$$O(n)$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long llu; #define MOD 1000000007 #define print(A,n) for(ll i=0;i<n;++i)cout<<A[i].ss<<' ';cout<<endl; #define _accept(X,N) for(ll i=0;i<N;++i)cin>>X[i]; #define forzer(i,n) for(ll i=0;i<n;++i) #define rep(i,s,e) for(ll i=s;i<e;++i) #define pb push_back #define mp make_pair #define ff first #define ss second int main() { ll n,x,y; cin>>n; x=n/2+1,y=1; ll min=MOD,div=-1; for(ll i=1;i<=n;++i) { if(ceil(n/(double)i)+i<=min) { min=ceil(n/(double)i)+i; div=i; } else break; } ll nper=ceil(n/(double)div); for(ll i=1;i<=div;++i) { for(ll j=0;j<nper;++j) { if((div-i)*nper+j+1<=n) cout<<(div-i)*nper+j+1<<' '; } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Bracket Subsequence</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. You are given a regular bracket sequence $$$s$$$ and an integer number $$$k$$$. Your task is to find a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$. It is guaranteed that such sequence always exists.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 2 \cdot 10^5$$$, both $$$n$$$ and $$$k$$$ are even) — the length of $$$s$$$ and the length of the sequence you are asked to find. The second line is a string $$$s$$$ — regular bracket sequence of length $$$n$$$.</div><h2>Output Format</h2><div>Print a single string — a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$. It is guaranteed that such sequence always exists.</div><h2>Example</h2><h3>Input</h3><div>6 4 ()(())</div><h3>Output</h3><div>()()</div><h2>Example</h2><h3>Input</h3><div>8 8 (()(()))</div><h3>Output</h3><div>(()(()))</div><h2>Editorial</h2><div>Let the array $$$used$$$ of $$$n$$$ boolean values describe if the corresponding bracket of string $$$s$$$ is included in answer or not. The algorithm goes like this: iterate over the string from $$$1$$$ to $$$n$$$, maintain the stack of positions of currenly unmatched opening brackets $$$st$$$. When opening bracket is met at position $$$i$$$, push $$$i$$$ to $$$st$$$, and when closing bracket is met, set $$$used[st.top()] = True$$$, $$$used[i] = True$$$ and pop the top of $$$st$$$. When $$$k$$$ values are marked True in $$$used$$$ then break, iterate from $$$1$$$ to $$$n$$$ and print the brackets at positions where $$$used[i] = True$$$. Obviously, this algorithm produces a subsequence of $$$s$$$ of length $$$k$$$. Why will it be a regular bracket sequence? The requirements for it are: 1. no prefix contains more closing brackets than opening ones; 2. the total number of closing bracket equals the total number of opening brackets. The first requirement is met by construction, we couldn't pop more elements from the stack than it had. The second requirement is also met as we marked brackets in pairs. Overall complexity: $$$O(n)$$$.</div></body></html>
#include <cstdio> #include <cstring> #define N 200005 char a[N]; int n, k; int main(){ scanf("%d%d%s", &n, &k, a + 1); int cnt = 0, top = 0; for (register int i = 1; i <= n; ++i){ if (a[i] == '(' && cnt < (k >> 1)) putchar('('), ++cnt, ++top; if (a[i] == ')' && top > 0) putchar(')'), --top; } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Bracket Subsequence</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. You are given a regular bracket sequence $$$s$$$ and an integer number $$$k$$$. Your task is to find a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$. It is guaranteed that such sequence always exists.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 2 \cdot 10^5$$$, both $$$n$$$ and $$$k$$$ are even) — the length of $$$s$$$ and the length of the sequence you are asked to find. The second line is a string $$$s$$$ — regular bracket sequence of length $$$n$$$.</div><h2>Output Format</h2><div>Print a single string — a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$. It is guaranteed that such sequence always exists.</div><h2>Example</h2><h3>Input</h3><div>6 4 ()(())</div><h3>Output</h3><div>()()</div><h2>Example</h2><h3>Input</h3><div>8 8 (()(()))</div><h3>Output</h3><div>(()(()))</div><h2>Editorial</h2><div>Let the array $$$used$$$ of $$$n$$$ boolean values describe if the corresponding bracket of string $$$s$$$ is included in answer or not. The algorithm goes like this: iterate over the string from $$$1$$$ to $$$n$$$, maintain the stack of positions of currenly unmatched opening brackets $$$st$$$. When opening bracket is met at position $$$i$$$, push $$$i$$$ to $$$st$$$, and when closing bracket is met, set $$$used[st.top()] = True$$$, $$$used[i] = True$$$ and pop the top of $$$st$$$. When $$$k$$$ values are marked True in $$$used$$$ then break, iterate from $$$1$$$ to $$$n$$$ and print the brackets at positions where $$$used[i] = True$$$. Obviously, this algorithm produces a subsequence of $$$s$$$ of length $$$k$$$. Why will it be a regular bracket sequence? The requirements for it are: 1. no prefix contains more closing brackets than opening ones; 2. the total number of closing bracket equals the total number of opening brackets. The first requirement is met by construction, we couldn't pop more elements from the stack than it had. The second requirement is also met as we marked brackets in pairs. Overall complexity: $$$O(n)$$$.</div></body></html>
/* yusitong 2018-08-03 */ #define _CRT_SECURE_NO_WARNINGS #include"bits/stdc++.h" using namespace std; typedef long long LL; const int INF = 0x7fffffff; const int N = 2e5 + 100; int n,k; char s[N],sta[N]; int main() { cin>>n>>k; scanf("%s",s+1); int top=0,a=0,b=0; for(int i=1;i<=n&&top<k;i++){ if(s[i]=='('){ if(a<k/2){ sta[++top]=s[i]; ++a; } }else{ if(a>b){ sta[++top]=s[i]; ++b; } } } puts(sta+1); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Minimum Value Rectangle</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>You have $$$n$$$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately.</div><h2>Input Format</h2><div>The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase.</div><h2>Output Format</h2><div>Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them.</div><h2>Example</h2><h3>Input</h3><div>3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5</div><h3>Output</h3><div>2 7 7 2 2 2 1 1 5 5 5 5</div><h2>Note</h2><div>There is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$. The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$. You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$.</div><h2>Editorial</h2><div>Let's work with the formula a bit: $$$\frac{P^2}{s}$$$ $$$=$$$ $$$\frac{4(x + y)^2}{xy}$$$ $$$=$$$ $$$4(2 + \frac x y + \frac y x)$$$ $$$=$$$ $$$8 + 4(\frac x y + \frac y x)$$$ Let $$$a = \frac x y$$$, then the formula becomes $$$8 + 4(a + \frac 1 a)$$$. Considering $$$x \ge y$$$, $$$a = \frac x y \ge 1$$$, thus $$$(a + \frac 1 a)$$$ is strictly increasing and has its minimum at $$$a = 1$$$. So the solution will be to sort the list, extract the pairs of sticks of equal length and check only neighbouring pairs in sorted order for the answer. Overall complexity: $$$O(n \log n)$$$.</div></body></html>
//Problem C #include<bits/stdc++.h> using namespace std; int t,n,a[1000005],b[1000005],s,v,u; void rd(int &a){ a=0;char ch=getchar(); while(!isdigit(ch))ch=getchar(); while( isdigit(ch))a=(a<<1)+(a<<3)+(ch^48),ch=getchar(); } long long cal(int a,int b,int c,int d){ return 1ll*(a+b)*(a+b)*c*d; } void op(int a){ if(9<a)op(a/10); putchar((a%10)|48); } void oop(int a,char ch){ op(a),putchar(ch); } int main(){ rd(t); while(t--){ rd(n),s=0; for(int i=1;i<=n;i++)rd(a[i]); sort(a+1,a+n+1); v=u=0; int f=0; for(int i=1,j=1;i<=n;i=j){ while(j<=n&&a[i]==a[j])++j; int x=(j-i)>>1; while(x--)b[++s]=a[i]; } for(int i=1;i<s;i++){ int V=b[i],U=b[i+1]; if(!v)v=V,u=U;else if(cal(V,U,v,u)<cal(v,u,V,U))v=V,u=U; } oop(v,' '); oop(v,' '); oop(u,' '); oop(u,'\n'); } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Unnatural Conditions</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let $$$s(x)$$$ be sum of digits in decimal representation of positive integer $$$x$$$. Given two integers $$$n$$$ and $$$m$$$, find some positive integers $$$a$$$ and $$$b$$$ such that - $$$s(a) \ge n$$$, - $$$s(b) \ge n$$$, - $$$s(a + b) \le m$$$.</div><h2>Input Format</h2><div>The only line of input contain two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1129$$$).</div><h2>Output Format</h2><div>Print two lines, one for decimal representation of $$$a$$$ and one for decimal representation of $$$b$$$. Both numbers must not contain leading zeros and must have length no more than $$$2230$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5</div><h3>Output</h3><div>6 7</div><h2>Example</h2><h3>Input</h3><div>8 16</div><h3>Output</h3><div>35 53</div><h2>Note</h2><div>In the first sample, we have $$$n = 6$$$ and $$$m = 5$$$. One valid solution is $$$a = 6$$$, $$$b = 7$$$. Indeed, we have $$$s(a) = 6 \ge n$$$ and $$$s(b) = 7 \ge n$$$, and also $$$s(a + b) = s(13) = 4 \le m$$$.</div><h2>Editorial</h2><div>First note, that if some output is correct for test with $$$n = 1129$$$ and $$$m = 1$$$, then it's correct for any valid test. After noticing this, we don't need to read input and output one answer for any test. One of many possible answers is $$$a = 99..9900..001$$$ (200 nines, 199 zeroes, 1 one) $$$b = 99..99$$$ (200 nines) $$$a + b = 10^{400}$$$ $$$s(a) = 1801, s(b) = 1800, s(a+b) = 1$$$</div></body></html>
#include <bits/stdc++.h> using namespace std; string a, b; int main(){ int n, m; cin>>n>>m; if(n<m){ for(int i = 0;i<n;i++) a+='1', b+='1'; if(2*n-m>=0) for(int d=2*n-m;d>0;d--) b+='0'; if(2*n-m<0) for(int d=m-2*n;d>0;d--) b = '1'+b; cout<<b<<endl<<a; }else{ for(int i = 0;i<n;i++) a+='1'; for(int i =0;i<m-1;i++) b+='9'; for(int i=0;i<n-m;i++) b+='8'; b+='9'; cout<<b<<endl<<a; } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Unnatural Conditions</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let $$$s(x)$$$ be sum of digits in decimal representation of positive integer $$$x$$$. Given two integers $$$n$$$ and $$$m$$$, find some positive integers $$$a$$$ and $$$b$$$ such that - $$$s(a) \ge n$$$, - $$$s(b) \ge n$$$, - $$$s(a + b) \le m$$$.</div><h2>Input Format</h2><div>The only line of input contain two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1129$$$).</div><h2>Output Format</h2><div>Print two lines, one for decimal representation of $$$a$$$ and one for decimal representation of $$$b$$$. Both numbers must not contain leading zeros and must have length no more than $$$2230$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5</div><h3>Output</h3><div>6 7</div><h2>Example</h2><h3>Input</h3><div>8 16</div><h3>Output</h3><div>35 53</div><h2>Note</h2><div>In the first sample, we have $$$n = 6$$$ and $$$m = 5$$$. One valid solution is $$$a = 6$$$, $$$b = 7$$$. Indeed, we have $$$s(a) = 6 \ge n$$$ and $$$s(b) = 7 \ge n$$$, and also $$$s(a + b) = s(13) = 4 \le m$$$.</div><h2>Editorial</h2><div>First note, that if some output is correct for test with $$$n = 1129$$$ and $$$m = 1$$$, then it's correct for any valid test. After noticing this, we don't need to read input and output one answer for any test. One of many possible answers is $$$a = 99..9900..001$$$ (200 nines, 199 zeroes, 1 one) $$$b = 99..99$$$ (200 nines) $$$a + b = 10^{400}$$$ $$$s(a) = 1801, s(b) = 1800, s(a+b) = 1$$$</div></body></html>
#include<bits/stdc++.h> using namespace std; int main(){ int n,m; cin>>n>>m; int i,j,l; deque<int> dq; j=n; if(j>=5) {dq.push_front(5); j=j-5;} else{ dq.push_front(j); j=0; } while(j>0){ if(j>4){ dq.push_front(4); j=j-4; } else{ dq.push_front(j); j=0; } } vector<int> v; while(!dq.empty()){ l=dq.front(); dq.pop_front(); v.push_back(l); } vector<int> vv; for(i=0;i<v.size();i++){ if(i!=v.size()-1){ l=9-v[i]; vv.push_back(l); } else{ l=10-v[i]; vv.push_back(l); } } for(i=0;i<v.size();i++){ cout<<v[i]; } cout<<endl; for(i=0;i<vv.size();i++){ cout<<vv[i]; } cout<<endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Unnatural Conditions</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let $$$s(x)$$$ be sum of digits in decimal representation of positive integer $$$x$$$. Given two integers $$$n$$$ and $$$m$$$, find some positive integers $$$a$$$ and $$$b$$$ such that - $$$s(a) \ge n$$$, - $$$s(b) \ge n$$$, - $$$s(a + b) \le m$$$.</div><h2>Input Format</h2><div>The only line of input contain two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1129$$$).</div><h2>Output Format</h2><div>Print two lines, one for decimal representation of $$$a$$$ and one for decimal representation of $$$b$$$. Both numbers must not contain leading zeros and must have length no more than $$$2230$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5</div><h3>Output</h3><div>6 7</div><h2>Example</h2><h3>Input</h3><div>8 16</div><h3>Output</h3><div>35 53</div><h2>Note</h2><div>In the first sample, we have $$$n = 6$$$ and $$$m = 5$$$. One valid solution is $$$a = 6$$$, $$$b = 7$$$. Indeed, we have $$$s(a) = 6 \ge n$$$ and $$$s(b) = 7 \ge n$$$, and also $$$s(a + b) = s(13) = 4 \le m$$$.</div><h2>Editorial</h2><div>First note, that if some output is correct for test with $$$n = 1129$$$ and $$$m = 1$$$, then it's correct for any valid test. After noticing this, we don't need to read input and output one answer for any test. One of many possible answers is $$$a = 99..9900..001$$$ (200 nines, 199 zeroes, 1 one) $$$b = 99..99$$$ (200 nines) $$$a + b = 10^{400}$$$ $$$s(a) = 1801, s(b) = 1800, s(a+b) = 1$$$</div></body></html>
#include <iostream> #include <algorithm> #include <vector> #include <cstring> #include <map> #include <set> #include <queue> #include <cstdio> #include <cmath> #include <limits.h> using namespace std; typedef long long LL; int n,m; int main() { scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) { printf("1"); } printf("\n"); for(int i=1;i<n;i++) { printf("8"); } printf("9\n"); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Unnatural Conditions</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let $$$s(x)$$$ be sum of digits in decimal representation of positive integer $$$x$$$. Given two integers $$$n$$$ and $$$m$$$, find some positive integers $$$a$$$ and $$$b$$$ such that - $$$s(a) \ge n$$$, - $$$s(b) \ge n$$$, - $$$s(a + b) \le m$$$.</div><h2>Input Format</h2><div>The only line of input contain two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1129$$$).</div><h2>Output Format</h2><div>Print two lines, one for decimal representation of $$$a$$$ and one for decimal representation of $$$b$$$. Both numbers must not contain leading zeros and must have length no more than $$$2230$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5</div><h3>Output</h3><div>6 7</div><h2>Example</h2><h3>Input</h3><div>8 16</div><h3>Output</h3><div>35 53</div><h2>Note</h2><div>In the first sample, we have $$$n = 6$$$ and $$$m = 5$$$. One valid solution is $$$a = 6$$$, $$$b = 7$$$. Indeed, we have $$$s(a) = 6 \ge n$$$ and $$$s(b) = 7 \ge n$$$, and also $$$s(a + b) = s(13) = 4 \le m$$$.</div><h2>Editorial</h2><div>First note, that if some output is correct for test with $$$n = 1129$$$ and $$$m = 1$$$, then it's correct for any valid test. After noticing this, we don't need to read input and output one answer for any test. One of many possible answers is $$$a = 99..9900..001$$$ (200 nines, 199 zeroes, 1 one) $$$b = 99..99$$$ (200 nines) $$$a + b = 10^{400}$$$ $$$s(a) = 1801, s(b) = 1800, s(a+b) = 1$$$</div></body></html>
#include <bits/stdc++.h> using namespace std; int main(){ int n, m; cin>>n>>m; int x; x=n/9+1; /*if(n%9){ x=n/9+1; }else{ x=n/9; }*/ for(int i=0;i<x;i++){ cout<<'9'; } cout<<"\n"; for(int i=0;i<x;i++){ cout<<'9'; } for(int i=0;i<x-1;i++){ cout<<'0'; } cout<<'1'; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Unnatural Conditions</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let $$$s(x)$$$ be sum of digits in decimal representation of positive integer $$$x$$$. Given two integers $$$n$$$ and $$$m$$$, find some positive integers $$$a$$$ and $$$b$$$ such that - $$$s(a) \ge n$$$, - $$$s(b) \ge n$$$, - $$$s(a + b) \le m$$$.</div><h2>Input Format</h2><div>The only line of input contain two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1129$$$).</div><h2>Output Format</h2><div>Print two lines, one for decimal representation of $$$a$$$ and one for decimal representation of $$$b$$$. Both numbers must not contain leading zeros and must have length no more than $$$2230$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5</div><h3>Output</h3><div>6 7</div><h2>Example</h2><h3>Input</h3><div>8 16</div><h3>Output</h3><div>35 53</div><h2>Note</h2><div>In the first sample, we have $$$n = 6$$$ and $$$m = 5$$$. One valid solution is $$$a = 6$$$, $$$b = 7$$$. Indeed, we have $$$s(a) = 6 \ge n$$$ and $$$s(b) = 7 \ge n$$$, and also $$$s(a + b) = s(13) = 4 \le m$$$.</div><h2>Editorial</h2><div>First note, that if some output is correct for test with $$$n = 1129$$$ and $$$m = 1$$$, then it's correct for any valid test. After noticing this, we don't need to read input and output one answer for any test. One of many possible answers is $$$a = 99..9900..001$$$ (200 nines, 199 zeroes, 1 one) $$$b = 99..99$$$ (200 nines) $$$a + b = 10^{400}$$$ $$$s(a) = 1801, s(b) = 1800, s(a+b) = 1$$$</div></body></html>
#include<bits/stdc++.h> typedef long long ll; using namespace std; int main() { int a,b; cin >> a >> b; int no = a/9; int lst = a%9; for(int i=0;i<no;i++) cout << 9; cout << lst; cout << endl; for(int i =0;i<no+1;i++) cout << 9; if(!lst) no--; for(int i=0;i<no;i++) cout << 0; cout << 10-lst; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Restore Array</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $$$a_1, a_2, \ldots, a_n$$$. Since the talk was long and not promising, Kostya created a new cyclic array $$$b_1, b_2, \ldots, b_{n}$$$ so that $$$b_i = (a_i \mod a_{i + 1})$$$, where we take $$$a_{n+1} = a_1$$$. Here $$$mod$$$ is the modulo operation. When the talk became interesting, Kostya completely forgot how array $$$a$$$ had looked like. Suddenly, he thought that restoring array $$$a$$$ from array $$$b$$$ would be an interesting problem (unfortunately, not A).</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$2 \le n \le 140582$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_{n}$$$ ($$$0 \le b_i \le 187126$$$).</div><h2>Output Format</h2><div>If it is possible to restore some array $$$a$$$ of length $$$n$$$ so that $$$b_i = a_i \mod a_{(i \mod n) + 1}$$$ holds for all $$$i = 1, 2, \ldots, n$$$, print «YES» in the first line and the integers $$$a_1, a_2, \ldots, a_n$$$ in the second line. All $$$a_i$$$ should satisfy $$$1 \le a_i \le 10^{18}$$$. We can show that if an answer exists, then an answer with such constraint exists as well. It it impossible to restore any valid $$$a$$$, print «NO» in one line. You can print each letter in any case (upper or lower).</div><h2>Example</h2><h3>Input</h3><div>4 1 3 1 0</div><h3>Output</h3><div>YES 1 3 5 2</div><h2>Example</h2><h3>Input</h3><div>2 4 4</div><h3>Output</h3><div>NO</div><h2>Note</h2><div>In the first example: - $$$1 \mod 3 = 1$$$ - $$$3 \mod 5 = 3$$$ - $$$5 \mod 2 = 1$$$ - $$$2 \mod 1 = 0$$$</div><h2>Editorial</h2><div>Let's consider a special case at first: all numbers are equal to $$$M$$$. Then the answer exists only if $$$M = 0$$$ (prove it as an exercise). Now let $$$M$$$ be the maximum value in array $$$b$$$. Then there exists an index $$$i$$$ such that $$$b_i = M$$$ and $$$b_{i-1} < M$$$ (we assume $$$b_0 = b_n$$$). We assume it's $$$b_n$$$, otherwise we just shift the array to make this happen. Then $$$a_n = b_n$$$ and $$$$$$a_i = \sum\limits_{j=i}^{n-1} b_j + 2 b_n,~i < n$$$$$$ satisfies all the constraints. That's true, because • $$$a_n < a_1$$$, • $$$a_i - a_{i+1} = b_i$$$ for $$$i < n$$$, • $$$b_i < a_{i+1}$$$ for $$$i < n - 1$$$ because $$$b_n$$$ is maximum value, which is greater than $$$0$$$, • $$$b_{n-1} < b_n = M$$$, • $$$b_n < a_1$$$ because $$$b_n < 2 b_n$$$. Note that without multiplier $$$2$$$ before $$$b_n$$$ in the formula solution won't work if all numbers in the array except for one are zeroes, because in this case $$$a_n \mod a_{1} = 0$$$. Also instead of $$$2b_n$$$ it's possible to use something like $$$b_n + 10^{10}$$$, the proof doesn't change much in this case.</div></body></html>
#include <algorithm> #include <cctype> #include <cstdio> #include <cstring> typedef long long LL; inline int readInt() { int ans = 0, c, f = 1; while (!isdigit(c = getchar())) if (c == '-') f *= -1; do ans = ans * 10 + c - '0'; while (isdigit(c = getchar())); return ans * f; } const int N = 200050; LL B[N], A[N]; int main() { int n = readInt(); for (int i = 0; i < n; ++i) B[i] = readInt(); int k = 0; while (k < n && B[k] <= B[(k - 1 + n) % n]) ++k; if (k >= n) { if (B[0] == 0) { puts("YES"); for (int i = 0; i < n; ++i) printf("1 "); return 0; } return puts("NO") & 0; } A[k] = B[k]; for (int i = (k - 1 + n) % n; i != k; i = (i - 1 + n) % n) { LL tb = B[(i - 1 + n) % n], ta = A[(i + 1) % n]; if (tb < B[i]) A[i] = B[i]; else A[i] = ((tb - B[i]) / ta + 1) * ta + B[i]; } puts("YES"); for (int i = 0; i < n; ++i) printf("%I64d ", A[i]); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Restore Array</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $$$a_1, a_2, \ldots, a_n$$$. Since the talk was long and not promising, Kostya created a new cyclic array $$$b_1, b_2, \ldots, b_{n}$$$ so that $$$b_i = (a_i \mod a_{i + 1})$$$, where we take $$$a_{n+1} = a_1$$$. Here $$$mod$$$ is the modulo operation. When the talk became interesting, Kostya completely forgot how array $$$a$$$ had looked like. Suddenly, he thought that restoring array $$$a$$$ from array $$$b$$$ would be an interesting problem (unfortunately, not A).</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$2 \le n \le 140582$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_{n}$$$ ($$$0 \le b_i \le 187126$$$).</div><h2>Output Format</h2><div>If it is possible to restore some array $$$a$$$ of length $$$n$$$ so that $$$b_i = a_i \mod a_{(i \mod n) + 1}$$$ holds for all $$$i = 1, 2, \ldots, n$$$, print «YES» in the first line and the integers $$$a_1, a_2, \ldots, a_n$$$ in the second line. All $$$a_i$$$ should satisfy $$$1 \le a_i \le 10^{18}$$$. We can show that if an answer exists, then an answer with such constraint exists as well. It it impossible to restore any valid $$$a$$$, print «NO» in one line. You can print each letter in any case (upper or lower).</div><h2>Example</h2><h3>Input</h3><div>4 1 3 1 0</div><h3>Output</h3><div>YES 1 3 5 2</div><h2>Example</h2><h3>Input</h3><div>2 4 4</div><h3>Output</h3><div>NO</div><h2>Note</h2><div>In the first example: - $$$1 \mod 3 = 1$$$ - $$$3 \mod 5 = 3$$$ - $$$5 \mod 2 = 1$$$ - $$$2 \mod 1 = 0$$$</div><h2>Editorial</h2><div>Let's consider a special case at first: all numbers are equal to $$$M$$$. Then the answer exists only if $$$M = 0$$$ (prove it as an exercise). Now let $$$M$$$ be the maximum value in array $$$b$$$. Then there exists an index $$$i$$$ such that $$$b_i = M$$$ and $$$b_{i-1} < M$$$ (we assume $$$b_0 = b_n$$$). We assume it's $$$b_n$$$, otherwise we just shift the array to make this happen. Then $$$a_n = b_n$$$ and $$$$$$a_i = \sum\limits_{j=i}^{n-1} b_j + 2 b_n,~i < n$$$$$$ satisfies all the constraints. That's true, because • $$$a_n < a_1$$$, • $$$a_i - a_{i+1} = b_i$$$ for $$$i < n$$$, • $$$b_i < a_{i+1}$$$ for $$$i < n - 1$$$ because $$$b_n$$$ is maximum value, which is greater than $$$0$$$, • $$$b_{n-1} < b_n = M$$$, • $$$b_n < a_1$$$ because $$$b_n < 2 b_n$$$. Note that without multiplier $$$2$$$ before $$$b_n$$$ in the formula solution won't work if all numbers in the array except for one are zeroes, because in this case $$$a_n \mod a_{1} = 0$$$. Also instead of $$$2b_n$$$ it's possible to use something like $$$b_n + 10^{10}$$$, the proof doesn't change much in this case.</div></body></html>
#include <cstdio> using namespace std; int b[141000]; long long a[141000]; int main(){ int N; scanf("%d", &N); for(int i = 1; i <= N; i++) scanf("%d", &b[i]); int bi = 0; b[0] = b[N]; for(int i = 1; i <= N; i++) if(b[i] > b[i - 1]){ bi = i; break; } if(bi == 0){ if(b[1] == 0){ puts("YES"); for(int i = 1; i <= N; i++) printf("%d ", 1); printf("\n"); } else puts("NO"); return 0; } a[bi] = b[bi]; for(int i = bi - 1; i >= 1; i--){ if(b[i - 1] < b[i]) a[i] = b[i]; else{ int db = b[i - 1] - b[i]; a[i] = db - db % a[i + 1] + a[i + 1] + b[i]; } } a[N + 1] = a[1]; for(int i = N; i > bi; i--){ if(b[i - 1] < b[i]) a[i] = b[i]; else{ int db = b[i - 1] - b[i]; a[i] = db - db % a[i + 1] + a[i + 1] + b[i]; } } puts("YES"); for(int i = 1; i <= N; i++) printf("%lld ", a[i]); printf("\n"); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>In Search of an Easy Problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>When preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked $$$n$$$ people about their opinions. Each person answered whether this problem is easy or hard. If at least one of these $$$n$$$ people has answered that the problem is hard, the coordinator decides to change the problem. For the given responses, check if the problem is easy enough.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of people who were asked to give their opinions. The second line contains $$$n$$$ integers, each integer is either $$$0$$$ or $$$1$$$. If $$$i$$$-th integer is $$$0$$$, then $$$i$$$-th person thinks that the problem is easy; if it is $$$1$$$, then $$$i$$$-th person thinks that the problem is hard.</div><h2>Output Format</h2><div>Print one word: "EASY" if the problem is easy according to all responses, or "HARD" if there is at least one person who thinks the problem is hard. You may print every letter in any register: "EASY", "easy", "EaSY" and "eAsY" all will be processed correctly.</div><h2>Example</h2><h3>Input</h3><div>3 0 0 1</div><h3>Output</h3><div>HARD</div><h2>Example</h2><h3>Input</h3><div>1 0</div><h3>Output</h3><div>EASY</div><h2>Note</h2><div>In the first example the third person says it's a hard problem, so it should be replaced. In the second example the problem easy for the only person, so it doesn't have to be replaced.</div><h2>Editorial</h2><div>Codebait. Comforting problem. Find $$$\max\limits_{i=1..n}{(\text{answer}_i)}$$$, if it's equal to $$$1$$$ then answer is HARD, otherwise — EASY.</div></body></html>
#include <bits/stdc++.h> using namespace std; int main() { int i = 0; bool ans = 1; int n; cin >> n; char in; for (i; i < n; i++) { cin >> in; if (in == '1'){ ans >>= 1; break; } } for (++i; i < n; ++i) cin >> in; cout << (ans ? "EASY" : "HARD") << "\n"; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>In Search of an Easy Problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>When preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked $$$n$$$ people about their opinions. Each person answered whether this problem is easy or hard. If at least one of these $$$n$$$ people has answered that the problem is hard, the coordinator decides to change the problem. For the given responses, check if the problem is easy enough.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of people who were asked to give their opinions. The second line contains $$$n$$$ integers, each integer is either $$$0$$$ or $$$1$$$. If $$$i$$$-th integer is $$$0$$$, then $$$i$$$-th person thinks that the problem is easy; if it is $$$1$$$, then $$$i$$$-th person thinks that the problem is hard.</div><h2>Output Format</h2><div>Print one word: "EASY" if the problem is easy according to all responses, or "HARD" if there is at least one person who thinks the problem is hard. You may print every letter in any register: "EASY", "easy", "EaSY" and "eAsY" all will be processed correctly.</div><h2>Example</h2><h3>Input</h3><div>3 0 0 1</div><h3>Output</h3><div>HARD</div><h2>Example</h2><h3>Input</h3><div>1 0</div><h3>Output</h3><div>EASY</div><h2>Note</h2><div>In the first example the third person says it's a hard problem, so it should be replaced. In the second example the problem easy for the only person, so it doesn't have to be replaced.</div><h2>Editorial</h2><div>Codebait. Comforting problem. Find $$$\max\limits_{i=1..n}{(\text{answer}_i)}$$$, if it's equal to $$$1$$$ then answer is HARD, otherwise — EASY.</div></body></html>
#include <bits/stdc++.h> using namespace std; int main() { int i,q,p,a; cin >>p; a=0; for (i=0; i<p; i++){ cin >>q; if (q==1){ a=1; break; } } if (a==1) cout<<"HARD"; else cout<<"EASY"; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Personalized Cup</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>At many competitions that have a word «cup» in its official name the winner is presented with an actual cup. This time the organizers of one unusual programming competition have decided to please the winner even more and to add a nameplate to the cup with the handle of the winner. The nameplate is to be rectangular and the text on it will be printed as a table of several rows and columns. Having some measurements done, the organizers have found out that the number $$$a$$$ of rows cannot be greater than $$$5$$$ while the number $$$b$$$ of columns cannot exceed $$$20$$$. Every cell of the table will contain either an asterisk («*») or a letter of user's handle. Furthermore, the organizers want the rows of the table to be uniform, which means that the number of asterisks used in different rows should differ by at most one (i.e. you can't have two asterisks in the first row and none in the second). The main goal, however, is to obtain the winner's handle precisely when reading the table from top to bottom and from left to right in every row (skipping asterisks). The organizers want for the nameplate to have as few rows as possible and among all valid tables with the minimum number of rows they want to choose the one that has the minimum number of columns. The winner is not yet determined so your task is to write a program that, given a certain handle, generates the necessary table.</div><h2>Input Format</h2><div>The only line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$), comprised of uppercase and lowercase Latin letters,  — the handle of the winner.</div><h2>Output Format</h2><div>In the first line output the minimum number $$$a$$$ of rows in the table and the minimum number $$$b$$$ of columns in an optimal table with rows. The following $$$a$$$ lines should contain $$$b$$$ characters each  — any valid table.</div><h2>Example</h2><h3>Input</h3><div>tourist</div><h3>Output</h3><div>1 7 tourist</div><h2>Example</h2><h3>Input</h3><div>MyNameIsLifeIAmForeverByYourSideMyNameIsLife</div><h3>Output</h3><div>3 15 MyNameIsLifeIAm ForeverByYourSi deMyNameIsL*ife</div><h2>Editorial</h2><div>Let's iterate over all possible pairs $$$(a, b)$$$ with $$$1 \leq a \leq 5$$$ and $$$1 \leq b \leq 20$$$ to find the optimal one satisfying the inequality $$$a \cdot b \geq |s|$$$. So now we need to place the characters of $$$s$$$ in the same relative order through the table. Possibly, several cells will remain unused, but we will distribute them over the table and place at most one asterisk in every row. One can show that we will have at most one asterisk in each row, because otherwise we would have been able to reduce the value of $$$b$$$.</div></body></html>
#include <iostream> using namespace std; int main() { int n,k,a[5]={0},sm=0,mx=0; string s,r[5]; cin>>s; n=s.size(); k=n/20; if(n%20)k++; for(int i=0;i<k;i++){ a[i]=n/k; sm+=a[i]; } mx=n/k; int i=0; while(sm<n){ sm++; a[i]++; if (mx<a[i])mx=a[i]; i++; } int p=0; for (i=0;i<k;i++){ for(int t=0;t<a[i];t++) r[i]+=s[t+p]; p+=a[i]; if (mx>a[i])r[i]+="*"; } cout<<k<<" "<<mx<<endl; for (i=0;i<k;i++)cout<<r[i]<<endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Non-Coprime Partition</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that: $$$\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1$$$ Here $$$\mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$\mathrm{gcd}$$$ means thegreatest common divisor. Every integer number from $$$1$$$ to $$$n$$$ should be present in exactly one of $$$S_1$$$ or $$$S_2$$$.</div><h2>Input Format</h2><div>The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 45\,000$$$)</div><h2>Output Format</h2><div>If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them.</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>No</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>Yes 1 2 2 1 3</div><h2>Note</h2><div>In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No". In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$\mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers.</div><h2>Editorial</h2><div>There are many ways to solve this question. The easiest way perhaps was to note that the sum of first $$$n$$$ numbers is given by $$$\frac{n*(n+1) }{2}$$$, and one of $$$\frac{n}{2}$$$ or $$$\frac{n+1}{2}$$$ has to be an integer, suppose $$$k$$$. Then we can partition the numbers into two sets, one containing $$$k$$$ and the other containing the remaining integers, both of which will have $$$k$$$ as a common factor. Special Case: There is no answer for $$$n \le 2$$$ Overall Complexity: $$$O(n)$$$</div></body></html>
#include <iostream> #include <cstdio> #include <string> using namespace std; int min(int a, int b) { if (a < b) return a; return b; } int main() { int n, k = -1; cin >> n; if (n <= 2) { cout << "No"; return 0; } if (n % 2 == 0) { cout << "Yes" << endl; cout << "2 1 " << n << endl; cout << n - 2; for (int i = 2; i < n; i++) cout << " " << i; cout << endl; return 0; } k = (n + 1) / 2; cout << "Yes" << endl; cout << "1 " << k << endl; cout << n - 1; for (int i = 1; i <= n; i++) if (i != k) cout << " " << i; cout << endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Non-Coprime Partition</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that: $$$\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1$$$ Here $$$\mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$\mathrm{gcd}$$$ means thegreatest common divisor. Every integer number from $$$1$$$ to $$$n$$$ should be present in exactly one of $$$S_1$$$ or $$$S_2$$$.</div><h2>Input Format</h2><div>The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 45\,000$$$)</div><h2>Output Format</h2><div>If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them.</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>No</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>Yes 1 2 2 1 3</div><h2>Note</h2><div>In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No". In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$\mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers.</div><h2>Editorial</h2><div>There are many ways to solve this question. The easiest way perhaps was to note that the sum of first $$$n$$$ numbers is given by $$$\frac{n*(n+1) }{2}$$$, and one of $$$\frac{n}{2}$$$ or $$$\frac{n+1}{2}$$$ has to be an integer, suppose $$$k$$$. Then we can partition the numbers into two sets, one containing $$$k$$$ and the other containing the remaining integers, both of which will have $$$k$$$ as a common factor. Special Case: There is no answer for $$$n \le 2$$$ Overall Complexity: $$$O(n)$$$</div></body></html>
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define ii pair<int, int> #define vi vector<int> #define vii vector<ii> #define inf 0x3f3f3f3f using namespace std; #define MAXN 45001 int n; int gcd(int a, int b) { if(a == b) return a; if(a>b) return gcd(a-b, b); return gcd(a, b-a); } int main() { ios::sync_with_stdio(false); cin >> n; if(n == 1 || n == 2) { cout << "No" << endl; return 0; //break; } cout << "Yes" << endl; cout << n-2 << " "; for(int i=2; i<n; i++) cout << i << " "; cout << endl; cout << 2 << " 1 " << n << endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Non-Coprime Partition</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that: $$$\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1$$$ Here $$$\mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$\mathrm{gcd}$$$ means thegreatest common divisor. Every integer number from $$$1$$$ to $$$n$$$ should be present in exactly one of $$$S_1$$$ or $$$S_2$$$.</div><h2>Input Format</h2><div>The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 45\,000$$$)</div><h2>Output Format</h2><div>If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them.</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>No</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>Yes 1 2 2 1 3</div><h2>Note</h2><div>In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No". In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$\mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers.</div><h2>Editorial</h2><div>There are many ways to solve this question. The easiest way perhaps was to note that the sum of first $$$n$$$ numbers is given by $$$\frac{n*(n+1) }{2}$$$, and one of $$$\frac{n}{2}$$$ or $$$\frac{n+1}{2}$$$ has to be an integer, suppose $$$k$$$. Then we can partition the numbers into two sets, one containing $$$k$$$ and the other containing the remaining integers, both of which will have $$$k$$$ as a common factor. Special Case: There is no answer for $$$n \le 2$$$ Overall Complexity: $$$O(n)$$$</div></body></html>
#include <iostream> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b%a, a); } int main() { long long n, s1=0, s2=0, num=0; cin >> n; for (int i = 1; i <= n; i++) { if (i % 2 == 0) s2 += i; else { s1 += i; } } if (gcd(s1, s2) > 1) { cout << "Yes\n"; if (n % 2 == 0) { cout << n / 2 << " "; for (int i = 1; i <= n; i++) { if (i %2 == 1) cout << i << " "; } cout << '\n'; cout << n / 2 << " "; for (int i = 1; i <= n; i++) { if (i % 2 == 0) cout << i << " "; } } else { cout << n / 2 << " "; for (int i = 1; i <= n; i++) { if (i % 2 == 0) cout << i << " "; } cout << '\n'; cout << n / 2 + 1 << " "; for (int i = 1; i <= n; i++) { if (i % 2 == 1) cout << i << " "; } } } else cout << "No"; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Non-Coprime Partition</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that: $$$\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1$$$ Here $$$\mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$\mathrm{gcd}$$$ means thegreatest common divisor. Every integer number from $$$1$$$ to $$$n$$$ should be present in exactly one of $$$S_1$$$ or $$$S_2$$$.</div><h2>Input Format</h2><div>The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 45\,000$$$)</div><h2>Output Format</h2><div>If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them.</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>No</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>Yes 1 2 2 1 3</div><h2>Note</h2><div>In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No". In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$\mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers.</div><h2>Editorial</h2><div>There are many ways to solve this question. The easiest way perhaps was to note that the sum of first $$$n$$$ numbers is given by $$$\frac{n*(n+1) }{2}$$$, and one of $$$\frac{n}{2}$$$ or $$$\frac{n+1}{2}$$$ has to be an integer, suppose $$$k$$$. Then we can partition the numbers into two sets, one containing $$$k$$$ and the other containing the remaining integers, both of which will have $$$k$$$ as a common factor. Special Case: There is no answer for $$$n \le 2$$$ Overall Complexity: $$$O(n)$$$</div></body></html>
#include <bits/stdc++.h> using namespace std; #define INF (int)1e9 #define pb push_back #define mp make_pair #define st first #define nd second #define girdi freopen("in.gir", "r", stdin); #define cikti freopen("out.cik", "w", stdout); typedef pair < int, int > ii; typedef long long lint; int main() { //girdi cikti int n; cin >> n; if(n <= 2) cout << "No\n"; else if(n%2){ cout << "Yes\n"; cout << "1 "<< (n+1)/2 << endl; cout << n-1 << " "; for(int i = 1; i < n; i++) if(i != (n+1)/2) cout << i << " "; cout << n; } else{ cout << "Yes\n"; cout << n/2 << " "; for(int i = 1; i <= n; i+=2) cout << i << " "; cout << endl << n/2 << " "; for(int i = 2; i < n; i+=2) cout << i << " "; cout << n; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Timetable</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>There are two bus stops denoted A and B, and there $$$n$$$ buses that go from A to B every day. The shortest path from A to B takes $$$t$$$ units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route. At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as $$$a_1 < a_2 < \ldots < a_n$$$ for stop A and as $$$b_1 < b_2 < \ldots < b_n$$$ for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least $$$t$$$ units of time later than departs. It is known that for an order to be valid the latest possible arrival for the bus that departs at $$$a_i$$$ is $$$b_{x_i}$$$, i.e. $$$x_i$$$-th in the timetable. In other words, for each $$$i$$$ there exists such a valid order of arrivals that the bus departed $$$i$$$-th arrives $$$x_i$$$-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the $$$i$$$-th departed bus arrives $$$(x_i + 1)$$$-th. Formally, let's call a permutation $$$p_1, p_2, \ldots, p_n$$$ valid, if $$$b_{p_i} \ge a_i + t$$$ for all $$$i$$$. Then $$$x_i$$$ is the maximum value of $$$p_i$$$ among all valid permutations. You are given the sequences $$$a_1, a_2, \ldots, a_n$$$ and $$$x_1, x_2, \ldots, x_n$$$, but not the arrival timetable. Find out any suitable timetable for stop B $$$b_1, b_2, \ldots, b_n$$$ or determine that there is no such timetable.</div><h2>Input Format</h2><div>The first line of the input contains two integers $$$n$$$ and $$$t$$$ ($$$1 \leq n \leq 200\,000$$$, $$$1 \leq t \leq 10^{18}$$$) — the number of buses in timetable for and the minimum possible travel time from stop A to stop B. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 < a_2 < \ldots < a_n \leq 10^{18}$$$), defining the moments of time when the buses leave stop A. The third line contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_i \leq n$$$), the $$$i$$$-th of them stands for the maximum possible timetable position, at which the $$$i$$$-th bus leaving stop A can arrive at stop B.</div><h2>Output Format</h2><div>If a solution exists, print "Yes" (without quotes) in the first line of the output. In the second line print $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 < b_2 < \ldots < b_n \leq 3 \cdot 10^{18}$$$). We can show that if there exists any solution, there exists a solution that satisfies such constraints on $$$b_i$$$. If there are multiple valid answers you can print any of them. If there is no valid timetable, print "No" (without quotes) in the only line of the output.</div><h2>Example</h2><h3>Input</h3><div>3 10 4 6 8 2 2 3</div><h3>Output</h3><div>Yes 16 17 21</div><h2>Example</h2><h3>Input</h3><div>2 1 1 2 2 1</div><h3>Output</h3><div>No</div><h2>Note</h2><div>Consider the first example and the timetable $$$b_1, b_2, \ldots, b_n$$$ from the output. To get $$$x_1 = 2$$$ the buses can arrive in the order $$$(2, 1, 3)$$$. To get $$$x_2 = 2$$$ and $$$x_3 = 3$$$ the buses can arrive in the order $$$(1, 2, 3)$$$. $$$x_1$$$ is not $$$3$$$, because the permutations $$$(3, 1, 2)$$$ and $$$(3, 2, 1)$$$ (all in which the $$$1$$$-st bus arrives $$$3$$$-rd) are not valid (sube buses arrive too early), $$$x_2$$$ is not $$$3$$$ because of similar reasons.</div><h2>Editorial</h2><div>If there is at least one valid ordering $$$p$$$'s (and it surely exists since the $$$x$$$ is defined), then the ordering $$$p_i = i$$$ is also valid. Hence if for some $$$i$$$ doesn't hold $$$x_i \ge i$$$ then the answer is no. Also, from this follows that $$$b_i \ge a_i + t$$$. Otherwise, what it means that $$$x_i = c$$$? It means that there is an ordering $$$p$$$, in which the $$$i$$$-th bus comes as $$$c$$$, where the other buses will come then? It turns out, that the least restricting way to complete the ordering is following: $$$i \to c$$$, $$$i + 1 \to i$$$, $$$i + 2 \to i$$$, ..., $$$c \to c - 1$$$. Note that since ordering $$$p_i = i$$$, it is also allowed for $$$i$$$ to go to $$$c$$$ (it wouldn't be too fast), but we can doubt whether $$$i + 1 \to i$$$, $$$i + 2 \to i$$$ and etc are good. More over, since $$$x_i = c$$$ (not, say, $$$c + 1$$$), it must hold that $$$i + 1 \to i$$$, $$$i + 2 \to i$$$, ..., $$$c \to c - 1$$$ are "good" (not fast enough), but doesn't hold $$$c + 1 \to c$$$ (too fast). So for each $$$i$$$ we can use scanline to calculate whether it is good or not. And then we can restore $$$b$$$'s in negative order. What conditions must hold on $$$b$$$? $$$b_i \ge a_i + i$$$, and depending on whether some $$$i$$$ is good or not $$$b_i \ge a_{i + 1} + t$$$ or $$$b_i < a_{i + 1} + t$$$. We can go in reverse order and select the value of $$$b_i$$$ on the basis of the cases above. Also, since $$$b_i < b_{i + 1}$$$ if there are many options for $$$b_i$$$ it is best to select the largest of them.</div></body></html>
#include<bits/stdc++.h> using namespace std; #define ll long long #define MAXN 200005 int n,i,x[MAXN],c[MAXN],d[MAXN]; ll m,l,a[MAXN],b[MAXN]; int main() { scanf("%d%I64d",&n,&m); for(i=1;i<=n;i++)scanf("%I64d",a+i); for(i=1;i<=n;i++) { scanf("%d",x+i); if(x[i]<x[i-1]||x[i]<i) { puts("No"); return 0; } c[i]++; c[x[i]]--; d[x[i]]=1; } for(i=1;i<=n;i++) { c[i]+=c[i-1]; l=max(b[i-1]+1,a[i]+m); if(c[i])l=max(l,a[i+1]+m); if(d[i]&&i<n&&l>=a[i+1]+m) { puts("No"); return 0; } b[i]=l; } puts("Yes"); for(i=1;i<=n;i++)printf("%I64d ",b[i]); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Shashlik Cooking</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over. This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again). As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$. Help Miroslav turn over all $$$n$$$ skewers.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 1000$$$, $$$0 \leq k \leq 1000$$$) — the number of skewers and the number of skewers from each side that are turned in one step.</div><h2>Output Format</h2><div>The first line should contain integer $$$l$$$ — the minimum number of actions needed by Miroslav to turn over all $$$n$$$ skewers. After than print $$$l$$$ integers from $$$1$$$ to $$$n$$$ denoting the number of the skewer that is to be turned over at the corresponding step.</div><h2>Example</h2><h3>Input</h3><div>7 2</div><h3>Output</h3><div>2 1 6</div><h2>Example</h2><h3>Input</h3><div>5 1</div><h3>Output</h3><div>2 1 4</div><h2>Note</h2><div>In the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$. In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations.</div><h2>Editorial</h2><div>The funny thing about this problem that it is entirely based on real facts, in the real life the $$$k$$$ was equal to $$$1$$$ and one skewer really turned two more. So it is easy to see, that answer is at least $$$\lceil \frac{n}{2k + 1} \rceil$$$ (because in smaller number of operations we wouldn't simply able to touch all skewers), where the $$$\lceil a \rceil$$$ denotes rounding up. Set's build an answer with exactly this number of operations. We will make our answer in such way, that each skewer belongs exactly to one operation. That is, we need to put segments of length $$$2k + 1$$$ over the array, such that the ends are touching and that the first and last segments don't pop out of the array too much. Let's define $$$x = \lceil \frac{n}{2k + 1} \rceil (2k + 1) - n$$$, $$$a = min(n, k)$$$, $$$b = x - a$$$. Note that $$$0 \le a, b \le k$$$ and $$$a + b = x$$$. Let's make segments described above in such way, that the first segments outweighs over the array exactly by $$$a$$$ and the last one exactly by $$$b$$$. Since $$$a, b \le k$$$ it follows, that the centre of this segments stays inside the array. Some example: $$$n = 7$$$, $$$k = 2$$$, $$$x = 3$$$, $$$a = 2$$$, $$$b = 1$$$ The answer is then as in follows: [#|#|@|@|@] [@|@|@|@|#], where @ denotes skewer, and one block of square brackets corresponds to one operation.</div></body></html>
#include <bits/stdc++.h> using namespace std; vector<int >v; int n,k; void find(int i){ int be=1; i-=(k+1)*2; be+=min(i,k); v.push_back(be); be+=(2*k+1); //cout<<be<<endl; while(be<=n){ v.push_back(be); be+=(2*k+1); } cout<<v.size()<<endl; for(int i=0;i<(int)v.size();++i){ cout<<v[i]<<" "; } cout<<endl; } int main() { cin>>n>>k; if(n<(k+1)*2){ cout<<1<<endl; cout<<min(n,k+1)<<endl; return 0; } for(int i=(k+1)*2;i<=(2*k+1)*2;++i){ if((n-i)%(2*k+1)==0){ find(i); return 0; } } cout<<n<<endl; for(int i=1;i<=n;++i){ cout<<i<<" "; } cout<<endl; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Shashlik Cooking</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over. This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again). As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$. Help Miroslav turn over all $$$n$$$ skewers.</div><h2>Input Format</h2><div>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 1000$$$, $$$0 \leq k \leq 1000$$$) — the number of skewers and the number of skewers from each side that are turned in one step.</div><h2>Output Format</h2><div>The first line should contain integer $$$l$$$ — the minimum number of actions needed by Miroslav to turn over all $$$n$$$ skewers. After than print $$$l$$$ integers from $$$1$$$ to $$$n$$$ denoting the number of the skewer that is to be turned over at the corresponding step.</div><h2>Example</h2><h3>Input</h3><div>7 2</div><h3>Output</h3><div>2 1 6</div><h2>Example</h2><h3>Input</h3><div>5 1</div><h3>Output</h3><div>2 1 4</div><h2>Note</h2><div>In the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$. In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations.</div><h2>Editorial</h2><div>The funny thing about this problem that it is entirely based on real facts, in the real life the $$$k$$$ was equal to $$$1$$$ and one skewer really turned two more. So it is easy to see, that answer is at least $$$\lceil \frac{n}{2k + 1} \rceil$$$ (because in smaller number of operations we wouldn't simply able to touch all skewers), where the $$$\lceil a \rceil$$$ denotes rounding up. Set's build an answer with exactly this number of operations. We will make our answer in such way, that each skewer belongs exactly to one operation. That is, we need to put segments of length $$$2k + 1$$$ over the array, such that the ends are touching and that the first and last segments don't pop out of the array too much. Let's define $$$x = \lceil \frac{n}{2k + 1} \rceil (2k + 1) - n$$$, $$$a = min(n, k)$$$, $$$b = x - a$$$. Note that $$$0 \le a, b \le k$$$ and $$$a + b = x$$$. Let's make segments described above in such way, that the first segments outweighs over the array exactly by $$$a$$$ and the last one exactly by $$$b$$$. Since $$$a, b \le k$$$ it follows, that the centre of this segments stays inside the array. Some example: $$$n = 7$$$, $$$k = 2$$$, $$$x = 3$$$, $$$a = 2$$$, $$$b = 1$$$ The answer is then as in follows: [#|#|@|@|@] [@|@|@|@|#], where @ denotes skewer, and one block of square brackets corresponds to one operation.</div></body></html>
#include<bits/stdc++.h> using namespace std; int main(){ int n,k; cin>>n>>k; if(n <= 2*k+1){ cout<<1<<endl; cout<<(n/2)+1<<endl; exit(0); } int fp = k+1; int rem = n%(2*k+1); // cout<<"fp:"<<fp<<endl; // cout<<"rem:"<<rem<<endl; int diff = 0; if(rem > 0 && rem < k+1){ diff = (k+1)-rem; } fp -= diff; // cout<<"diff"<<diff<<" fp:"<<fp<<endl; vector<int> ans; while(fp <= n){ ans.push_back(fp); fp += 2*k+1; } cout<<ans.size()<<endl; for(auto it:ans){ cout<<it<<" "; } cout<<endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Coffee Break</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Recently Monocarp got a job. His working day lasts exactly $$$m$$$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $$$n$$$ minutes $$$a_1, a_2, \dots, a_n$$$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $$$a_i$$$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $$$d$$$ minutes pass between any two coffee breaks. Monocarp also wants to take these $$$n$$$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $$$d$$$ minutes pass between the end of any working day and the start of the following working day. For each of the $$$n$$$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.</div><h2>Input Format</h2><div>The first line contains three integers $$$n$$$, $$$m$$$, $$$d$$$ $$$(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$$$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le m)$$$, where $$$a_i$$$ is some minute when Monocarp wants to have a coffee break.</div><h2>Output Format</h2><div>In the first line, write the minimum number of days required to make a coffee break in each of the $$$n$$$ given minutes. In the second line, print $$$n$$$ space separated integers. The $$$i$$$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $$$a_i$$$. Days are numbered from $$$1$$$. If there are multiple optimal solutions, you may print any of them.</div><h2>Example</h2><h3>Input</h3><div>4 5 3 3 5 1 2</div><h3>Output</h3><div>3 3 1 1 2</div><h2>Example</h2><h3>Input</h3><div>10 10 1 10 5 7 4 6 3 2 1 9 8</div><h3>Output</h3><div>2 2 1 1 2 2 1 2 1 1 2</div><h2>Note</h2><div>In the first example, Monocarp can take two coffee breaks during the first day (during minutes $$$1$$$ and $$$5$$$, $$$3$$$ minutes will pass between these breaks). One break during the second day (at minute $$$2$$$), and one break during the third day (at minute $$$3$$$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.</div><h2>Editorial</h2><div>Let put in set the pairs in the following format: $$$a_i$$$ — the time for $$$i$$$-th break and the number of this break in the input data. So, we got pairs sorted by $$$a_i$$$. While set contains elements we will determine the breaks, which should be done in a single day. For the next day the first break should be dine in the time, which is at the beginning of the set (let this time is $$$x$$$). So, next break should be done after at least $$$d$$$ minutes. We should find a pair in the set, where the first element not less than $$$x + d + 1$$$. It can be done with $$$lower\_bound$$$. In the same way, we can find the next breaks in this day. With help of the second elements of pairs, we can easily remember the answer days for breaks. Also, we should erase from set all considered pairs. If for some pair we cannot find the needed element, we should go to the next day.</div></body></html>
#include<bits/stdc++.h> using namespace std; #define pii pair<int,int> #define mp make_pair #define pb push_back #define lg long long #define db double #define lb(x) ((x)&-(x)) #define ft first #define sd second template <class _T_> void read(_T_& d){ d=0;int f=1;char c=getchar(); for(;c<'0'||c>'9';c=getchar())if(c=='-')f*=-1; for(;c>='0'&&c<='9';c=getchar())d=d*10+c-'0'; d*=f; } /************************************************/ #define MN 200005 int n,m,k; pii a[MN]; int R[MN]; int main(){ read(n);read(m);read(k);++k; for(int i=1;i<=n;++i)read(a[i].ft),a[i].sd=i; sort(a+1,a+n+1);int res=0; for(int i=1,j=1;i<=n;++i){ while(j<=n&&a[j].ft-a[i].ft<k)++j;--j; res=max(res,j-i+1); } printf("%d\n",res); for(int i=1;i<=n;++i)R[a[i].sd]=R[a[i-1].sd]%res+1; for(int i=1;i<=n;++i)printf("%d ",R[i]); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Tree Reconstruction</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $$$1$$$ to $$$n$$$. For every edge $$$e$$$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $$$e$$$ (and only this edge) is erased from the tree. Monocarp has given you a list of $$$n - 1$$$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.</div><h2>Input Format</h2><div>The first line contains one integer $$$n$$$ ($$$2 \le n \le 1\,000$$$) — the number of vertices in the tree. Each of the next $$$n-1$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ each ($$$1 \le a_i < b_i \le n$$$) — the maximal indices of vertices in the components formed if the $$$i$$$-th edge is removed.</div><h2>Output Format</h2><div>If there is no such tree that can produce the given list of pairs, print "NO" (without quotes). Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $$$n - 1$$$ lines. Each of the last $$$n - 1$$$ lines should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$) — vertices connected by an edge. Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.</div><h2>Example</h2><h3>Input</h3><div>4 3 4 1 4 3 4</div><h3>Output</h3><div>YES 1 3 3 2 2 4</div><h2>Example</h2><h3>Input</h3><div>3 1 3 1 3</div><h3>Output</h3><div>NO</div><h2>Example</h2><h3>Input</h3><div>3 1 2 2 3</div><h3>Output</h3><div>NO</div><h2>Note</h2><div>Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs.</div><h2>Editorial</h2><div>First of all, if there exists some $$$b_i < n$$$, then the answer is clearly NO. Then let's consider that every $$$b_i = n$$$ and analyze only the values of $$$a_i$$$ (and furthermore, let's sort all values of $$$a_i$$$ beforehand). Suppose that we have constructed a tree satisfying all the requirements and rooted it at vertex $$$n$$$. Then for any $$$k$$$ such that $$$1 \le k \le n - 1$$$ there exist no more than $$$k$$$ subtrees containing only vertices with indices not exceeding $$$k$$$ (because there are no more than $$$k$$$ vertices that can be the roots of such subtrees). So, if for some constant $$$k$$$ the number of $$$a_i \le k$$$ is greater than $$$k$$$, then the answer is NO since it would imply that there are more than $$$k$$$ subtrees containing only values not greater then $$$k$$$. Now we consider only the case such that for every $$$k \in [1, n - 1]$$$ the number of $$$i$$$ such that $$$a_i \le k$$$ is not greater than $$$k$$$. In this case the answer is YES; let's prove it with an algorithm that constructs a tree meeting the constraints. Actually, we can always build a bamboo (a tree where no vertex has degree greater than $$$2$$$, or simply a path) according to these constraints. Let's put vertex $$$n$$$ at one of the ends of the bamboo and start building a bamboo from the other end. It's obvious that if we make some vertex $$$x$$$ a leaf, then the array $$$a$$$ will contain only values not less than $$$x$$$. So, if we consider values of $$$a_i$$$ to be sorted, then the leaf has index $$$a_1$$$. Then let's repeat the following process for every $$$i \in [2, n - 1]$$$: • if $$$a_i > a_{i - 1}$$$, then let's use the vertex with index $$$a_i$$$ as the parent of the previous vertex; • otherwise, let's find any index $$$j$$$ such that $$$j < a_i$$$ and index $$$j$$$ is not used yet, and use vertex with index $$$j$$$ as the parent of the previous vertex (there will be at least one such vertex since for every $$$k \in [1, n - 1]$$$ the number of $$$i$$$ such that $$$a_i \le k$$$ is not greater than $$$k$$$). It's easy to prove that the bamboo we construct in such a way meets the constraints given in the statement.</div></body></html>
#include<bits/stdc++.h> #define N 1007 using namespace std; int usd[N],last,a[N],b[N],n,t[N],p,ega[N],egb[N],tot; int get() { static int tog=1; while (usd[tog]) tog++; return usd[tog]=1,tog; } signed main () { scanf("%d",&n); for (int i=1;i<n;i++) { scanf("%d%d",a+i,b+i); if (a[i]>b[i]) swap(a[i],b[i]); if (b[i]!=n) { puts("NO"); return 0 ; } t[a[i]]++; usd[a[i]]=1; } for (int i=1;i<n;i++) { if (!t[i]) continue; last=n; usd[i]=1; for (int j=1;j<t[i];j++) { p=get(); if (p>i) { puts("NO"); return 0; } ega[++tot]=last; egb[tot]=p; last=p; } ega[++tot]=last; egb[tot]=i; // printf("%d %d\n",last,i); } puts("YES"); for (int i=1;i<n;i++) { printf("%d %d\n",ega[i],egb[i]); } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Smallest Word</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: - $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$; - in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.</div><h2>Input Format</h2><div>The first and the only line contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$), describing the initial string formed by magnets. The string $$$s$$$ consists only of characters 'a' and 'b'.</div><h2>Output Format</h2><div>Output exactly $$$|s|$$$ integers. If IA should reverse the $$$i$$$-th prefix (that is, the substring from $$$1$$$ to $$$i$$$), the $$$i$$$-th integer should be equal to $$$1$$$, and it should be equal to $$$0$$$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them.</div><h2>Example</h2><h3>Input</h3><div>bbab</div><h3>Output</h3><div>0 1 1 0</div><h2>Example</h2><h3>Input</h3><div>aaaaa</div><h3>Output</h3><div>1 0 0 0 1</div><h2>Note</h2><div>In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.</div></body></html>
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios::sync_with_stdio(false); ios_base::sync_with_stdio(false); string str; cin>>str; vector<ll> v(str.length(),0); v[0] = 0; for(ll i=1;i<str.length();i++) { if(i==str.length()-1 && str[i]=='a') { v[i] = 1; for(ll j=0;j<(i/2);j++) { swap(str[j],str[i-j]); } } else if(str[i]!=str[i+1] && i!=str.length()-1) { v[i] = 1; for(ll j=0;j<(i/2);j++) { swap(str[j],str[i-j]); } } else { v[i] = 0; } // cout<<str<<endl; } for(ll i=0;i<str.length();i++) { cout<<v[i]<<" "; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Smallest Word</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: - $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$; - in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.</div><h2>Input Format</h2><div>The first and the only line contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$), describing the initial string formed by magnets. The string $$$s$$$ consists only of characters 'a' and 'b'.</div><h2>Output Format</h2><div>Output exactly $$$|s|$$$ integers. If IA should reverse the $$$i$$$-th prefix (that is, the substring from $$$1$$$ to $$$i$$$), the $$$i$$$-th integer should be equal to $$$1$$$, and it should be equal to $$$0$$$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them.</div><h2>Example</h2><h3>Input</h3><div>bbab</div><h3>Output</h3><div>0 1 1 0</div><h2>Example</h2><h3>Input</h3><div>aaaaa</div><h3>Output</h3><div>1 0 0 0 1</div><h2>Note</h2><div>In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.</div></body></html>
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<int> a(s.size(), 0); vector<pair<char, int>> t; s.push_back('!'); for (int i = 1; i < s.size(); ++i) { if (s[i] != s[i - 1]) { t.push_back({s[i - 1], i - 1}); } } for (int i = 1; i < t.size(); ++i) { if (t[i - 1].first == 'b' && t[i].first == 'a') { a[t[i - 1].second] = 1; a[t[i].second] = 1; } } for (auto i: a) { cout << i << ' '; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Smallest Word</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: - $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$; - in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.</div><h2>Input Format</h2><div>The first and the only line contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$), describing the initial string formed by magnets. The string $$$s$$$ consists only of characters 'a' and 'b'.</div><h2>Output Format</h2><div>Output exactly $$$|s|$$$ integers. If IA should reverse the $$$i$$$-th prefix (that is, the substring from $$$1$$$ to $$$i$$$), the $$$i$$$-th integer should be equal to $$$1$$$, and it should be equal to $$$0$$$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them.</div><h2>Example</h2><h3>Input</h3><div>bbab</div><h3>Output</h3><div>0 1 1 0</div><h2>Example</h2><h3>Input</h3><div>aaaaa</div><h3>Output</h3><div>1 0 0 0 1</div><h2>Note</h2><div>In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.</div></body></html>
#include <bits/stdc++.h> using namespace std; const int N=1000; char s[N+1]; bool ans[N]; int main(){ scanf("%s",&s); int n=strlen(s); for(int i=n-1;i>=0;i--){ if(s[i]=='a'){ ans[i]=!ans[i]; if(i>0)ans[i-1]=true; } } for(int i=0;i<n;i++)printf("%d ",ans[i]); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Smallest Word</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: - $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$; - in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.</div><h2>Input Format</h2><div>The first and the only line contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$), describing the initial string formed by magnets. The string $$$s$$$ consists only of characters 'a' and 'b'.</div><h2>Output Format</h2><div>Output exactly $$$|s|$$$ integers. If IA should reverse the $$$i$$$-th prefix (that is, the substring from $$$1$$$ to $$$i$$$), the $$$i$$$-th integer should be equal to $$$1$$$, and it should be equal to $$$0$$$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them.</div><h2>Example</h2><h3>Input</h3><div>bbab</div><h3>Output</h3><div>0 1 1 0</div><h2>Example</h2><h3>Input</h3><div>aaaaa</div><h3>Output</h3><div>1 0 0 0 1</div><h2>Note</h2><div>In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.</div></body></html>
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; int len = s.length(); for (int i = 0; i < len; i++) { int should = 0; if (i + 1 < len) { if (s[i] != s[i + 1]) { reverse(s.begin(), s.begin() + i + 1); should = 1; } } else { if (s[i] == 'a') { reverse(s.begin(), s.begin() + i + 1); should = 1; } } cout << should << " "; } } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); solve(); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Smallest Word</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: - $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$; - in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.</div><h2>Input Format</h2><div>The first and the only line contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$), describing the initial string formed by magnets. The string $$$s$$$ consists only of characters 'a' and 'b'.</div><h2>Output Format</h2><div>Output exactly $$$|s|$$$ integers. If IA should reverse the $$$i$$$-th prefix (that is, the substring from $$$1$$$ to $$$i$$$), the $$$i$$$-th integer should be equal to $$$1$$$, and it should be equal to $$$0$$$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them.</div><h2>Example</h2><h3>Input</h3><div>bbab</div><h3>Output</h3><div>0 1 1 0</div><h2>Example</h2><h3>Input</h3><div>aaaaa</div><h3>Output</h3><div>1 0 0 0 1</div><h2>Note</h2><div>In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.</div></body></html>
#include <bits/stdc++.h> using namespace std; #define F first #define S second typedef long long ll; typedef long double ld; typedef pair <ll, ll> pll; ifstream in; ofstream out; const long long kk=1000; const long long ml=kk*kk; const long long mod=ml*kk+7; const long long inf=ml*ml*ml+7; ll n,i,j; vector <ll> m; bool viv=false; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin>>s; n=s.size(); vector <ll> m(n, 0); for (i=0; i<n-1; i++) if (s[i]!=s[i+1]) m[i]=1; if (s.back()=='a') m.back()=1; for (auto i:m) cout<<i<<' '; cout<<endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Little C Loves 3 I</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Little C loves number «3» very much. He loves all things about it. Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.</div><h2>Input Format</h2><div>A single line containing one integer $$$n$$$ ($$$3 \leq n \leq 10^9$$$) — the integer Little C has.</div><h2>Output Format</h2><div>Print $$$3$$$ positive integers $$$a,b,c$$$ in a single line, such that $$$a+b+c=n$$$ and none of them is a multiple of $$$3$$$. It can be proved that there is at least one solution. If there are multiple solutions, print any of them.</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>1 1 1</div><h2>Example</h2><h3>Input</h3><div>233</div><h3>Output</h3><div>77 77 79</div><h2>Editorial</h2><div>If $$$n-2$$$ is not a multiple of $$$3$$$, $$$a=1,b=1,c=n-2$$$ is OK. Otherwise, $$$a=1,b=2,c=n-3$$$ is OK.</div></body></html>
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i, j, k; cin >> n; int d = n / 3, r = n % 3; int a = d, b = d, c = d + r; if (a % 3 == 0 && c % 3 != 0) { a += 1; b -= 1; } else if (a % 3 == 0 && c % 3 == 0) { a -= 1; b -= 1; c += 2; } else if (a % 3 != 0 && c % 3 == 0) { if ((a + 1) % 3 == 0) { a += 2; c -= 2; } else { a += 1; c -= 1; } } cout << a << " " << b << " " << c; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Little C Loves 3 I</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Little C loves number «3» very much. He loves all things about it. Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.</div><h2>Input Format</h2><div>A single line containing one integer $$$n$$$ ($$$3 \leq n \leq 10^9$$$) — the integer Little C has.</div><h2>Output Format</h2><div>Print $$$3$$$ positive integers $$$a,b,c$$$ in a single line, such that $$$a+b+c=n$$$ and none of them is a multiple of $$$3$$$. It can be proved that there is at least one solution. If there are multiple solutions, print any of them.</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>1 1 1</div><h2>Example</h2><h3>Input</h3><div>233</div><h3>Output</h3><div>77 77 79</div><h2>Editorial</h2><div>If $$$n-2$$$ is not a multiple of $$$3$$$, $$$a=1,b=1,c=n-2$$$ is OK. Otherwise, $$$a=1,b=2,c=n-3$$$ is OK.</div></body></html>
#include<bits/stdc++.h> #define io ios_base::sync_with_stdio(false) #define mp make_pair #define pb push_back using namespace std; #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } typedef long long int ll; typedef vector< int > vi; typedef vector< vi > vvi; typedef pair<int,int> ii; int a,b,c; int main() { io; int n,m; cin>>n; m=n; a=1; b=1; while(n>0) { if((n-a-b)%3==0 || a%3==0 || b%3==0) a++; //else if((n-a-b)%3==0 || a%3==0 || b%3==0) b++; else {c= n-a-b; break;} } cout<<a<<" "<<b<<" "<<c; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Little C Loves 3 I</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Little C loves number «3» very much. He loves all things about it. Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.</div><h2>Input Format</h2><div>A single line containing one integer $$$n$$$ ($$$3 \leq n \leq 10^9$$$) — the integer Little C has.</div><h2>Output Format</h2><div>Print $$$3$$$ positive integers $$$a,b,c$$$ in a single line, such that $$$a+b+c=n$$$ and none of them is a multiple of $$$3$$$. It can be proved that there is at least one solution. If there are multiple solutions, print any of them.</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>1 1 1</div><h2>Example</h2><h3>Input</h3><div>233</div><h3>Output</h3><div>77 77 79</div><h2>Editorial</h2><div>If $$$n-2$$$ is not a multiple of $$$3$$$, $$$a=1,b=1,c=n-2$$$ is OK. Otherwise, $$$a=1,b=2,c=n-3$$$ is OK.</div></body></html>
#include <bits/stdc++.h> using namespace std; int main(){ int n; scanf("%d" , &n); if(!(n % 3)) printf("1 1 %d" , n - 2); if(n % 3) printf("1 2 %d" , n - 3); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Little C Loves 3 I</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Little C loves number «3» very much. He loves all things about it. Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.</div><h2>Input Format</h2><div>A single line containing one integer $$$n$$$ ($$$3 \leq n \leq 10^9$$$) — the integer Little C has.</div><h2>Output Format</h2><div>Print $$$3$$$ positive integers $$$a,b,c$$$ in a single line, such that $$$a+b+c=n$$$ and none of them is a multiple of $$$3$$$. It can be proved that there is at least one solution. If there are multiple solutions, print any of them.</div><h2>Example</h2><h3>Input</h3><div>3</div><h3>Output</h3><div>1 1 1</div><h2>Example</h2><h3>Input</h3><div>233</div><h3>Output</h3><div>77 77 79</div><h2>Editorial</h2><div>If $$$n-2$$$ is not a multiple of $$$3$$$, $$$a=1,b=1,c=n-2$$$ is OK. Otherwise, $$$a=1,b=2,c=n-3$$$ is OK.</div></body></html>
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 1; typedef long long ll; ll n,m,k,a,b,c; int main() { cin >> n; a = n / 3; b = n / 3; c = n / 3 + n % 3; if (a % 3 == 0 and c % 3 == 0) { cout << a - 1 << " " << b - 1 << " " << c + 2 << endl; } else if (a % 3 == 0) { cout << a - 1 << " " << b + 1 << " " << c << endl; } else if (c % 3 == 0) { if (a % 3 == 1) cout << a << " " << b + 1 << " " << c - 1 << endl; else if (a % 3 == 2) cout << a << " " << b - 1 << " " << c + 1 << endl; } else { cout << a << " " << b << " " << c << endl; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Dark Assembly</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of n senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%. Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved. If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to A / (A + B), where A is the sum of levels of all player's characters and B is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved. Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than k sweets to the courtroom. Candies should be given to the senators before the start of voting. Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way.</div><h2>Input Format</h2><div>The first line contains three integers n, k and A (1 ≤ n, k ≤ 8, 1 ≤ A ≤ 9999). Then n lines follow. The i-th of them contains two numbers — bi and li — the i-th senator's level and his loyalty. The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10.</div><h2>Output Format</h2><div>Print one real number with precision 10 - 6 — the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators.</div><h2>Example</h2><h3>Input</h3><div>5 6 100 11 80 14 90 23 70 80 30 153 70</div><h3>Output</h3><div>1.0000000000</div><h2>Example</h2><h3>Input</h3><div>5 3 100 11 80 14 90 23 70 80 30 153 70</div><h3>Output</h3><div>0.9628442962</div><h2>Example</h2><h3>Input</h3><div>1 3 20 20 20</div><h3>Output</h3><div>0.7500000000</div><h2>Note</h2><div>In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes. It the second sample player should give all three candies to the fifth senator.</div></body></html>
#include<cstdio> #include<cstdlib> using namespace std; int n,m,x,i,a[8],b[8]; double r,res; void cnt(int l, int d, double s, double p) { if (l==n) { if (d*2>n) r+=p; else r+=(p*x)/(x+s);//killl return; } if (b[l]>0) cnt(l+1,d+1,s,p*0.1*b[l]);//大了10倍` if (b[l]<10) cnt(l+1,d,s+a[l],p*0.1*(10-b[l])); } void rec(int l, int c) { if (l==n) {//給完 r=0; cnt(0,0,0,1.0); if (r>res) res=r;//overall answer return; } for (int i=0; i<=c && b[l]+i<=10; i++) {//so must start from 0 b[l]+=i; rec(l+1,c-i); b[l]-=i;//c:remaining candies } } int main() { scanf("%d%d%d",&n,&m,&x); for (i=0; i<n; i++) { scanf("%d%d",&a[i],&b[i]); b[i]/=10; } rec(0,m); printf("%.8lf\n",res); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Dark Assembly</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of n senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%. Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved. If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to A / (A + B), where A is the sum of levels of all player's characters and B is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved. Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than k sweets to the courtroom. Candies should be given to the senators before the start of voting. Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way.</div><h2>Input Format</h2><div>The first line contains three integers n, k and A (1 ≤ n, k ≤ 8, 1 ≤ A ≤ 9999). Then n lines follow. The i-th of them contains two numbers — bi and li — the i-th senator's level and his loyalty. The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10.</div><h2>Output Format</h2><div>Print one real number with precision 10 - 6 — the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators.</div><h2>Example</h2><h3>Input</h3><div>5 6 100 11 80 14 90 23 70 80 30 153 70</div><h3>Output</h3><div>1.0000000000</div><h2>Example</h2><h3>Input</h3><div>5 3 100 11 80 14 90 23 70 80 30 153 70</div><h3>Output</h3><div>0.9628442962</div><h2>Example</h2><h3>Input</h3><div>1 3 20 20 20</div><h3>Output</h3><div>0.7500000000</div><h2>Note</h2><div>In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes. It the second sample player should give all three candies to the fifth senator.</div></body></html>
#include <stdio.h> int n,m,x,i,a[8],b[8]; double r,res; void cnt(int l, int d, double s, double p) { if (l==n) { if (d*2>n) r+=p; else r+=(p*x)/(x+s); return; } if (b[l]>0) cnt(l+1,d+1,s,p*0.1*b[l]); if (b[l]<10) cnt(l+1,d,s+a[l],p*0.1*(10-b[l])); } void rec(int l, int c) { if (l==n) { r=0; cnt(0,0,0,1.0); if (r>res) res=r; return; } for (int i=0; i<=c && b[l]+i<=10; i++) { b[l]+=i; rec(l+1,c-i); b[l]-=i; } } int main() { scanf("%d%d%d",&n,&m,&x); for (i=0; i<n; i++) { scanf("%d%d",&a[i],&b[i]); b[i]/=10; } rec(0,m); printf("%.8lf\n",res); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Dark Assembly</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of n senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%. Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved. If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to A / (A + B), where A is the sum of levels of all player's characters and B is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved. Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than k sweets to the courtroom. Candies should be given to the senators before the start of voting. Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way.</div><h2>Input Format</h2><div>The first line contains three integers n, k and A (1 ≤ n, k ≤ 8, 1 ≤ A ≤ 9999). Then n lines follow. The i-th of them contains two numbers — bi and li — the i-th senator's level and his loyalty. The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10.</div><h2>Output Format</h2><div>Print one real number with precision 10 - 6 — the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators.</div><h2>Example</h2><h3>Input</h3><div>5 6 100 11 80 14 90 23 70 80 30 153 70</div><h3>Output</h3><div>1.0000000000</div><h2>Example</h2><h3>Input</h3><div>5 3 100 11 80 14 90 23 70 80 30 153 70</div><h3>Output</h3><div>0.9628442962</div><h2>Example</h2><h3>Input</h3><div>1 3 20 20 20</div><h3>Output</h3><div>0.7500000000</div><h2>Note</h2><div>In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes. It the second sample player should give all three candies to the fifth senator.</div></body></html>
#include <stdio.h> #define min(a,b) ((a)<(b)?(a):(b)) int N,K,A,B,ps[9],L[9]; double ans,tar; void proc(int n,int c,int sum,double pos) { if (c > N/2){ tar += pos; return; } if (n > N){ tar += pos*double(A)/double(A+sum); return; } proc(n+1,c+1,sum,pos*(ps[n]/100.)); proc(n+1,c,sum+L[n],pos*(1-(ps[n]/100.))); } void dfs(int n,int left) { if (n == N){ int tmp=ps[n]; ps[n] = min(100,ps[n]+left*10); tar = 0; proc(1,0,0,1); if (ans < tar) ans = tar; ps[n] = tmp; return; } if (!left){ tar = 0; proc(1,0,0,1); if (ans < tar) ans = tar; return; } if (ps[n] < 100 && left > 0){ ps[n] += 10; dfs(n,left-1); ps[n] -= 10; } dfs(n+1,left); } int main() { int i; scanf("%d%d%d",&N,&K,&A); for (i=1;i<=N;i++) scanf("%d%d",L+i,ps+i), B += L[i]; dfs(1,K); printf("%.9lf",ans); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Vasya and Multisets</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Vasya has a multiset $$$s$$$ consisting of $$$n$$$ integer numbers. Vasya calls some number $$$x$$$ nice if it appears in the multiset exactly once. For example, multiset $$$\{1, 1, 2, 3, 3, 3, 4\}$$$ contains nice numbers $$$2$$$ and $$$4$$$. Vasya wants to split multiset $$$s$$$ into two multisets $$$a$$$ and $$$b$$$ (one of which may be empty) in such a way that the quantity of nice numbers in multiset $$$a$$$ would be the same as the quantity of nice numbers in multiset $$$b$$$ (the quantity of numbers to appear exactly once in multiset $$$a$$$ and the quantity of numbers to appear exactly once in multiset $$$b$$$).</div><h2>Input Format</h2><div>The first line contains a single integer $$$n~(2 \le n \le 100)$$$. The second line contains $$$n$$$ integers $$$s_1, s_2, \dots s_n~(1 \le s_i \le 100)$$$ — the multiset $$$s$$$.</div><h2>Output Format</h2><div>If there exists no split of $$$s$$$ to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of $$$n$$$ characters. $$$i$$$-th character should be equal to 'A' if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$a$$$ and 'B' if if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$b$$$. Elements are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there exist multiple solutions, then print any of them.</div><h2>Example</h2><h3>Input</h3><div>4 3 5 7 1</div><h3>Output</h3><div>YES BABA</div><h2>Example</h2><h3>Input</h3><div>3 3 5 1</div><h3>Output</h3><div>NO</div><h2>Editorial</h2><div>Write down all the numbers, which appear exactly once, let there be $$$k$$$ of them. If $$$k$$$ is even, put the first $$$\frac{k}{2}$$$ of them into the first multiset and put the other $$$\frac{k}{2}$$$ into the second multiset. All the other numbers (which appear more than once) also go into the first multiset. The only nice numbers will be the initial $$$k$$$, thus the answer is valid. If $$$k$$$ is odd and there is no number to appear more than twice, then the answer is "NO", as all the numbers to appear exactly twice don't change the difference of the amounts of the nice numbers at all. If there is a number to appear more than twice (let it be $$$x$$$), then let's firstly add $$$\lceil \frac{k}{2} \rceil$$$ of the numbers to appear exactly once to the first multiset, add $$$\lfloor \frac{k}{2} \rfloor$$$ others of them to the second multiset. Then the first occurrence of $$$x$$$ goes to the second multiset and all the other numbers go to the first multiset. It's easy to notice that multisets will contain equal number of the nice numbers after all the partitioning.</div></body></html>
#include <bits/stdc++.h> using namespace std; const int maxn = 1e2+10; int n, a[maxn], cnt[maxn]; char ans[maxn]; int main() { cin >> n; for (int i=1; i <= n; i++) cin >> a[i], cnt[a[i]]++; int sum = 0, isup = 0; for (int i=1; i <= 100; i++) { sum += (cnt[i] == 1); isup += (cnt[i] > 2); } if (sum%2 && isup == 0) return 0 & puts("NO"); puts("YES"); for (int i=1; i <= n; i++) ans[i] = '~'; int val = 0, ss = 0; for (int i=1; i <= n; i++) if (cnt[a[i]] == 1) { ans[i] = char(val+'A'); ss++; if (ss == (sum+1)/2) val++; } for (int i=1; i <= n; i++) if (sum%2 && cnt[a[i]] > 2) { ans[i] = 'B'; break; } for (int i=1; i <= n; i++) if (ans[i] == '~') ans[i] = 'A'; for (int i=1; i <= n; i++) cout << ans[i]; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Space Rescuers</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>The Galaxy contains n planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them. Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station's location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can't sole this problem. As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (xi, yi, zi) and (xj, yj, zj) can be calculated by the formula $$p = \sqrt{(x_i - x_j)^2 + (y_i - y_j)^2 + (z_i - z_j)^2}$$. The rescue station can be positioned in any point in the space. It can also coincide with some planet. Galaxy is in danger! Save the space rescuers and find the required point for them.</div><h2>Input Format</h2><div>The first line of the input file contains integer n — the number of planets (1 ≤ N ≤ 100). Each of the following n lines contains information about the planets. The i-th line contains three integers xi, yi, zi — the coordinates of the i-th planet ( - 104 ≤ xi, yi, zi ≤ 104, 1 ≤ i ≤ n). No two planets coincide.</div><h2>Output Format</h2><div>Print on the first line of the output file three space-separated real numbers x0, y0, z0 — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in no more than 10 - 6 in absolute or relative value.</div><h2>Example</h2><h3>Input</h3><div>5 5 0 0 -5 0 0 0 3 4 4 -3 0 2 2 -2</div><h3>Output</h3><div>0.000 0.000 0.000</div></body></html>
#include<bits/stdc++.h> using namespace std; struct point { double x,y,z; }; double dist(point a, point b) { return sqrt(((a.x - b.x) * (a.x-b.x)) +((a.y - b.y) * (a.y-b.y))+ ((a.z - b.z) * (a.z-b.z))); } point pontos[1002]; int main() { int n; scanf("%d",&n); for(int i = 0 ; i < n ; i++) { scanf("%lf%lf%lf",&pontos[i].x,&pontos[i].y, &pontos[i].z); } double taxa = 0.99; point resp = pontos[0]; for(int i = 0 ; i < 100000; i++) { point far; double maior = -1.0; for(int j = 0 ; j < n ; j++) { double t = dist(resp, pontos[j]); if(t > maior) { far = pontos[j]; maior = t; } } resp.x += taxa*(far.x - resp.x); resp.y += taxa*(far.y - resp.y); resp.z += taxa*(far.z - resp.z); //printf("%.3lf %.3lf %.3lf\n",resp.x, resp.y, resp.z); taxa*=0.999; } printf("%.15lf %.15lf %.15lf\n",resp.x, resp.y, resp.z); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Space Rescuers</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>The Galaxy contains n planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them. Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station's location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can't sole this problem. As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (xi, yi, zi) and (xj, yj, zj) can be calculated by the formula $$p = \sqrt{(x_i - x_j)^2 + (y_i - y_j)^2 + (z_i - z_j)^2}$$. The rescue station can be positioned in any point in the space. It can also coincide with some planet. Galaxy is in danger! Save the space rescuers and find the required point for them.</div><h2>Input Format</h2><div>The first line of the input file contains integer n — the number of planets (1 ≤ N ≤ 100). Each of the following n lines contains information about the planets. The i-th line contains three integers xi, yi, zi — the coordinates of the i-th planet ( - 104 ≤ xi, yi, zi ≤ 104, 1 ≤ i ≤ n). No two planets coincide.</div><h2>Output Format</h2><div>Print on the first line of the output file three space-separated real numbers x0, y0, z0 — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in no more than 10 - 6 in absolute or relative value.</div><h2>Example</h2><h3>Input</h3><div>5 5 0 0 -5 0 0 0 3 4 4 -3 0 2 2 -2</div><h3>Output</h3><div>0.000 0.000 0.000</div></body></html>
#include<bits/stdc++.h> using namespace std; struct point { double x,y,z; }; double dist(point a, point b) { return sqrt(((a.x - b.x) * (a.x-b.x)) +((a.y - b.y) * (a.y-b.y))+ ((a.z - b.z) * (a.z-b.z))); } point pontos[1002]; int main() { int n; scanf("%d",&n); for(int i = 0 ; i < n ; i++) { scanf("%lf%lf%lf",&pontos[i].x,&pontos[i].y, &pontos[i].z); } double taxa = 1.; point resp = pontos[0]; for(int i = 0 ; i < 100000; i++) { point far; double maior = -1.0; for(int j = 0 ; j < n ; j++) { double t = dist(resp, pontos[j]); if(t > maior) { far = pontos[j]; maior = t; } } resp.x += taxa*(far.x - resp.x); resp.y += taxa*(far.y - resp.y); resp.z += taxa*(far.z - resp.z); //printf("%.3lf %.3lf %.3lf\n",resp.x, resp.y, resp.z); taxa*=0.999; } printf("%.15lf %.15lf %.15lf\n",resp.x, resp.y, resp.z); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Knights</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan places knights on infinite chessboard. Initially there are $$$n$$$ knights. If there is free cell which is under attack of at least $$$4$$$ knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that position in the end does not depend on the order in which new knights are placed. Ivan asked you to find initial placement of exactly $$$n$$$ knights such that in the end there will be at least $$$\lfloor \frac{n^{2}}{10} \rfloor$$$ knights.</div><h2>Input Format</h2><div>The only line of input contains one integer $$$n$$$ ($$$1 \le n \le 10^{3}$$$) — number of knights in the initial placement.</div><h2>Output Format</h2><div>Print $$$n$$$ lines. Each line should contain $$$2$$$ numbers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$-10^{9} \le x_{i}, \,\, y_{i} \le 10^{9}$$$) — coordinates of $$$i$$$-th knight. For all $$$i \ne j$$$, $$$(x_{i}, \,\, y_{i}) \ne (x_{j}, \,\, y_{j})$$$ should hold. In other words, all knights should be in different cells. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>4</div><h3>Output</h3><div>1 1 3 1 1 5 4 4</div><h2>Example</h2><h3>Input</h3><div>7</div><h3>Output</h3><div>2 1 1 2 4 1 5 2 2 6 5 7 6 6</div><h2>Note</h2><div>Let's look at second example: Green zeroes are initial knights. Cell $$$(3, \,\, 3)$$$ is under attack of $$$4$$$ knights in cells $$$(1, \,\, 2)$$$, $$$(2, \,\, 1)$$$, $$$(4, \,\, 1)$$$ and $$$(5, \,\, 2)$$$, therefore Ivan will place a knight in this cell. Cell $$$(4, \,\, 5)$$$ is initially attacked by only $$$3$$$ knights in cells $$$(2, \,\, 6)$$$, $$$(5, \,\, 7)$$$ and $$$(6, \,\, 6)$$$. But new knight in cell $$$(3, \,\, 3)$$$ also attacks cell $$$(4, \,\, 5)$$$, now it is attacked by $$$4$$$ knights and Ivan will place another knight in this cell. There are no more free cells which are attacked by $$$4$$$ or more knights, so the process stops. There are $$$9$$$ knights in the end, which is not less than $$$\lfloor \frac{7^{2}}{10} \rfloor = 4$$$.</div><h2>Editorial</h2><div>If after some loops of the process we will have two neighboring lines with length $$$x$$$ total complexity of knights would be not less than $$$O( \frac{x^2}{4} )$$$. In this construction: $$$0$$$ — initial placement. $$$1, \,\, 2$$$ — added knights. Would be two neighboring lines with length $$$O(\frac{2 \cdot n}{3})$$$ so total complexity of knights would be $$$O( \frac{(\frac{2 \cdot n}{3})^2}{4} ) = O( \frac{n^2}{9} )$$$. The possible way to facilitate the invention this (or over) solutions is to write process modeling. Bonus: Solve this problem with complexity $$$ O( \frac{n^2}{6} )$$$.</div></body></html>
#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/STACK:100000000") #include <stdio.h> #include <vector> #include <string> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <bitset> #include <queue> #include <algorithm> #include <string.h> #include <math.h> #include <fstream> #include <iostream> #include <ctime> using namespace std; const int N=100100; int main() { int n; scanf("%d", &n); printf("%d %d\n", 0, 0); n--; for(int i=0; i<n/4; printf("%d %d\n", i+1, 0), i++); for(int i=n/4; i<2*n/4; printf("%d %d\n", -(i-n/4+1), 0), i++); for(int i=2*n/4; i<3*n/4; printf("%d %d\n", 0, i-2*n/4+1), i++); for(int i=3*n/4; i<n; printf("%d %d\n", 0, -(i-3*n/4+1)), i++); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Knights</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan places knights on infinite chessboard. Initially there are $$$n$$$ knights. If there is free cell which is under attack of at least $$$4$$$ knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that position in the end does not depend on the order in which new knights are placed. Ivan asked you to find initial placement of exactly $$$n$$$ knights such that in the end there will be at least $$$\lfloor \frac{n^{2}}{10} \rfloor$$$ knights.</div><h2>Input Format</h2><div>The only line of input contains one integer $$$n$$$ ($$$1 \le n \le 10^{3}$$$) — number of knights in the initial placement.</div><h2>Output Format</h2><div>Print $$$n$$$ lines. Each line should contain $$$2$$$ numbers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$-10^{9} \le x_{i}, \,\, y_{i} \le 10^{9}$$$) — coordinates of $$$i$$$-th knight. For all $$$i \ne j$$$, $$$(x_{i}, \,\, y_{i}) \ne (x_{j}, \,\, y_{j})$$$ should hold. In other words, all knights should be in different cells. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>4</div><h3>Output</h3><div>1 1 3 1 1 5 4 4</div><h2>Example</h2><h3>Input</h3><div>7</div><h3>Output</h3><div>2 1 1 2 4 1 5 2 2 6 5 7 6 6</div><h2>Note</h2><div>Let's look at second example: Green zeroes are initial knights. Cell $$$(3, \,\, 3)$$$ is under attack of $$$4$$$ knights in cells $$$(1, \,\, 2)$$$, $$$(2, \,\, 1)$$$, $$$(4, \,\, 1)$$$ and $$$(5, \,\, 2)$$$, therefore Ivan will place a knight in this cell. Cell $$$(4, \,\, 5)$$$ is initially attacked by only $$$3$$$ knights in cells $$$(2, \,\, 6)$$$, $$$(5, \,\, 7)$$$ and $$$(6, \,\, 6)$$$. But new knight in cell $$$(3, \,\, 3)$$$ also attacks cell $$$(4, \,\, 5)$$$, now it is attacked by $$$4$$$ knights and Ivan will place another knight in this cell. There are no more free cells which are attacked by $$$4$$$ or more knights, so the process stops. There are $$$9$$$ knights in the end, which is not less than $$$\lfloor \frac{7^{2}}{10} \rfloor = 4$$$.</div><h2>Editorial</h2><div>If after some loops of the process we will have two neighboring lines with length $$$x$$$ total complexity of knights would be not less than $$$O( \frac{x^2}{4} )$$$. In this construction: $$$0$$$ — initial placement. $$$1, \,\, 2$$$ — added knights. Would be two neighboring lines with length $$$O(\frac{2 \cdot n}{3})$$$ so total complexity of knights would be $$$O( \frac{(\frac{2 \cdot n}{3})^2}{4} ) = O( \frac{n^2}{9} )$$$. The possible way to facilitate the invention this (or over) solutions is to write process modeling. Bonus: Solve this problem with complexity $$$ O( \frac{n^2}{6} )$$$.</div></body></html>
#include<bits/stdc++.h> using namespace std; #define SET(a,e) memset(a,e,sizeof(a)) #define LL long long #define LD long double #define pb push_back #define x first #define y second #define PII pair<int,int> #define PLI pair<LL,int> #define PIL pair<int,LL> #define PLL pair<LL,LL> #define PDD pair<LD,LD> #define eps 1e-9 #define HH1 402653189 #define HH2 1610612741 int n; inline int count(int x) { return x * x - (x - 3) * (x - 4); } int main() { scanf("%d", &n); int k = 0; while (count(k) <= n) k++; int p = 0; for (int i = 0; i < k; i++) { if (i < 3) { for (int j = 0; j < k; j++) if (p < n) { printf("%d %d\n", i * 2, j * 2); p++; if (p == n) break; } } else { for (int j = 0; j < k; j++) if (p < n) { if (j < 2 || j >= k - 2) { printf("%d %d\n", i * 2, j * 2); p++; if (p == n) break; } } } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Knights</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan places knights on infinite chessboard. Initially there are $$$n$$$ knights. If there is free cell which is under attack of at least $$$4$$$ knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that position in the end does not depend on the order in which new knights are placed. Ivan asked you to find initial placement of exactly $$$n$$$ knights such that in the end there will be at least $$$\lfloor \frac{n^{2}}{10} \rfloor$$$ knights.</div><h2>Input Format</h2><div>The only line of input contains one integer $$$n$$$ ($$$1 \le n \le 10^{3}$$$) — number of knights in the initial placement.</div><h2>Output Format</h2><div>Print $$$n$$$ lines. Each line should contain $$$2$$$ numbers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$-10^{9} \le x_{i}, \,\, y_{i} \le 10^{9}$$$) — coordinates of $$$i$$$-th knight. For all $$$i \ne j$$$, $$$(x_{i}, \,\, y_{i}) \ne (x_{j}, \,\, y_{j})$$$ should hold. In other words, all knights should be in different cells. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>4</div><h3>Output</h3><div>1 1 3 1 1 5 4 4</div><h2>Example</h2><h3>Input</h3><div>7</div><h3>Output</h3><div>2 1 1 2 4 1 5 2 2 6 5 7 6 6</div><h2>Note</h2><div>Let's look at second example: Green zeroes are initial knights. Cell $$$(3, \,\, 3)$$$ is under attack of $$$4$$$ knights in cells $$$(1, \,\, 2)$$$, $$$(2, \,\, 1)$$$, $$$(4, \,\, 1)$$$ and $$$(5, \,\, 2)$$$, therefore Ivan will place a knight in this cell. Cell $$$(4, \,\, 5)$$$ is initially attacked by only $$$3$$$ knights in cells $$$(2, \,\, 6)$$$, $$$(5, \,\, 7)$$$ and $$$(6, \,\, 6)$$$. But new knight in cell $$$(3, \,\, 3)$$$ also attacks cell $$$(4, \,\, 5)$$$, now it is attacked by $$$4$$$ knights and Ivan will place another knight in this cell. There are no more free cells which are attacked by $$$4$$$ or more knights, so the process stops. There are $$$9$$$ knights in the end, which is not less than $$$\lfloor \frac{7^{2}}{10} \rfloor = 4$$$.</div><h2>Editorial</h2><div>If after some loops of the process we will have two neighboring lines with length $$$x$$$ total complexity of knights would be not less than $$$O( \frac{x^2}{4} )$$$. In this construction: $$$0$$$ — initial placement. $$$1, \,\, 2$$$ — added knights. Would be two neighboring lines with length $$$O(\frac{2 \cdot n}{3})$$$ so total complexity of knights would be $$$O( \frac{(\frac{2 \cdot n}{3})^2}{4} ) = O( \frac{n^2}{9} )$$$. The possible way to facilitate the invention this (or over) solutions is to write process modeling. Bonus: Solve this problem with complexity $$$ O( \frac{n^2}{6} )$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int nn = n; for(int i = 0; i<n; i++){ if(i % 2 == 0){ cout << i << " " << 2 << endl; nn--; if(nn == 0) return 0; cout << i << " " << 0 << endl; nn--; if(nn == 0) return 0; } if(i % 2 == 1){ cout << i << " " << 1 << endl; nn--; if(nn == 0) return 0; } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Colored Rooks</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} \times 10^{9}$$$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: - For any color there is a rook of this color on a board; - For any color the set of rooks of this color is connected; - For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement.</div><h2>Input Format</h2><div>The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$$$) — number of colors and number of pairs of colors which harmonize with each other. In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list.</div><h2>Output Format</h2><div>Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color. In the first line of block print one number $$$a_{i}$$$ ($$$1 \le a_{i} \le 5000$$$) — number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, \,\, y \le 10^{9}$$$) — coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $$$5000$$$. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>3 2 1 2 2 3</div><h3>Output</h3><div>2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1</div><h2>Example</h2><h3>Input</h3><div>3 3 1 2 2 3 3 1</div><h3>Output</h3><div>1 1 1 1 1 2 1 1 3</div><h2>Example</h2><h3>Input</h3><div>3 1 1 3</div><h3>Output</h3><div>1 1 1 1 2 2 1 3 1</div><h2>Note</h2><div>Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).</div><h2>Editorial</h2><div>Let's put rooks with color $$$i$$$ just on line number $$$i$$$. Then, obviously, for any color the set of rooks of this color would be connected. Let's put rooks on positions $$$(i, \,\, i)$$$ for $$$i$$$ from $$$1$$$ to $$$n$$$. After that for any color there is a rook of this color on a board and for any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ wouldn't be connected. And for final step we can do the following for every pair of harmonizing colors $$$a$$$ $$$b$$$: let $$$j$$$ be index of first column without rooks, put rooks on cells ($$$j, \,\, a$$$) and ($$$j, \,\, b$$$). After that for colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ would become connected and for other pairs the connectedness doesn't change. Total number of rooks is $$$n + 2 \cdot m$$$.</div></body></html>
#include <bits/stdc++.h> #pragma GCC optimize("03") #define ll long long #define ld long double #define fi first #define se second #define mod 100 using namespace std; int n, m, x, y, ok[110][110], vf[110], sol[110][110], cnt[110]; int main() { // ifstream cin("tst.in"); // ofstream out("tst.out"); ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m; int cnt = 0; for (int i = 1; i <= m; i++) { cin >> x >> y; cnt++; vf[x]++; vf[y]++; sol[x][vf[x]] = cnt; sol[y][vf[y]] = cnt; } for (int i = 1; i <= n; i++) { if (vf[i] == 0) { vf[i]++; sol[i][vf[i]] = ++cnt; } } int rs = 0; for (int i = 1; i <= n; i++) { cout << vf[i] << '\n'; for (int j = 1; j <= vf[i]; j++) cout << i << ' ' << sol[i][j] << '\n'; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Colored Rooks</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} \times 10^{9}$$$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: - For any color there is a rook of this color on a board; - For any color the set of rooks of this color is connected; - For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement.</div><h2>Input Format</h2><div>The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$$$) — number of colors and number of pairs of colors which harmonize with each other. In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list.</div><h2>Output Format</h2><div>Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color. In the first line of block print one number $$$a_{i}$$$ ($$$1 \le a_{i} \le 5000$$$) — number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, \,\, y \le 10^{9}$$$) — coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $$$5000$$$. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>3 2 1 2 2 3</div><h3>Output</h3><div>2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1</div><h2>Example</h2><h3>Input</h3><div>3 3 1 2 2 3 3 1</div><h3>Output</h3><div>1 1 1 1 1 2 1 1 3</div><h2>Example</h2><h3>Input</h3><div>3 1 1 3</div><h3>Output</h3><div>1 1 1 1 2 2 1 3 1</div><h2>Note</h2><div>Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).</div><h2>Editorial</h2><div>Let's put rooks with color $$$i$$$ just on line number $$$i$$$. Then, obviously, for any color the set of rooks of this color would be connected. Let's put rooks on positions $$$(i, \,\, i)$$$ for $$$i$$$ from $$$1$$$ to $$$n$$$. After that for any color there is a rook of this color on a board and for any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ wouldn't be connected. And for final step we can do the following for every pair of harmonizing colors $$$a$$$ $$$b$$$: let $$$j$$$ be index of first column without rooks, put rooks on cells ($$$j, \,\, a$$$) and ($$$j, \,\, b$$$). After that for colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ would become connected and for other pairs the connectedness doesn't change. Total number of rooks is $$$n + 2 \cdot m$$$.</div></body></html>
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define S second #define F first using namespace std; vector <pair<int, int> > ans[10000]; int main() { int n, m; cin>>n>>m; for(int i = 0; i<n; i++) { ans[i].push_back({10000000 - i, i}); } for(int i = 0; i<m; i++) { int l, r; cin>>l>>r; ans[l-1].push_back({i + 1, l-1}); ans[r-1].push_back({i + 1, r- 1}); } for(int i = 0; i<n; i++) { cout<<ans[i].size()<<'\n'; for(int j = 0; j<ans[i].size(); j++) { cout<<ans[i][j].first + 1<<" "<<ans[i][j].second + 1<<'\n'; } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Colored Rooks</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} \times 10^{9}$$$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: - For any color there is a rook of this color on a board; - For any color the set of rooks of this color is connected; - For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement.</div><h2>Input Format</h2><div>The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$$$) — number of colors and number of pairs of colors which harmonize with each other. In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list.</div><h2>Output Format</h2><div>Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color. In the first line of block print one number $$$a_{i}$$$ ($$$1 \le a_{i} \le 5000$$$) — number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, \,\, y \le 10^{9}$$$) — coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $$$5000$$$. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>3 2 1 2 2 3</div><h3>Output</h3><div>2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1</div><h2>Example</h2><h3>Input</h3><div>3 3 1 2 2 3 3 1</div><h3>Output</h3><div>1 1 1 1 1 2 1 1 3</div><h2>Example</h2><h3>Input</h3><div>3 1 1 3</div><h3>Output</h3><div>1 1 1 1 2 2 1 3 1</div><h2>Note</h2><div>Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).</div><h2>Editorial</h2><div>Let's put rooks with color $$$i$$$ just on line number $$$i$$$. Then, obviously, for any color the set of rooks of this color would be connected. Let's put rooks on positions $$$(i, \,\, i)$$$ for $$$i$$$ from $$$1$$$ to $$$n$$$. After that for any color there is a rook of this color on a board and for any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ wouldn't be connected. And for final step we can do the following for every pair of harmonizing colors $$$a$$$ $$$b$$$: let $$$j$$$ be index of first column without rooks, put rooks on cells ($$$j, \,\, a$$$) and ($$$j, \,\, b$$$). After that for colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ would become connected and for other pairs the connectedness doesn't change. Total number of rooks is $$$n + 2 \cdot m$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; int p[101],y = 1; struct lol{ int x,y; lol(int a = 0,int b = 0){x = a;y = b;} }; vector<lol>ans[101]; int main(){ // freopen("readin.txt","r",stdin); int n,m; cin >> n >> m; for(int i = 0;i < n;i++)ans[i].push_back({i + 1,y++}); for(int i = 0,a,b;i < m;i++){ cin >> a >> b; a--;b--; ans[a].push_back({a + 1,y}); ans[b].push_back({b + 1,y++}); } for(int i = 0;i < n;i++){ cout << ans[i].size() << endl; for(auto i : ans[i])printf("%d %d\n",i.x,i.y); } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Colored Rooks</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} \times 10^{9}$$$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: - For any color there is a rook of this color on a board; - For any color the set of rooks of this color is connected; - For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement.</div><h2>Input Format</h2><div>The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$$$) — number of colors and number of pairs of colors which harmonize with each other. In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list.</div><h2>Output Format</h2><div>Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color. In the first line of block print one number $$$a_{i}$$$ ($$$1 \le a_{i} \le 5000$$$) — number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, \,\, y \le 10^{9}$$$) — coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $$$5000$$$. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>3 2 1 2 2 3</div><h3>Output</h3><div>2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1</div><h2>Example</h2><h3>Input</h3><div>3 3 1 2 2 3 3 1</div><h3>Output</h3><div>1 1 1 1 1 2 1 1 3</div><h2>Example</h2><h3>Input</h3><div>3 1 1 3</div><h3>Output</h3><div>1 1 1 1 2 2 1 3 1</div><h2>Note</h2><div>Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).</div><h2>Editorial</h2><div>Let's put rooks with color $$$i$$$ just on line number $$$i$$$. Then, obviously, for any color the set of rooks of this color would be connected. Let's put rooks on positions $$$(i, \,\, i)$$$ for $$$i$$$ from $$$1$$$ to $$$n$$$. After that for any color there is a rook of this color on a board and for any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ wouldn't be connected. And for final step we can do the following for every pair of harmonizing colors $$$a$$$ $$$b$$$: let $$$j$$$ be index of first column without rooks, put rooks on cells ($$$j, \,\, a$$$) and ($$$j, \,\, b$$$). After that for colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ would become connected and for other pairs the connectedness doesn't change. Total number of rooks is $$$n + 2 \cdot m$$$.</div></body></html>
#include <iostream> #include <vector> #include <utility> #include <set> #include <algorithm> using namespace std; vector<int> rooks_[101]; vector<int> a_, b_; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; set<pair<int, int>> set; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); if (a == b or set.count({a, b})) continue; set.insert({a, b}); // bの行はaによって決まる rooks_[a].push_back((a - 1) * n + b); rooks_[b].push_back((a - 1) * n + b); } for (int c = 1; c <= n; c++) { if (rooks_[c].size() == 0) { rooks_[c].push_back((c - 1) * n + 1); } cout << rooks_[c].size() << '\n'; for (int r : rooks_[c]) { cout << c << " " << r << '\n'; } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Colored Rooks</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} \times 10^{9}$$$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: - For any color there is a rook of this color on a board; - For any color the set of rooks of this color is connected; - For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement.</div><h2>Input Format</h2><div>The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$$$) — number of colors and number of pairs of colors which harmonize with each other. In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list.</div><h2>Output Format</h2><div>Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color. In the first line of block print one number $$$a_{i}$$$ ($$$1 \le a_{i} \le 5000$$$) — number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, \,\, y \le 10^{9}$$$) — coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $$$5000$$$. It is guaranteed that the solution exists.</div><h2>Example</h2><h3>Input</h3><div>3 2 1 2 2 3</div><h3>Output</h3><div>2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1</div><h2>Example</h2><h3>Input</h3><div>3 3 1 2 2 3 3 1</div><h3>Output</h3><div>1 1 1 1 1 2 1 1 3</div><h2>Example</h2><h3>Input</h3><div>3 1 1 3</div><h3>Output</h3><div>1 1 1 1 2 2 1 3 1</div><h2>Note</h2><div>Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).</div><h2>Editorial</h2><div>Let's put rooks with color $$$i$$$ just on line number $$$i$$$. Then, obviously, for any color the set of rooks of this color would be connected. Let's put rooks on positions $$$(i, \,\, i)$$$ for $$$i$$$ from $$$1$$$ to $$$n$$$. After that for any color there is a rook of this color on a board and for any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ wouldn't be connected. And for final step we can do the following for every pair of harmonizing colors $$$a$$$ $$$b$$$: let $$$j$$$ be index of first column without rooks, put rooks on cells ($$$j, \,\, a$$$) and ($$$j, \,\, b$$$). After that for colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ would become connected and for other pairs the connectedness doesn't change. Total number of rooks is $$$n + 2 \cdot m$$$.</div></body></html>
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define F first #define S second #define pb emplace_back #define pi pair<int,int> using namespace std; const int maxn = 104; int n, m; int x, y; vector<pi> v; vector<pi> g[maxn]; int mi[maxn]; int main(){ // freopen("scc", "r", stdin); // freopen("in.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>m; for (int i = 0; i < m; ++i){ cin>>x>>y; v.pb(x, y); } for (int i = 1; i <= n; ++i){ g[i].pb(i * 10000, i * 10000); mi[i] = 1; } for (int i = 0; i < m; ++i){ tie(x, y) = v[i]; g[x].pb(x * 10000, y * 10000-mi[y]); g[y].pb(y * 10000, y * 10000-mi[y]); mi[y]++; } for (int i = 1; i <= n; ++i){ cout<<g[i].size()<<'\n'; for (auto j: g[i]){ cout<<j.F<<' '<<j.S<<'\n'; } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Meme Problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Try guessing the statement from this picture: You are given a non-negative integer $$$d$$$. You have to find two non-negative real numbers $$$a$$$ and $$$b$$$ such that $$$a + b = d$$$ and $$$a \cdot b = d$$$.</div><h2>Input Format</h2><div>The first line contains $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 \le d \le 10^3)$$$.</div><h2>Output Format</h2><div>For each test print one line. If there is an answer for the $$$i$$$-th test, print "Y", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print "N". Your answer will be considered correct if $$$|(a + b) - a \cdot b| \le 10^{-6}$$$ and $$$|(a + b) - d| \le 10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>7 69 0 1 4 5 999 1000</div><h3>Output</h3><div>Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005</div><h2>Editorial</h2><div>To solve this problem we need to use some math and solve the equation on the paper. If $$$a + b = d$$$ then $$$a = d - b$$$ and $$$a \cdot b = d$$$ transforms to $$$b (d - b) = d$$$ or $$$db - b^2 - d = 0$$$. Then $$$a, b = (d \pm \sqrt{D}) / 2$$$ where $$$D = d^2 - 4d$$$. So if $$$d = 0$$$ then $$$a = b = 0$$$, or if $$$0 < d < 4$$$ there is no answer. Since values are small, calculating answer in double was enough, all we need to do is just output answer with sufficient number of digits after the decimal point.</div></body></html>
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll t; cin>>t; while(t--) { ll n; cin>>n; long double k1,k2; if((n*n-4*n)<0) cout<<"N"<<endl; else{ k1=(n+sqrt(n*n-4*n))/2; k2=n-k1; cout<<"Y"<<" "; std::cout<<std::setprecision(11)<<k1; std::cout<<" "<<std::setprecision(11)<<k2<<endl; } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Meme Problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Try guessing the statement from this picture: You are given a non-negative integer $$$d$$$. You have to find two non-negative real numbers $$$a$$$ and $$$b$$$ such that $$$a + b = d$$$ and $$$a \cdot b = d$$$.</div><h2>Input Format</h2><div>The first line contains $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 \le d \le 10^3)$$$.</div><h2>Output Format</h2><div>For each test print one line. If there is an answer for the $$$i$$$-th test, print "Y", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print "N". Your answer will be considered correct if $$$|(a + b) - a \cdot b| \le 10^{-6}$$$ and $$$|(a + b) - d| \le 10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>7 69 0 1 4 5 999 1000</div><h3>Output</h3><div>Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005</div><h2>Editorial</h2><div>To solve this problem we need to use some math and solve the equation on the paper. If $$$a + b = d$$$ then $$$a = d - b$$$ and $$$a \cdot b = d$$$ transforms to $$$b (d - b) = d$$$ or $$$db - b^2 - d = 0$$$. Then $$$a, b = (d \pm \sqrt{D}) / 2$$$ where $$$D = d^2 - 4d$$$. So if $$$d = 0$$$ then $$$a = b = 0$$$, or if $$$0 < d < 4$$$ there is no answer. Since values are small, calculating answer in double was enough, all we need to do is just output answer with sufficient number of digits after the decimal point.</div></body></html>
#include<bits/stdc++.h> #define FOR(i, x, a) for(long i=x; i<=a; i++) #define FORD(i, x, a) for(long i=x; i>=a; i--) #define ll long long #define pii pair < int, int > #define vii vector < pii > #define ms(a, x) memset(a, x, sizeof(a)) #define NMAX 1000 using namespace std; long t, d; double del; int main() { //ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen("text.inp", "r", stdin); //freopen("text.out", "w", stdout); cin >> t; while (t--) { cin >> d; //cout << d << endl; if (d*d-4*d < 0) cout << "N\n"; else { del = sqrt(d*d-4*d); printf("Y %.9llf %.9llf\n", (d+del)/2.0, (d-del)/2.0); } } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Meme Problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Try guessing the statement from this picture: You are given a non-negative integer $$$d$$$. You have to find two non-negative real numbers $$$a$$$ and $$$b$$$ such that $$$a + b = d$$$ and $$$a \cdot b = d$$$.</div><h2>Input Format</h2><div>The first line contains $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 \le d \le 10^3)$$$.</div><h2>Output Format</h2><div>For each test print one line. If there is an answer for the $$$i$$$-th test, print "Y", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print "N". Your answer will be considered correct if $$$|(a + b) - a \cdot b| \le 10^{-6}$$$ and $$$|(a + b) - d| \le 10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>7 69 0 1 4 5 999 1000</div><h3>Output</h3><div>Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005</div><h2>Editorial</h2><div>To solve this problem we need to use some math and solve the equation on the paper. If $$$a + b = d$$$ then $$$a = d - b$$$ and $$$a \cdot b = d$$$ transforms to $$$b (d - b) = d$$$ or $$$db - b^2 - d = 0$$$. Then $$$a, b = (d \pm \sqrt{D}) / 2$$$ where $$$D = d^2 - 4d$$$. So if $$$d = 0$$$ then $$$a = b = 0$$$, or if $$$0 < d < 4$$$ there is no answer. Since values are small, calculating answer in double was enough, all we need to do is just output answer with sufficient number of digits after the decimal point.</div></body></html>
#include<iostream> #include<cmath> #include<iomanip> using namespace std; int main() { int n, a, p, i; double q1, q2; cin>>n; cout<<setprecision(16); for(i=0; i<n; i++) { cin>>a; p=a*a-4*a; if(p<0) { cout<<"N"<<endl; } else { q1=a; q2=sqrt(p); cout<<"Y"<<" "<<(q1+q2)/2.0<<" "<<(q1-q2)/2.0<<endl; } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Meme Problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Try guessing the statement from this picture: You are given a non-negative integer $$$d$$$. You have to find two non-negative real numbers $$$a$$$ and $$$b$$$ such that $$$a + b = d$$$ and $$$a \cdot b = d$$$.</div><h2>Input Format</h2><div>The first line contains $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 \le d \le 10^3)$$$.</div><h2>Output Format</h2><div>For each test print one line. If there is an answer for the $$$i$$$-th test, print "Y", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print "N". Your answer will be considered correct if $$$|(a + b) - a \cdot b| \le 10^{-6}$$$ and $$$|(a + b) - d| \le 10^{-6}$$$.</div><h2>Example</h2><h3>Input</h3><div>7 69 0 1 4 5 999 1000</div><h3>Output</h3><div>Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005</div><h2>Editorial</h2><div>To solve this problem we need to use some math and solve the equation on the paper. If $$$a + b = d$$$ then $$$a = d - b$$$ and $$$a \cdot b = d$$$ transforms to $$$b (d - b) = d$$$ or $$$db - b^2 - d = 0$$$. Then $$$a, b = (d \pm \sqrt{D}) / 2$$$ where $$$D = d^2 - 4d$$$. So if $$$d = 0$$$ then $$$a = b = 0$$$, or if $$$0 < d < 4$$$ there is no answer. Since values are small, calculating answer in double was enough, all we need to do is just output answer with sufficient number of digits after the decimal point.</div></body></html>
#include<bits/stdc++.h> using namespace std; int main() { long long int t,dis; cin>>t; double a,b,c,x; while(t--) { cin>>dis; x=(dis*dis-4*dis); if(x<0) { printf("N\n"); } else { c=sqrt(x); b=(dis+c)/2; a=(abs)(b/(b-1)); printf("Y %.9f %.9f\n",b,a); } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Good Array</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $$$a=[1, 3, 3, 7]$$$ is good because there is the element $$$a_4=7$$$ which equals to the sum $$$1 + 3 + 3$$$. You are given an array $$$a$$$ consisting of $$$n$$$ integers. Your task is to print all indices $$$j$$$ of this array such that after removing the $$$j$$$-th element from the array it will be good (let's call such indices nice). For example, if $$$a=[8, 3, 5, 2]$$$, the nice indices are $$$1$$$ and $$$4$$$: - if you remove $$$a_1$$$, the array will look like $$$[3, 5, 2]$$$ and it is good; - if you remove $$$a_4$$$, the array will look like $$$[8, 3, 5]$$$ and it is good. You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.</div><h2>Input Format</h2><div>The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — elements of the array $$$a$$$.</div><h2>Output Format</h2><div>In the first line print one integer $$$k$$$ — the number of indices $$$j$$$ of the array $$$a$$$ such that after removing the $$$j$$$-th element from the array it will be good (i.e. print the number of the nice indices). In the second line print $$$k$$$ distinct integers $$$j_1, j_2, \dots, j_k$$$ in any order — nice indices of the array $$$a$$$. If there are no such indices in the array $$$a$$$, just print $$$0$$$ in the first line and leave the second line empty or do not print it at all.</div><h2>Example</h2><h3>Input</h3><div>5 2 5 1 2 2</div><h3>Output</h3><div>3 4 1 5</div><h2>Example</h2><h3>Input</h3><div>4 8 3 5 2</div><h3>Output</h3><div>2 1 4</div><h2>Example</h2><h3>Input</h3><div>5 2 1 2 4 3</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first example you can remove any element with the value $$$2$$$ so the array will look like $$$[5, 1, 2, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 1 + 2 + 2$$$). In the second example you can remove $$$8$$$ so the array will look like $$$[3, 5, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 3 + 2$$$). You can also remove $$$2$$$ so the array will look like $$$[8, 3, 5]$$$. The sum of this array is $$$16$$$ and there is an element equals to the sum of remaining elements ($$$8 = 3 + 5$$$). In the third example you cannot make the given array good by removing exactly one element.</div><h2>Editorial</h2><div>The first part: calculate the sum of the whole array: $$$sum = \sum\limits_{i = 1}^n a_i$$$ (be careful, it can be $$$2 \cdot 10^{11}$$$!). The second part: let's maintain an array $$$cnt$$$ of size $$$10^6 + 1$$$ where $$$cnt_i$$$ will be equal to the number of elements in the given array equals to $$$i$$$. The third part: iterate over the array, let the current position be $$$i$$$. Set $$$sum := sum - a_i$$$, make $$$cnt_{a_i} := cnt_{a_i} - 1$$$. If $$$sum$$$ is even, $$$\frac{sum}{2} \le 10^6$$$ and $$$cnt_{\frac{sum}{2}} > 0$$$ then the index $$$i$$$ is nice otherwise it doesn't. And after all make $$$cnt_{a_i} := cnt_{a_i} + 1$$$ and set $$$sum := sum + a_i$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; int num[200005]; int cnt[200005]; bool flag[200005]; int main(int argc, char** argv) { int n; int m=0; cin>>n; int max1=0; int xu=0; long long ans=0; for (int a=1;a<=n;a++) { scanf("%d",&num[a]); ans+=num[a]; if (num[a]>max1) { xu=a; max1=num[a]; } } long long l=ans; l-=max1; for (int a=1;a<=n;a++) { if (l-num[a]==max1&&a!=xu) { flag[a]=1; cnt[++m]=a; } } int max2=0; int xu2=0; for (int a=1;a<=n;a++) if (num[a]>max2&&a!=xu) { xu2=a; max2=num[a]; } l=ans; l-=max2; l-=max1; if (l==max2&&flag[xu]==0) cnt[++m]=xu; cout<<m<<endl; for (int a=1;a<=m;a++) printf("%d ",cnt[a]); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Good Array</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $$$a=[1, 3, 3, 7]$$$ is good because there is the element $$$a_4=7$$$ which equals to the sum $$$1 + 3 + 3$$$. You are given an array $$$a$$$ consisting of $$$n$$$ integers. Your task is to print all indices $$$j$$$ of this array such that after removing the $$$j$$$-th element from the array it will be good (let's call such indices nice). For example, if $$$a=[8, 3, 5, 2]$$$, the nice indices are $$$1$$$ and $$$4$$$: - if you remove $$$a_1$$$, the array will look like $$$[3, 5, 2]$$$ and it is good; - if you remove $$$a_4$$$, the array will look like $$$[8, 3, 5]$$$ and it is good. You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.</div><h2>Input Format</h2><div>The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — elements of the array $$$a$$$.</div><h2>Output Format</h2><div>In the first line print one integer $$$k$$$ — the number of indices $$$j$$$ of the array $$$a$$$ such that after removing the $$$j$$$-th element from the array it will be good (i.e. print the number of the nice indices). In the second line print $$$k$$$ distinct integers $$$j_1, j_2, \dots, j_k$$$ in any order — nice indices of the array $$$a$$$. If there are no such indices in the array $$$a$$$, just print $$$0$$$ in the first line and leave the second line empty or do not print it at all.</div><h2>Example</h2><h3>Input</h3><div>5 2 5 1 2 2</div><h3>Output</h3><div>3 4 1 5</div><h2>Example</h2><h3>Input</h3><div>4 8 3 5 2</div><h3>Output</h3><div>2 1 4</div><h2>Example</h2><h3>Input</h3><div>5 2 1 2 4 3</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first example you can remove any element with the value $$$2$$$ so the array will look like $$$[5, 1, 2, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 1 + 2 + 2$$$). In the second example you can remove $$$8$$$ so the array will look like $$$[3, 5, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 3 + 2$$$). You can also remove $$$2$$$ so the array will look like $$$[8, 3, 5]$$$. The sum of this array is $$$16$$$ and there is an element equals to the sum of remaining elements ($$$8 = 3 + 5$$$). In the third example you cannot make the given array good by removing exactly one element.</div><h2>Editorial</h2><div>The first part: calculate the sum of the whole array: $$$sum = \sum\limits_{i = 1}^n a_i$$$ (be careful, it can be $$$2 \cdot 10^{11}$$$!). The second part: let's maintain an array $$$cnt$$$ of size $$$10^6 + 1$$$ where $$$cnt_i$$$ will be equal to the number of elements in the given array equals to $$$i$$$. The third part: iterate over the array, let the current position be $$$i$$$. Set $$$sum := sum - a_i$$$, make $$$cnt_{a_i} := cnt_{a_i} - 1$$$. If $$$sum$$$ is even, $$$\frac{sum}{2} \le 10^6$$$ and $$$cnt_{\frac{sum}{2}} > 0$$$ then the index $$$i$$$ is nice otherwise it doesn't. And after all make $$$cnt_{a_i} := cnt_{a_i} + 1$$$ and set $$$sum := sum + a_i$$$.</div></body></html>
#include<bits/stdc++.h> #define gc getchar #define rint register int #define rep(i,u,z) for(rint i=u;i<=z;++i) #define reb(i,u,z) for(rint i=u;i>=z;--i) #define inf 0x7fffffff using namespace std; typedef long long ll; const int N=2e5+10; int n,cnt; ll tot; struct node{ ll num; int id; bool ok; bool operator < (const node ano) const{ return num<ano.num; } }a[N]; inline int readint(){ int rt=0,sign=1; char ch=gc(); while(ch<'0'||ch>'9'){ if(ch=='-')sign=-1; ch=gc(); } while(ch>='0'&&ch<='9'){ rt=(rt<<3)+(rt<<1)+(ch&15); ch=gc(); } return rt*sign; } bool cmp(node x,node y){ return x.id<y.id; } int main(){ n=readint(); rep(i,1,n){ a[i].num=readint(); tot+=a[i].num; a[i].id=i; } sort(a+1,a+1+n); if(tot-a[n].num-a[n-1].num==a[n-1].num)a[n].ok=true,++cnt; tot-=a[n].num; rep(i,1,n-1){ if(tot-a[i].num==a[n].num)a[i].ok=true,++cnt; } printf("%d\n",cnt); rep(i,1,n)if(a[i].ok)printf("%d ",a[i].id); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Good Array</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $$$a=[1, 3, 3, 7]$$$ is good because there is the element $$$a_4=7$$$ which equals to the sum $$$1 + 3 + 3$$$. You are given an array $$$a$$$ consisting of $$$n$$$ integers. Your task is to print all indices $$$j$$$ of this array such that after removing the $$$j$$$-th element from the array it will be good (let's call such indices nice). For example, if $$$a=[8, 3, 5, 2]$$$, the nice indices are $$$1$$$ and $$$4$$$: - if you remove $$$a_1$$$, the array will look like $$$[3, 5, 2]$$$ and it is good; - if you remove $$$a_4$$$, the array will look like $$$[8, 3, 5]$$$ and it is good. You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.</div><h2>Input Format</h2><div>The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — elements of the array $$$a$$$.</div><h2>Output Format</h2><div>In the first line print one integer $$$k$$$ — the number of indices $$$j$$$ of the array $$$a$$$ such that after removing the $$$j$$$-th element from the array it will be good (i.e. print the number of the nice indices). In the second line print $$$k$$$ distinct integers $$$j_1, j_2, \dots, j_k$$$ in any order — nice indices of the array $$$a$$$. If there are no such indices in the array $$$a$$$, just print $$$0$$$ in the first line and leave the second line empty or do not print it at all.</div><h2>Example</h2><h3>Input</h3><div>5 2 5 1 2 2</div><h3>Output</h3><div>3 4 1 5</div><h2>Example</h2><h3>Input</h3><div>4 8 3 5 2</div><h3>Output</h3><div>2 1 4</div><h2>Example</h2><h3>Input</h3><div>5 2 1 2 4 3</div><h3>Output</h3><div>0</div><h2>Note</h2><div>In the first example you can remove any element with the value $$$2$$$ so the array will look like $$$[5, 1, 2, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 1 + 2 + 2$$$). In the second example you can remove $$$8$$$ so the array will look like $$$[3, 5, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 3 + 2$$$). You can also remove $$$2$$$ so the array will look like $$$[8, 3, 5]$$$. The sum of this array is $$$16$$$ and there is an element equals to the sum of remaining elements ($$$8 = 3 + 5$$$). In the third example you cannot make the given array good by removing exactly one element.</div><h2>Editorial</h2><div>The first part: calculate the sum of the whole array: $$$sum = \sum\limits_{i = 1}^n a_i$$$ (be careful, it can be $$$2 \cdot 10^{11}$$$!). The second part: let's maintain an array $$$cnt$$$ of size $$$10^6 + 1$$$ where $$$cnt_i$$$ will be equal to the number of elements in the given array equals to $$$i$$$. The third part: iterate over the array, let the current position be $$$i$$$. Set $$$sum := sum - a_i$$$, make $$$cnt_{a_i} := cnt_{a_i} - 1$$$. If $$$sum$$$ is even, $$$\frac{sum}{2} \le 10^6$$$ and $$$cnt_{\frac{sum}{2}} > 0$$$ then the index $$$i$$$ is nice otherwise it doesn't. And after all make $$$cnt_{a_i} := cnt_{a_i} + 1$$$ and set $$$sum := sum + a_i$$$.</div></body></html>
#include <bits/stdc++.h> #define ll long long using namespace std; inline int read(){ int x=0,f=1;char cc=getchar(); while(cc<'0' || cc>'9') {if(cc=='-') f=-1;cc=getchar();} while(cc>='0' && cc<='9') {x=x*10+cc-'0';cc=getchar();} return x*f; } int n,a[200010],s[200010],v[200010],m; ll t; inline bool cmp(int i,int j){ return a[i]<a[j]; } int main(){ n=read(); for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) s[i]=i; sort(s+1,s+n+1,cmp); t=0; for(int i=1;i<=n;i++) t+=a[i]; for(int i=1;i<n;i++) if(t-a[s[i]]==a[s[n]]<<1) v[s[i]]=1; if(t-a[s[n]]==a[s[n-1]]<<1) v[s[n]]=1; for(int i=1;i<=n;i++) m+=v[i]; printf("%d\n",m); for(int i=1;i<=n;i++) if(v[i]) printf("%d ",i); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Cutting Out</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>You are given an array $$$s$$$ consisting of $$$n$$$ integers. You have to find any array $$$t$$$ of length $$$k$$$ such that you can cut out maximum number of copies of array $$$t$$$ from array $$$s$$$. Cutting out the copy of $$$t$$$ means that for each element $$$t_i$$$ of array $$$t$$$ you have to find $$$t_i$$$ in $$$s$$$ and remove it from $$$s$$$. If for some $$$t_i$$$ you cannot find such element in $$$s$$$, then you cannot cut out one more copy of $$$t$$$. The both arrays can contain duplicate elements. For example, if $$$s = [1, 2, 3, 2, 4, 3, 1]$$$ and $$$k = 3$$$ then one of the possible answers is $$$t = [1, 2, 3]$$$. This array $$$t$$$ can be cut out $$$2$$$ times. - To cut out the first copy of $$$t$$$ you can use the elements $$$[1, \underline{\textbf{2}}, 3, 2, 4, \underline{\textbf{3}}, \underline{\textbf{1}}]$$$ (use the highlighted elements). After cutting out the first copy of $$$t$$$ the array $$$s$$$ can look like $$$[1, 3, 2, 4]$$$. - To cut out the second copy of $$$t$$$ you can use the elements $$$[\underline{\textbf{1}}, \underline{\textbf{3}}, \underline{\textbf{2}}, 4]$$$. After cutting out the second copy of $$$t$$$ the array $$$s$$$ will be $$$[4]$$$. Your task is to find such array $$$t$$$ that you can cut out the copy of $$$t$$$ from $$$s$$$ maximum number of times. If there are multiple answers, you may choose any of them.</div><h2>Input Format</h2><div>The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$s$$$ and the desired number of elements in $$$t$$$, respectively. The second line of the input contains exactly $$$n$$$ integers $$$s_1, s_2, \dots, s_n$$$ ($$$1 \le s_i \le 2 \cdot 10^5$$$).</div><h2>Output Format</h2><div>Print $$$k$$$ integers — the elements of array $$$t$$$ such that you can cut out maximum possible number of copies of this array from $$$s$$$. If there are multiple answers, print any of them. The required array $$$t$$$ can contain duplicate elements. All the elements of $$$t$$$ ($$$t_1, t_2, \dots, t_k$$$) should satisfy the following condition: $$$1 \le t_i \le 2 \cdot 10^5$$$.</div><h2>Example</h2><h3>Input</h3><div>7 3 1 2 3 2 4 3 1</div><h3>Output</h3><div>1 2 3</div><h2>Example</h2><h3>Input</h3><div>10 4 1 3 1 3 10 3 7 7 12 3</div><h3>Output</h3><div>7 3 1 3</div><h2>Example</h2><h3>Input</h3><div>15 2 1 2 1 1 1 2 1 1 2 1 2 1 1 1 1</div><h3>Output</h3><div>1 1</div><h2>Note</h2><div>The first example is described in the problem statement. In the second example the only answer is $$$[7, 3, 1, 3]$$$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $$$2$$$. In the third example the array $$$t$$$ can be cut out $$$5$$$ times.</div><h2>Editorial</h2><div>Let's solve the problem using binary search by the answer. It is easy to see that if we can construct the answer for some number of copies $$$val$$$ then we also can do it for $$$val - 1$$$. The only thing we need is to write the function $$$can(val)$$$ which will say can we cut off $$$val$$$ copies of some array $$$t$$$ from $$$s$$$ or not. Let's imagine $$$val$$$ copies of string $$$t$$$ as a matrix of size $$$val \times k$$$. Obviously, each row of this matrix should be equal to each other row. Let's fill not rows but columns of this matrix. For some element $$$i$$$ of $$$s$$$ we can easy notice that we can take exactly $$$\lfloor\frac{cnt_i}{val}\rfloor$$$ columns containing this element where $$$cnt_i$$$ is the number of such elements in $$$s$$$. So, overall number of columns we can fill in this matrix will be $$$\sum\limits_{i = 1}^{2 \cdot 10^5} \lfloor\frac{cnt_i}{val}\rfloor$$$. If this value is greater than or equal to $$$k$$$ then $$$can(val)$$$ is true otherwise it is false. It is easy to construct the answer using all things we described above. Overall complexity is $$$O(n + |A|\log n)$$$ where $$$|A|$$$ is the size of the alphabet.</div></body></html>
#include <bits/stdc++.h> using namespace std; inline int read(){ int x=0,f=1;char cc=getchar(); while(cc<'0' || cc>'9') {if(cc=='-') f=-1;cc=getchar();} while(cc>='0' && cc<='9') {x=x*10+cc-'0';cc=getchar();} return x*f; } int n,k,m,s[200020],a[200020],l,r; int main(){ n=read();k=read(); for(int i=1;i<=n;i++) s[read()]++; for(int i=1;i<=200000;i++) if(s[i]) m=i; l=1;r=n/k; while(l<r){ int mid=((l+r)>>1)+1; int tmp=0; for(int i=1;i<=m;i++) tmp+=s[i]/mid; if(tmp>=k) l=mid;else r=mid-1; } int tmp=0; for(int i=1;i<=m;i++) { for(int j=1;j<=s[i]/l;j++){ tmp++;printf("%d ",i); if(tmp==k) return 0; } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Farewell Party</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced. Chouti remembered that $$$n$$$ persons took part in that party. To make the party funnier, each person wore one hat among $$$n$$$ kinds of weird hats numbered $$$1, 2, \ldots n$$$. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone. After the party, the $$$i$$$-th person said that there were $$$a_i$$$ persons wearing a hat differing from his own. It has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let $$$b_i$$$ be the number of hat type the $$$i$$$-th person was wearing, Chouti wants you to find any possible $$$b_1, b_2, \ldots, b_n$$$ that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the number of persons in the party. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le n-1$$$), the statements of people.</div><h2>Output Format</h2><div>If there is no solution, print a single line "Impossible". Otherwise, print "Possible" and then $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$). If there are multiple answers, print any of them.</div><h2>Example</h2><h3>Input</h3><div>3 0 0 0</div><h3>Output</h3><div>Possible 1 1 1</div><h2>Example</h2><h3>Input</h3><div>5 3 3 2 2 2</div><h3>Output</h3><div>Possible 1 1 2 2 2</div><h2>Example</h2><h3>Input</h3><div>4 0 1 2 3</div><h3>Output</h3><div>Impossible</div><h2>Note</h2><div>In the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind $$$1$$$. In the answer to the second example, the first and the second person wore the hat with type $$$1$$$ and all other wore a hat of type $$$2$$$. So the first two persons will say there were three persons with hats differing from their own. Similarly, three last persons will say there were two persons wearing a hat different from their own. In the third example, it can be shown that no solution exists. In the first and the second example, other possible configurations are possible.</div><h2>Editorial</h2><div>let $$$b_i=n-a_i$$$ represent the number of people wearing the same type of hat of $$$i$$$-th person. It's clear that the number of people having the same $$$b_i$$$ should be a multiple of $$$b_i$$$. This is also sufficient, just give $$$b_i$$$ people a new hat color, one bunch at a time.</div></body></html>
#include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<ctime> #include<bitset> #include<cstdio> #include<cstdlib> #include<cstring> #include<complex> #include<iostream> #include<algorithm> #define N 100001 #define LL long long #define LOG(x) cerr<<#x<<" = "<<x<<endl #define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v #define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) char ch;bool fs;void re(int& x) { while(ch=getchar(),ch<33); if(ch=='-')fs=1,x=0;else fs=0,x=ch-48; while(ch=getchar(),ch>33)x=x*10+ch-48; if(fs)x=-x; } using namespace std; int n,ctt,a[N],cnt[N],sy[N]; int main() { re(n); for(int i=1;i<=n;++i) re(a[i]),a[i]=n-a[i],++cnt[a[i]]; bool ok=1; for(int i=1;i<=n;++i) if(cnt[i]%i) ok=0; if(!ok)puts("Impossible"); else { puts("Possible"); for(int i=1;i<=n;++i) { if(cnt[a[i]]%a[i]==0) sy[a[i]]=++ctt; --cnt[a[i]]; printf("%d ",sy[a[i]]); } } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Missing Numbers</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Chouti is working on a strange math problem. There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square). Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him. The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.</div><h2>Input Format</h2><div>The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).</div><h2>Output Format</h2><div>If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5 11 44</div><h3>Output</h3><div>Yes 4 5 16 11 64 44</div><h2>Example</h2><h3>Input</h3><div>2 9900</div><h3>Output</h3><div>Yes 100 9900</div><h2>Example</h2><h3>Input</h3><div>6 314 1592 6535</div><h3>Output</h3><div>No</div><h2>Note</h2><div>In the first example - $$$x_1=4$$$ - $$$x_1+x_2=9$$$ - $$$x_1+x_2+x_3=25$$$ - $$$x_1+x_2+x_3+x_4=36$$$ - $$$x_1+x_2+x_3+x_4+x_5=100$$$ - $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer. In the third example, it is possible to show, that no such sequence exists.</div></body></html>
#include <bits/stdc++.h> using namespace std; #include<stdio.h> #include<vector> #include<algorithm> #include<math.h> long long a[100001]; int main(){ int n; scanf("%d",&n); int i,j,k; int m=n/2; for(i=1;i<n;i+=2){ scanf("%lld",&a[i]); } long long cur=100000000000001LL; for(i=n-1;i>=1;i-=2){ bool ok=false; for(k=2-a[i]%2;(long long)(k)*k<=a[i];k+=2){ if(a[i]%k) continue; long long miden=a[i]/k; if((miden-k)%2) continue; long long upper=(a[i]/k)+(k-1); if(upper + 2 < cur){ ok=true; long long lower=(a[i]/k)-(k-1); a[i+1] = ((upper+2) + (cur-2)) * ((cur - (upper+2))/2) / 2; cur = lower; break; } } if(!ok){ break; } } a[0] = (1 + (cur-2)) * ((cur - 1)/2) / 2; if(a[0]>0&&i<1){ puts("Yes"); for(i=0;i<n;i++){ printf("%lld%c",a[i]," \n"[i==n-1]); } } else { puts("No"); } }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Missing Numbers</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Chouti is working on a strange math problem. There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square). Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him. The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.</div><h2>Input Format</h2><div>The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).</div><h2>Output Format</h2><div>If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.</div><h2>Example</h2><h3>Input</h3><div>6 5 11 44</div><h3>Output</h3><div>Yes 4 5 16 11 64 44</div><h2>Example</h2><h3>Input</h3><div>2 9900</div><h3>Output</h3><div>Yes 100 9900</div><h2>Example</h2><h3>Input</h3><div>6 314 1592 6535</div><h3>Output</h3><div>No</div><h2>Note</h2><div>In the first example - $$$x_1=4$$$ - $$$x_1+x_2=9$$$ - $$$x_1+x_2+x_3=25$$$ - $$$x_1+x_2+x_3+x_4=36$$$ - $$$x_1+x_2+x_3+x_4+x_5=100$$$ - $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer. In the third example, it is possible to show, that no such sequence exists.</div></body></html>
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) int const N = 100000, M = 3 * N; ll x[N]; void no() { printf("No\n"); exit(0); } pair<bool, int> sq(ll x){ int z = sqrt(x) + 0.5; if ((ll)z * z != x)return make_pair(false, 0); return make_pair(true, z); } int main(){ int n; scanf("%d", &n); for (int i = 1; i < n; i += 2)scanf("%lld", x + i); int a = 1; ll s = 0; for (int i = 0; i < n; i += 2){ while (a < M){ ll cr = (ll)a * a - s; pair<bool, int> t = sq(s + cr + x[i + 1]); if (t.first){ x[i] = cr; a = t.second + 1; s += x[i] + x[i + 1]; break; } ++a; } if (a == M)no(); } printf("Yes\n%lld", x[0]); f(i, 1, n)printf(" %lld", x[i]); printf("\n"); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and another construction problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Given an integer $$$x$$$, find 2 integers $$$a$$$ and $$$b$$$ such that: - $$$1 \le a,b \le x$$$ - $$$b$$$ divides $$$a$$$ ($$$a$$$ is divisible by $$$b$$$). - $$$a \cdot b>x$$$. - $$$\frac{a}{b}<x$$$.</div><h2>Input Format</h2><div>The only line contains the integer $$$x$$$ $$$(1 \le x \le 100)$$$.</div><h2>Output Format</h2><div>You should output two integers $$$a$$$ and $$$b$$$, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (without quotes).</div><h2>Example</h2><h3>Input</h3><div>10</div><h3>Output</h3><div>6 3</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>-1</div></body></html>
#include <iostream> #include <stdio.h> #include <cmath> #include <vector> #include <string> #include <functional> #include <set> #include <cstdlib> #include <map> #include <cctype> #include <algorithm> #define pii pair<int, int> #define ll long long #define pis pair<int, string> #define mp make_pair const ll MOD = 998244353; const int INF = 1 << 30; using namespace std; int main() { //freopen("input.txt", "r", stdin); int n; scanf("%d", &n); if (n == 1) { printf("-1"); } else { printf("%d %d", n, n); } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and another construction problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Given an integer $$$x$$$, find 2 integers $$$a$$$ and $$$b$$$ such that: - $$$1 \le a,b \le x$$$ - $$$b$$$ divides $$$a$$$ ($$$a$$$ is divisible by $$$b$$$). - $$$a \cdot b>x$$$. - $$$\frac{a}{b}<x$$$.</div><h2>Input Format</h2><div>The only line contains the integer $$$x$$$ $$$(1 \le x \le 100)$$$.</div><h2>Output Format</h2><div>You should output two integers $$$a$$$ and $$$b$$$, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (without quotes).</div><h2>Example</h2><h3>Input</h3><div>10</div><h3>Output</h3><div>6 3</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>-1</div></body></html>
//WikkieRitz #include <bits/stdc++.h> typedef long long int var; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int x; cin>>x; if(x==1) cout<<-1; else cout<<x<<" "<<x; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and another construction problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Given an integer $$$x$$$, find 2 integers $$$a$$$ and $$$b$$$ such that: - $$$1 \le a,b \le x$$$ - $$$b$$$ divides $$$a$$$ ($$$a$$$ is divisible by $$$b$$$). - $$$a \cdot b>x$$$. - $$$\frac{a}{b}<x$$$.</div><h2>Input Format</h2><div>The only line contains the integer $$$x$$$ $$$(1 \le x \le 100)$$$.</div><h2>Output Format</h2><div>You should output two integers $$$a$$$ and $$$b$$$, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (without quotes).</div><h2>Example</h2><h3>Input</h3><div>10</div><h3>Output</h3><div>6 3</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>-1</div></body></html>
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if (n==1) cout<<"-1"; else { int lan= n/2; cout<<lan*2<<" 2"; } return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and another construction problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Given an integer $$$x$$$, find 2 integers $$$a$$$ and $$$b$$$ such that: - $$$1 \le a,b \le x$$$ - $$$b$$$ divides $$$a$$$ ($$$a$$$ is divisible by $$$b$$$). - $$$a \cdot b>x$$$. - $$$\frac{a}{b}<x$$$.</div><h2>Input Format</h2><div>The only line contains the integer $$$x$$$ $$$(1 \le x \le 100)$$$.</div><h2>Output Format</h2><div>You should output two integers $$$a$$$ and $$$b$$$, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (without quotes).</div><h2>Example</h2><h3>Input</h3><div>10</div><h3>Output</h3><div>6 3</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>-1</div></body></html>
#include <iostream> using namespace std; int main() { int x; cin>>x; if (x<2) { cout<<-1; return 0; } cout<<x<<" "<<x; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and another construction problem</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>Given an integer $$$x$$$, find 2 integers $$$a$$$ and $$$b$$$ such that: - $$$1 \le a,b \le x$$$ - $$$b$$$ divides $$$a$$$ ($$$a$$$ is divisible by $$$b$$$). - $$$a \cdot b>x$$$. - $$$\frac{a}{b}<x$$$.</div><h2>Input Format</h2><div>The only line contains the integer $$$x$$$ $$$(1 \le x \le 100)$$$.</div><h2>Output Format</h2><div>You should output two integers $$$a$$$ and $$$b$$$, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (without quotes).</div><h2>Example</h2><h3>Input</h3><div>10</div><h3>Output</h3><div>6 3</div><h2>Example</h2><h3>Input</h3><div>1</div><h3>Output</h3><div>-1</div></body></html>
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> #include <string.h> #include <math.h> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll long long #define ull unsigned long long #define EPS 1e-8 #define IOS ios_base::sync_with_stdio(0); cin.tie(0) using namespace std; typedef pair<int,int> pii; typedef pair<long long,long long> pll; const double pi = acos(-1.0); inline ll read() { ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return f*x; } inline void write(ll a){ if (a<0) putchar('-'),a=-a; if (a>=10) write(a/10); putchar(a%10+'0'); } int main() { #ifdef TEST freopen("input.txt","r",stdin); #endif int x; cin>>x; if(x==1){ cout<<-1<<endl; return 0; } if(x&1){ x--; } cout<<x<<" "<<2<<endl; return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and a 2-operation task</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it: - choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(0 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 \le j \le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$. - choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(1 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j \% x$$$ for all $$$(1 \le j \le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations?</div><h2>Input Format</h2><div>The first line contains an integer $$$n$$$ $$$(1 \le n \le 2000)$$$, the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\dots$$$, $$$a_n$$$ $$$(0 \le a_i \le 10^5)$$$, the elements of the array $$$a$$$.</div><h2>Output Format</h2><div>On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "$$$1$$$ $$$i$$$ $$$x$$$"; to print a modding operation, use the format "$$$2$$$ $$$i$$$ $$$x$$$". If $$$i$$$ or $$$x$$$ don't satisfy the limitations above, or you use more than $$$n+1$$$ operations, you'll get wrong answer verdict.</div><h2>Example</h2><h3>Input</h3><div>3 1 2 3</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 7 6 3</div><h3>Output</h3><div>2 1 1 1 2 2 4</div><h2>Note</h2><div>In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes $$$[8,6,3]$$$. In the second step: the array becomes $$$[0,2,3]$$$.</div></body></html>
#include <bits/stdc++.h> using namespace std; int32_t main() { int n; scanf("%d", &n); int start = n * 10; printf("%d\n2 %d 1\n1 %d %d\n", n + 1, n, n, start--); for (int i = 0; i < n - 1; ++i) printf("2 %d %d\n", i + 1, start--); }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and a 2-operation task</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it: - choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(0 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 \le j \le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$. - choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(1 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j \% x$$$ for all $$$(1 \le j \le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations?</div><h2>Input Format</h2><div>The first line contains an integer $$$n$$$ $$$(1 \le n \le 2000)$$$, the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\dots$$$, $$$a_n$$$ $$$(0 \le a_i \le 10^5)$$$, the elements of the array $$$a$$$.</div><h2>Output Format</h2><div>On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "$$$1$$$ $$$i$$$ $$$x$$$"; to print a modding operation, use the format "$$$2$$$ $$$i$$$ $$$x$$$". If $$$i$$$ or $$$x$$$ don't satisfy the limitations above, or you use more than $$$n+1$$$ operations, you'll get wrong answer verdict.</div><h2>Example</h2><h3>Input</h3><div>3 1 2 3</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 7 6 3</div><h3>Output</h3><div>2 1 1 1 2 2 4</div><h2>Note</h2><div>In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes $$$[8,6,3]$$$. In the second step: the array becomes $$$[0,2,3]$$$.</div></body></html>
#include<bits/stdc++.h> #define _CRT_SECURE_NO_WARNINGS #define first fi #define second se //vector<pair<int, pair<int, int> > > vec; using namespace std; int n, sum, di, sh = 200000; long long a[2007]; bool yes = true; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; if (a[i] <= a[i - 1]) yes = false; } if (yes) { cout << "0\n"; } else { cout << n + 1 << "\n"; //vec.push_back(make_pair(1, make_pair(n - 1, sh))); cout << 1 << " " << n << " " << sh << "\n"; for (int i = 0; i < n; i++) { //vec.push_back(make_pair(2, make_pair(i, a[i] + sh - i))); cout << 2 << " " << i + 1 << " " << a[i] + sh - i << "\n"; } } //system("pause"); return 0; }
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Ehab and a 2-operation task</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it: - choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(0 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 \le j \le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$. - choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(1 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j \% x$$$ for all $$$(1 \le j \le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations?</div><h2>Input Format</h2><div>The first line contains an integer $$$n$$$ $$$(1 \le n \le 2000)$$$, the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\dots$$$, $$$a_n$$$ $$$(0 \le a_i \le 10^5)$$$, the elements of the array $$$a$$$.</div><h2>Output Format</h2><div>On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "$$$1$$$ $$$i$$$ $$$x$$$"; to print a modding operation, use the format "$$$2$$$ $$$i$$$ $$$x$$$". If $$$i$$$ or $$$x$$$ don't satisfy the limitations above, or you use more than $$$n+1$$$ operations, you'll get wrong answer verdict.</div><h2>Example</h2><h3>Input</h3><div>3 1 2 3</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 7 6 3</div><h3>Output</h3><div>2 1 1 1 2 2 4</div><h2>Note</h2><div>In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes $$$[8,6,3]$$$. In the second step: the array becomes $$$[0,2,3]$$$.</div></body></html>
#include <bits/stdc++.h> #define fto(i,a,b) for(int i=a; i<=b; ++i) #define fdto(i,a,b) for(int i=a; i>=b; --i) using namespace std; const int oo = 1e9+7; int n, a[2001]; int res[2001], res2[2001]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL); //("C.inp", "r", stdin); //freopen("C.out", "w", stdout); cin >> n; fto(i,1,n) cin >> a[i]; int tra=0, dem=0, tra1=0; fdto(i,n,1) { a[i]+=tra; tra1=(i-1)-(a[i]%n); if (tra1<0) tra1=n+tra1; if (tra1==0) continue; tra+=tra1; res[++dem]=tra1; res2[dem]=i; } cout << dem+1 << '\n'; fto(i,1,dem) cout << 1 << " " << res2[i] << " " << res[i] << '\n'; cout << 2 << " " << n << " " << n; return 0; }