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;
}
|
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 1