SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS

Similar documents
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

A First Program - Greeting.cpp

CS222 Homework 4(Solution)

Tenka1 Programmer Contest/Tenka1 Programmer Beginner Contest 2018

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Problem Solving: Storyboards for User Interaction

Chapter Four: Loops II

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Computer Programming : C++

Review Questions for Final Exam

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

The following expression causes a divide by zero error:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

REVIEW. The C++ Programming Language. CS 151 Review #2

To become familiar with array manipulation, searching, and sorting.

CS2141 Software Development using C/C++ C++ Basics

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Lab Instructor : Jean Lai

Scientific Computing

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Describing and Implementing Algorithms

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Bulgarian Math Olympiads with a Challenge Twist

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

EP578 Computing for Physicists

University of Dublin

c++ Solutions eedsohag.epizy.com Ahmed Ali

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

2 nd Week Lecture Notes

Chapter 3 Problem Solving and the Computer

Maciej Sobieraj. Lecture 1

CSCE Practice Midterm. Data Types

Lecture 7: General Loops (Chapter 7)

EP241 Computing Programming

Local and Global Variables

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

For 10 possible values of N, values of a, b, and c will be as under

Goals of this Lecture

CSCE Practice Midterm. Data Types

Why Is Repetition Needed?

Introduction to C ++

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming. C++ Basics

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Algorithm Analysis. Algorithm Analysis 1

Getting started with C++ (Part 2)

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Review Questions for Final Exam KEY

Decision Making -Branching. Class Incharge: S. Sasirekha

Week 1 Questions Question Options Answer & Explanation A. 10 B. 20 C. 21 D. 11. A. 97 B. 98 C. 99 D. a

WARM UP LESSONS BARE BASICS

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

DELHI PUBLIC SCHOOL TAPI

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

ARC093 / ABC092. wo01. min(a, B) + min(c, D) C++ #include<cstdio> #include<algorithm> using namespace std;

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

A Freshman C++ Programming Course

Learning to Program with Haiku

Programming Language. Functions. Eng. Anis Nazer First Semester

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

COT 3100 Spring 2010 Midterm 2

EE 109 Lab 8a Conversion Experience

A SHORT COURSE ON C++

C++ Programming: From Problem Analysis to Program Design, Third Edition

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;

do { statements } while (condition);

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Make sure the version number is marked on your scantron sheet. This is Version 1

Counting. Andreas Klappenecker

CSCE 110 PROGRAMMING FUNDAMENTALS

Input And Output of C++

VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Apllications. March 03, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications.

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Programming Language A

Name SECTION: 12:45 2:20. True or False (12 Points)

REPETITION CONTROL STRUCTURE LOGO

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Increment and the While. Class 15

Introduction to the C++ Programming Language

Review Questions II KEY

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

C++ Quick Reference. switch Statements

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

Recursion. Example R1

1 Unit 8 'for' Loops

Transcription:

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS July 8 13, 2008 Shumen, Bulgaria TASKS AND SOLUTIONS Day 1 Task 1. TOWERS OF COINS Statement Asen and Boyan are playing the following game. They choose two different positive integers K and L, and start the game with a tower of N coins. Asen always plays first, Boyan second, after that Asen again, then Boyan, and so on. The boy in turn can take 1, K or L coins from the tower. The winner is the boy, who takes the last coin (or coins). After a long, long playing, Asen realizes that there are cases in which he could win, no matter how Boyan plays. And in all other cases Boyan being careful can win, no matter how Asen plays. So, before the start of the game Asen is eager to know what game case they have. Write a program coins which help Asen to predict the game result for given K, L and N. The input describes m games. The first line of the standard input contains the integers K, L and m, 1 < K < L < 10, 3 < m < 50. The second line contains m integers N 1, N 2,, N m, 1 N i 1 000 000, i = 1, 2,..., m, representing the number of coins in each of the m towers. The standard output contains a string of length m composed of letters A and B. If Asen wins the i th game (no matter how the opponent plays), the i th letter of the string has to be A. When Boyan wins the i th game (no matter how Asen plays), the i th letter of the string has to be B.

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 2 Example 2 3 5 3 12 113 25714 88888 ABAAB Solution As it was mentioned in the statement of the problem in some games Asen could win regardless how Boyan is playing and the opposite in some games, if Boyan is playing optimally, Asen could not win. Let us denote with N any possible number of coins in the tower N is called position of the game. It is important to realize that for given L and K, each position is either winning or loosing. The goal of the task is to decide if the position is winning or loosing for each of m given positions N 1, N 2,, N m. Let us denote with N max the maximum of the numbers N 1, N 2,, N m. The value N max is found while reading the numbers N 1, N 2,, N m in the array in []. So we have to decide for each integer i = 0, 1,..., Nmax if the corresponding position is winning or loosing. For that purpose we will fill an array win[] with zeros and ones, where the value 0 of win[ i ] designates the loosing position i and the value 1 designates the winning position i. Finaly, the program has to print for each number N the letter A when win[ N ] = 1 and letter B when win[ N i ] = 0. How to find win[ i ]? Obviously win [0] = 0. Suppose that we calculate win [0], win[1],..., win[ i 1]. When the player is in position i he has tree possibilities take 1 coin, take K coins or take L coins, i.e. to transform the game to position i 1, i K or i L. If at least one of the positions i 1, i K or i L is loosing (i.e. win[ i 1] & & win[ i K] & & win[ i L] == 0 ) then the player in position i has a move that leads the game in a loosing position and so the position i is winning. So win[ i ] = 1. If each of the positions i 1, i K or i L is winning (i.e. win[ i 1] & & win[ i K ] & & win[ i L] == 1 ) then the player in position i has no move that leads the game in a loosing position and so the position i is loosing. So win[ i ] = 0. Notice that it is possible to have i K or i L less than 0. This means that the corresponding move does not exist and has no influence to the results. Thus each negative position can be considered as winning! i i

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 3 Realization #include <stdio.h> char win[1000001]; int in[11]; int main() { int K,L,M,N,Nmax=0,i; int one,ka,el; scanf("%d %d %d",&k,&l,&m); // reading input and finding Nmax for(i=1;i<=m;i++) { scanf("%d",&in[i]); if(nmax<in[i]) Nmax=in[i]; win[0]=0; // position 0 is loosing for(i=1;i<=nmax;i++) { one=win[i-1]; if(i-k>=0) ka=win[i-k]; else ka=1; if(i-l>=0) el=win[i-l]; else el=1; if(one&&ka&&el) win[i]=0; else win[i]=1; for(i=1;i<=m;i++) if(win[in[i]]) printf("a"); else printf("b"); printf("\n"); Task 2. CLOSEST Statement Consider two n-digit positive decimal integers A and B with no leading zeroes. We need to find the two closest to A n-digit numbers (the first one greater or equal to A, the other strictly less than A), with decimal writings containing all the digits of B in some order. For example if A = 3022 and B = 1232, using B s digits we can obtain the following 4-digit numbers: 1223, 1232, 1322, 2123, 2132, 2213, 2231, 2312, 2321, 3122, 3212 and 3221. The least number greater or equal to A

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 4 obtained by B s digits is 3122, and the biggest one, strictly less then A is 2321. If A = 1232, and B = 3022, the possible numbers are 2023, 2032, 2203, 2230, 2302, 2320, 3022, 3202 and 3220. The least number greater or equal to A obtained by B s digits is 2023, and there is no number less than A. Write a program closest to find these closest to A numbers for given A and B, or to determine that one of them does not exist. Two lines are read from the standard input, each of them containing an n- digit positive integer with no leading zeroes, with A read from the first, and B read from the second line (1 n 60 ). Write to the standard output: Line 1: the least n-digit number with no leading zeroes, not less than A, containing all the digits of B in some order. If such number does not exist, the output should be 0. Line 2: the biggest n-digit number with no leading zeroes, less than A, containing all the digits of B in some order. If such number does not exist, the output should be 0. Examples 3075 3000203 6604 4562454 4066 4244556 0 2655444 Solution The exhausting search through all n-digit numbers after A (and then before A, too) and the check, if they consist only of B s digits, will be good enough just for the first test (and for another one if organized with arrays/strings). The exhaustion of all B s permutations and determining the closest neighbors of A will suffice for three more tests. Partial considerations can solve some tests, but not all of them. For a full solution, it can be pointed out that the algorithm for determining a non-bigger neighbor will not be principally different from the one finding the non-less ( upper ) neighbor. If we subtract 1 from A, the second part of the problem becomes analogical to the first one: searching for the nonbigger neighbor of A-1. We only have to add a rule to eliminate pseudo

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 5 solutions with a leading zero. So we can concentrate on the first problem determination of the upper neighbor. We apply the following algorithm: If all digits of B are less than the first A s digit, there s no solution; If there is a digit in B equal to the first digit in A, we can try to reduce the problem to a less dimension: we rub out this digit from B, putting it as first in the result, delete the first digit of A and solve the same problem for (n 1)-digit numbers. If there is a solution for the reduced problem, this is the final result, preceded by the found digit. If we don t get a solution applying the previous step, the solution is found by taking the least digit in B bigger than the considered digit in A for the next digit in the result, and sorting in ascending order the digits of B which are not used yet. If such digit is not found, there is no solution. Realization #include <iostream> using namespace std; int const N=64; int a[n],r[n],st[10],n; void strtoarr(const char *s,int *a,int *n) {*n=0; while (*s) a[(*n)++]=*s++-'0'; void makestat(int n,const int *a,int *st) {memset(st,0,10*sizeof(int)); for (int i=0;i<n;i++) st[a[i]]++; void show(int n,int *a) {for (int i=0;i<n;i++) cout<<a[i]; cout<<endl; bool findhi(int p) {int d,i; if (p==n) return true; for (d=a[p]; d<=9; d++) if (st[d]) break; if (d>9) return false; if (d==a[p]){r[p]=d; st[d]--; if (findhi(p+1)) return true; st[d]++;

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 6 for (d=a[p]+1; d<=9; d++) if (st[d]) break; if (d>9) return false; r[p++]=d; st[d]--; for (d=0;d<=9;d++) for (i=0;i<st[d];i++) r[p++]=d; return true; bool findlo(int p) {int d,i; if (p==n) return true; for (d=a[p]; d>=0; d--) if (st[d]) break; if (d<0) return false; if (!p &&!d){do d++; while (!st[d]); if (d>a[p]) return false; if (d==a[p]){r[p]=d; st[d]--; if (findlo(p+1)) return true; st[d]++; for (d=a[p]-1; d>=0; d--) if (st[d]) break; if (d<0) return false; r[p++]=d; st[d]--; for (d=9;d>=0;d--) for (i=0;i<st[d];i++) r[p++]=d; return true; bool Dec(int n, int *a) {int i=n-1; while (!a[i]) a[i--]=9; a[i]--; return (bool)*a; int main(void) {char buf[64]; int b[64]; cin>>buf; strtoarr(buf,a,&n); cin>>buf;

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 7 strtoarr(buf,b,&n); makestat(n,b,st); if (findhi(0)) show(n,r); else cout<<"0\n"; makestat(n,b,st); if (!Dec(n,a)) cout<<"0\n"; else if (findlo(0)) show(n,r); else cout<<"0\n"; return 0; Task 3. RECTANGLES Statement Given are n rectangles, numbered from 1 to n. We place them tightly on the axis OX, from left to right, according to rectangles numbers. Each rectangle stays on the axis OX either by its shorter or by its longer side (see the picture below). Compute the length of the upper envelop line, i.e. perimeter s length of the obtained figure minus the length of the left, right and bottom straight line segments of the picture. Write program rec to find the maximum possible length of the upper envelop line. On the first line of the standard input, the value of n is written. On each of the next n lines, two integers are given a i and b i the side lengths of the i th rectangle. On a line of the standard output, your program should write the result as a positive integer. Constraints: 0 < n < 1000; 0 < a < b < 1000, for each i = 1, 2,..., n. Example 5 2 5 3 8 1 10 7 14 2 5 68 i i

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 8 Explanation: A configuration, that yields the maximum length of the upper envelop line, is presented on the picture. The upper envelop line consists of segments DC, CG, GF, FJ, JI, IM, ML, LP, and PO. The total length is 5 + 6 + 3 + 7 + 10 + 13 + 7 + 12 + 5 = 68. Solution Dynamic programming approach: Let us denote by f ( i ) the maximal length of the upper envelop line for the task involving the first i rectangles only, the last of them being placed on the base line OX with its short side. Analogously, let us denote by g( i ) the maximal length of the upper envelop line for the task involving the first i rectangles with the last of them placed on its long side. We have f (1) = a1 and g(1) = b1. If we have already computed values f ( i ) and g( i ) for some i, 1 i < n, we may compute: f ( i + 1) = a + max f ( i) + b b, g( i) + a b, i { i i+ 1 i i+ 1 g( i + 1) = bi + max { f ( i) + bi ai + 1, g( i) + ai ai + 1. f g ( f g ) ( f ( n), g( n )). The solution is equal to the largest of f ( n ) and g( n ). Iteratively, we obtain values of pairs ( (1), (1) ), (2), (2),..., In the program implementation below, the values of f ( i ) and g( i ) are stored as array s elements t[ i ][0] and t[ i ][1]. Realization #include<iostream> using namespace std; const int N=1024; int n; int a[n], b[n], t[n][2]; int main() { cin >> n; for(int i=1;i<=n;i++) cin >> a[i] >> b[i]; t[1][0]=a[1]; t[1][1]=b[1]; for(int i=2;i<=n;i++) { t[i][0]=a[i]+ max(t[i-1][0]+abs(b[i-1]-b[i]),t[i- 1][1]+abs(a[i-1]-b[i]));

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 9 t[i][1]=b[i]+ max(t[i-1][0]+abs(b[i-1]-a[i]),t[i- 1][1]+abs(a[i-1]-a[i])); cout << max(t[n][0], t[n][1]) << endl; Day 2 Task 1. JUMPS Statement A bunny has to pass n meters with jumps of lengths 3, 2 or 1 meters. In how many different ways this can be done, if the lengths of successive jumps form a non-increasing sequence? Write program jumps, which computes the number we are looking for. The value of n should be entered from the standard input (1 n 10 9 ). The program should write to the standard output one integer, equal to the remainder of the found number, divided by 1000000. Remark: In 50% of test cases, n 10 5. Example 6 7 Explanation: The number of different ways is 7, and its remainder modulo 1000000 is also 7. The different sequences of jumps are: 1) 3+3 2) 3+2+1 3) 3+1+1+1 4) 2+2+2 5) 2+2+1+1 6) 2+1+1+1+1 7) 1+1+1+1+1+1

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 10 Solution A Denote by x, y, z the number of jumps of length 3, 2, and 1, respectively. In this way we have to compute the number of different solution in nonnegative integers of the equation 3x + 2y + z = n. It is clear that x n/3, and for fixed value of x the possible values of y are 0, 1,, (n 3x)/2. int cnt=0; for(int x=0; x<= n/3; x++) for(int y=0; y<=(n-3*x)/2; y++) { cnt++; if(cnt>=m) cnt = cnt - M; cout << cnt << endl; Solution B We may remove the inner loop in Solution A. For fixed value of x there are exactly 1 + ( n 3 x) / 2 possibilities for y. In addition we may avoid multiplication, just denote a = 3x. int cnt=0; for(int a=0; a<=n; a=a+3) { cnt = cnt + (n-a)/2 + 1; if(cnt>m) cnt = cnt%m; cout << cnt << endl; Solution C Using Solution A or Solution B we may do some observations. Consider the computed numbers for n = 1, 7, 13, 19,, 6k + 1, n 1 7 13 19 25 cnt 1 8 = 1 + 7 21 = 8 + 13 40 = 21 + 19 65 = 40 + 25 The conjecture we could make is that cnt(x) = cnt(x 6) + x. It turns out that the same relationship holds for any value of x 6. The assumption that cnt(0) = 1 is quite natural.

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 11 int x = n%6; int cnt=0; if(x==0) cnt=1; else cnt=x; while(x<n) { x = x + 6; cnt = cnt + x; if(cnt >= M) cnt = cnt%m; cout << cnt << endl; Solution D Consider the example in Solution C: cnt(25) = 1 + 7 + 13 + 19 + 25. More generally cnt(6k + 1) = 1 + 7 + 13 + + (6k + 1). The increment of the sequence is 6, and the number of terms in the sum is k + 1. We may apply the Gauss trick for computing such a sum. For example: S = 1 + 7 + 13 + 19 + 25 S = 25 + 19 + 13 + 7 + 1 2 S = 26 + 26 + 26 + 26 + 26 = 26 * 5 S = 26 * 5 / 2 = 65. long long cnt = ((n%6 + n)/2%m * ((1 + n/6)%m)); if(n%6==0) cnt++; cout << cnt%m << endl; Similarly to Solution C a small correction has to be made for the case when n is divisible by 6.

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 12 Task 2. SQUARES Statement Let R be a rectangle with integer side lengths. The rectangle is divided into f R the unit squares. Considering one of the diagonals, we denote by ( ) number of squares which have a common interior point with it. For example, if the side lengths of R are 2 and 4 then f ( R ) = 4. Write a program squ to find out the number of all different rectangles R for which f ( R) = N. Two rectangles with sides a b and b a are not different. In a single line of the standard input the integer N (0 < N < 10 6 ) is given. The only line of the standard output should contain an integer the calculated number of rectangles. Remark: In 50% of test cases, N 2000. Example 4 4 Explanation: The different rectangles R for which f ( R ) = 4 are 4: with side lengths 1 and 4, 2 and 3, 2 and 4, 4 and 4. Solution Let the lengths of sides of the rectangle R be a and b and d gcd ( a, b) =. Then f ( R) = a + b d. So, we search the number of integer solutions of the equation a + b d = N, for which a b. Let a = x. d, b = y. d. Then 1 x y and ( ) Write the equation in the form ( x + y 1 ) d = N. gcd x, y = 1. Hence d is a divisor of N. Let N = m. d. Consequently x + y = m + 1.

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 13 Now for each m, which is a divisor of N we have to find the number of integer solutions of the equation x + y = m + 1, satisfying 1 x y and gcd ( x, y ) = 1. Note that ( ) gcd x, y = gcd( x, m + 1 x) = gcd( x, m + 1). Realization #include<iostream> using namespace std; int nod(int a, int b) { int r = a%b; while(r > 0) { a = b; b = r; r = a%b; return b; int main() { int n; cin >> n; int s = 0; for(int m = 1; m*m <= n; m++) { if (n % m == 0) if (m*m == n) { for(int x = 1; x <= (m + 1)/2; x++) if (nod(x, m + 1) == 1) s++; else { for(int x = 1; x <= (m + 1)/2; x++) if (nod(x, m + 1) == 1) s++; for(int x = 1; x <= (n/m + 1)/2; x++) if (nod(x, n/m + 1) == 1) s++; cout << s << endl; return 0;

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 14 Task 3. SUMX Statement Consider a set of n distinct positive integers a 1, a 2,, a n, having values between 1 and 1000000 and an integer x. Write a program sumx to determine the number of pairs (a i, a j ), where 1 i < j n and a i + a j = x. The first line of the standard input contains the integer n (1 n 100000). The second line contains n integers the elements of the set. On the third line the integer x is given (1 x 2000000). The program should output on a single line of the standard output an integer the calculated number of pairs. Remark: In 50% of test cases, n 1000. Example 9 5 12 7 10 9 1 2 3 11 13 3 Explanation: The different pairs with sum 13 are: (12, 1), (10, 3) and (2, 11). Solution Assume the sequence is sorted in an increasing order. Consider a 1 and a n and let y = a 1 + a n. If y > x, then a n cannot be part of the solution because an + ai > x for all i. Therefore, we eliminate a n from consideration and continue with a n 1. If y < x we eliminate a 1 according to the similar argument. If y = x, then the first pair is found and we continue with a 2 and a n 1. According to stated above we have to sort the sequence first. Realization #include <iostream> #include <cstdio>

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS 15 #include <vector> using namespace std; int main () { int n, b, x, c=0; vector <int> a; scanf ("%d", &n); for (int i = 0; i < n; i++){ scanf ("%d", &b); a.push_back (b); scanf ("%d", &x); sort(a.begin(),a.end()); int k=0, m=n-1; while (k<m){ if (a[k]+a[m]==x){c++; k++; m--; else if (a[k]+a[m]<x)k++; else if (a[k]+a[m]>x)m--; printf("%d\n", c); return 0;