Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x = 0;
cin >> x;
int array[x];
for (int i = 0; i < x; i++) {
cin >> array[i];
}
int one = 0, two = 0, three = 0;
for (int j = 0; j < x; j++) {
if (array[j] == 1) {
one++;
}
if (array[j] == 2) {
two++;
}
if (array[j] == 3) {
three++;
}
}
if (one > two && one > three) {
cout << x - one;
} else if (two > one && two > three) {
cout << x - two;
} else if (three > two && three > one) {
cout << x - three;
} else if (one == two) {
cout << x - two;
} else if (two == three) {
cout << x - three;
} else if (one == three) {
cout << x - one;
}
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const int MAX = numeric_limits<int>::max();
int main() {
int n;
scanf("%d", &n);
vector<int> v(3, 0);
for (int i = 0; i < n; ++i) {
int t;
scanf("%d", &t);
++v[t - 1];
}
int m = 0;
for (int i = 0; i < 3; ++i) {
if (v[i] > m) m = v[i];
}
printf("%d\n", v[0] + v[1] + v[2] - m);
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.HashMap;
import java.util.Scanner;
public class CF52A {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int max_freq=0;
HashMap<Integer,Integer> hm=new HashMap<>();
for(int i=0;i<n;i++){
int num=s.nextInt();
hm.put(num,hm.getOrDefault(num,0)+1);
max_freq=Math.max(max_freq,hm.get(num));
}
System.out.println(n-max_freq);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | a=input()
b=raw_input()
string = map(int, b.split())
d={}
for i in string:
if i in d:
d[i]+=1
else:
d[i]=1
print a-d[max(d,key=d.get)]
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(raw_input())
a = map(int, raw_input().split())
print min(a.count(1)+a.count(2), a.count(3)+a.count(1), a.count(2)+a.count(3)) | PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=input()
L=input()
o=L.count('1')
t=L.count('2')
th=L.count('3')
L=[o,t,th]
L.sort()
print(L[0]+L[1])
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(input())
a = list(map(int, input().split()))
print(n-max(a.count(1), a.count(2), a.count(3))) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.*;
public class A52
{
public static void main(String aa[])
{
Scanner ob=new Scanner(System.in);
int n,a=0,b=0,c=0,d=0,i,p;
n=ob.nextInt();
for(i=0;i<n;i++)
{
p=ob.nextInt();
if(p==1)
{a++;continue;}
else
if(p==2)
{b++;continue;}
else
{c++;continue;}
}
d=(a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println(n-d);
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long iLength, n1 = 0, n2 = 0, n3 = 0, i;
int iInput;
cin >> iLength;
for (i = 1; i <= iLength; ++i) {
cin >> iInput;
switch (iInput) {
case 1:
++n1;
break;
case 2:
++n2;
break;
case 3:
++n3;
break;
default:
break;
}
}
cout << (iLength - max(max(n1, n2), n3));
endapp:
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, count1 = 0, count2 = 0, count3 = 0;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == 1) {
count1++;
}
if (arr[i] == 2) {
count2++;
}
if (arr[i] == 3) {
count3++;
}
}
cout << n - max(count1, max(count2, count3));
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.Scanner;
public class Sequence52A
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
int k=0,l=0,m=0;
for(int i=0;i<n;i++)
{
if(a[i] == 1)
k++;
else if(a[i] == 2)
l++;
else
m++;
}
if(k >=l &&k>=m)
System.out.println(n-k);
else if(l>=k &&l>=m)
System.out.println(n-l);
else
System.out.println(n-m);
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
StreamTokenizer in; PrintWriter out; boolean oj;
BufferedReader br; Reader reader; Writer writer;
void init() throws IOException {
oj = System.getProperty("ONLINE_JUDGE") != null;
reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt");
writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt");
br = new BufferedReader(reader);
in = new StreamTokenizer(br);
out = new PrintWriter(writer);
}
void run() throws IOException {
long beginTime = System.currentTimeMillis();
init();
solve();
if(!oj){
long endTime = System.currentTimeMillis();
if (!oj) {
System.out.println("Memory used = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
System.out.println("Running time = " + (endTime - beginTime));
}
}
out.flush();
}
void solve() throws IOException {
int n=nI(), a[] = new int[3]; Arrays.fill(a, 0);
for(int i=0; i<n; i++)a[nI()-1]++; Arrays.sort(a);
out.println(a[0]+a[1]);
}
int nI() throws IOException {
in.nextToken();
return (int) in.nval;
}
long nL() throws IOException {
in.nextToken();
return (long) in.nval;
}
String nS() throws IOException {
in.nextToken();
return in.sval;
}
double nD() throws IOException {
in.nextToken();
return in.nval;
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, a[4], c;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &c), a[c]++;
cout << n - max(max(a[1], a[2]), a[3]);
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int max(int x, int y) {
if (x > y)
return x;
else
return y;
}
int main() {
int n;
while (cin >> n) {
int a;
int c1 = 0, c2 = 0, c3 = 0;
for (int i = 1; i <= n; i++) {
cin >> a;
if (a == 1)
c1++;
else if (a == 2)
c2++;
else if (a == 3)
c3++;
}
int maxx = -1;
maxx = max(c1, c2);
maxx = max(maxx, c3);
cout << n - maxx << endl;
}
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.*;
import java.util.*;
public class Main {
static class Assert {
static void check(boolean e) {
if (!e) {
throw new Error();
}
}
}
static class Scanner {
StreamTokenizer in;
Scanner(Reader r) {
in = new StreamTokenizer(new BufferedReader(r));
in.resetSyntax();
in.whitespaceChars(0, 32);
in.wordChars(33, 255);
}
String next() {
try {
in.nextToken();
Assert.check(in.ttype == in.TT_WORD);
return in.sval;
} catch (IOException e) {
throw new Error(e);
}
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream in) throws IOException {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
FastScanner in;
//Scanner in;
PrintWriter out;
void solve() {
int n = in.nextInt();
int[] freq = new int[3];
for (int i = 0; i < n; ++i) {
int curr = in.nextInt();
freq[curr - 1]++;
}
out.println(Math.min(Math.min(freq[0] + freq[1], freq[0] + freq[2]), freq[1] + freq[2]));
}
void run() {
try {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
//in = new Scanner(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
} catch (IOException e) {
throw new Error(e);
}
try {
solve();
} finally {
out.close();
}
}
public static void main(String args[]) {
new Main().run();
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
l=input().split()
c1,c2,c3=l.count('1'),l.count('2'),l.count('3')
print(min(c1+c2,c2+c3,c3+c1)) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
import java.awt.Point;
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;
public class Main {
static StringBuilder a = new StringBuilder();
public static void main(String[] args) throws IOException {
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//(new FileReader("input.in"));
StringBuilder out = new StringBuilder();
// StringTokenizer tk;
// tk = new StringTokenizer(in.readLine());
//Scanner Reader = new Scanner(System.in);
Reader.init(System.in);
int n=Reader.nextInt(),o=0,t=0,th=0;
for (int i = 0; i < n; i++) {
int x=Reader.nextInt();
if(x==1)
o++;
else if(x==2)
t++;
else if(x==3)
th++;
}
if(o>t&&o>th)
System.out.println(t+th);
else if(t>o&&th<t)
System.out.println(o+th);
else if(th>o&&th>t)
System.out.println(o+t);
else if(o==t&&t==th)
System.out.println(o+t);
else if(o==t)
System.out.println(o+th);
else if(o==th)
System.out.println(o+t);
else if(t==th)
System.out.println(o+t);
}
}
//class card implements Comparator<card>{
//int x,y;
//public card(){}
//public card(int a,int b){
// x=a;
// y=b;
//}
//
//
// public int compare(card o1, card o2) {
// if (o1.y> o2.y) {
// return 1;
// } else if (o1.y < o2.y) {
// return -1;
// }
// return 0;
// }
//
//
//
//}
class Reader {
static StringTokenizer tokenizer;
static BufferedReader reader;
public static void init(InputStream input) throws UnsupportedEncodingException {
reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
tokenizer = new StringTokenizer("");
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() throws IOException {
return reader.readLine();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | # 123-sequence
import collections
n = int(raw_input())
count = collections.Counter(x for x in raw_input().split())
print n-count.most_common(1)[0][1] | PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CF_52_A_123_SEQUENCE {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
int one =0;
int two = 0;
int three = 0;
StringTokenizer sc = new StringTokenizer(br.readLine());
while(n-->0)
{
int x = Integer.parseInt(sc.nextToken());
if(x == 1)
one++;
else if (x == 2)
two++;
else if(x == 3)
three ++ ;
}
if(two>=one && two>=three)
{
out.println(one+three);
}else if(one>=two && one>=three)
{
out.println(two+three);
} else if(three>=two && three>=one)
out.println(one+two);
out.flush();
out.close();
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int hashh[100005];
int main() {
memset(hashh, 0, sizeof(hashh));
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int temp;
scanf("%d", &temp);
hashh[temp]++;
}
int ans = 0;
int count = INT_MIN;
for (int i = 0; i < 100003; i++) {
if (count < hashh[i]) {
count = hashh[i];
}
}
printf("%d ", (n - count));
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Codeforces {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader in = new FastReader();
int n = in.nextInt();
int one = 0;
int two = 0;
int three = 0;
int[] a = new int[n];
for (int i=0;i<n;i++){
a[i] = in.nextInt();
if(a[i]==1)
one++;
else if(a[i]==2)
two++;
else three++;
}
int[] b = new int[3];
b[0] = two+three;
b[1] = one + three;
b[2] = one +two;
Arrays.sort(b);
System.out.println(b[0]);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(input())
seq = list(map(int, input().split()))
print(n - max(seq.count(x) for x in [1,2,3])) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n, t = int(input()), input()[:: 2]
a, b = t.count('1'), t.count('2')
print(n - max(a, b, n - a - b)) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = sc.nextInt();
String input;
int num[] = new int[4];
sc.nextLine();
input = sc.nextLine();
int len = input.length();
for (int i = 0; i < len; i++)
{
char c = input.charAt(i);
if (c == ' ')
continue;
num[(byte) c - (byte) '0']++;
}
int maxNum = 0;
for (int i = 1; i < 4; i++)
{
maxNum = Math.max(maxNum, num[i]);
}
System.out.println(n - maxNum);
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, a[9999999], d1, d2, d3;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 1) d1++;
if (a[i] == 2) d2++;
if (a[i] == 3) d3++;
}
int res = max(max(d1, d2), d3);
cout << n - res;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(input())
arr = list(map(int,input().strip().split()))
x = arr.count(1)
y = arr.count(2)
z = arr.count(3)
ans1 = max(x,y)
ans2 = max(ans1,z)
print(len(arr)-ans2) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import sys
n_nums = int(sys.stdin.readline())
values = [int(x) for x in sys.stdin.readline().split()]
one, two, three = 0, 0, 0
for i in values:
if i == 1:
one += 1
if i == 2:
two += 1
if i == 3:
three += 1
result = one + two + three - max(one, two, three)
print(result)
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | total_number=int(input())
my_list=input()
user_list=my_list.split()
a, b, c = 0, 0, 0
for num in user_list:
if int(num)==1:
a+=1
elif int(num)==2:
b+=1
elif int(num)==3:
c+=1
d=max(a,b,c)
print (total_number-d)
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, s[4];
int main() {
scanf("%d", &n);
for (int x, i = 1; i <= n; i++) scanf("%d", &x), s[x]++;
cout << n - max(s[1], max(s[2], s[3]));
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
void solve() throws IOException {
int n = nextInt();
int[] sum = {0,0,0,0};
for (int i = 0; i < n; i++) {
sum[nextInt()]++;
}
out.print(Math.min(Math.min(sum[1]+sum[2], sum[2]+sum[3]), sum[3]+sum[1]));
}
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
Main() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
solve();
in.close();
out.close();
}
private void eat(String str) {
st = new StringTokenizer(str);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
new Main();
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import sys
import math
n = int(sys.stdin.readline())
an = [int(x) for x in (sys.stdin.readline()).split()]
v = [0] * 4
vall = 0
for i in an:
vall += 1
v[i] += 1
print(vall - max(v))
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
arr = [int(i) for i in input().split()]
a=arr.count(1)
b=arr.count(2)
c=arr.count(3)
min=b+c
if min>(a+c):
min=a+c
if min>(a+b):
min=a+b
print(min)
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int a[1000005], cnt1, cnt2, cnt3, n, k;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
if (a[i] == 1) cnt1++;
if (a[i] == 2) cnt2++;
if (a[i] == 3) cnt3++;
}
k = max(cnt1, max(cnt2, cnt3));
cout << n - k;
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=input()
a=map(int, raw_input().split())
q1,q2,q3=0,0,0
for i in a:
if i==1:
q1+=1
elif i==2:
q2+=1
elif i==3:
q3+=1
s1=q2+q3
s2=q1+q3
s3=q1+q2
if s1<=s2 and s1<=s3:
print s1
elif s2<=s1 and s2<=s3:
print s2
else:
print s3
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, x;
cin >> n;
int arr[4] = {0};
for (int i = 0; i < n; i++) {
cin >> x;
arr[x]++;
}
sort(arr + 1, arr + 4);
cout << arr[1] + arr[2];
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T> p) {
out << "(" << p.first << "," << p.second << ")";
return out;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
for (auto a : v) out << a << " ";
return out;
}
template <typename T>
ostream& operator<<(ostream& out, const set<T>& S) {
for (auto a : S) cout << a << " ";
return out;
}
template <typename T>
ostream& operator<<(ostream& out, const multiset<T>& S) {
for (auto a : S) cout << a << " ";
return out;
}
template <typename S, typename T>
ostream& operator<<(ostream& out, const map<S, T>& M) {
for (auto m : M) cout << "(" << m.first << "->" << m.second << ") ";
return out;
}
template <typename S, typename T>
pair<S, T> operator+(pair<S, T> a, pair<S, T> b) {
return make_pair(a.first + b.first, a.second + b.second);
}
template <typename S, typename T>
pair<S, T> operator-(pair<S, T> a, pair<S, T> b) {
return make_pair(a.first - b.first, a.second - b.second);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
vector<int> A;
cin >> n;
A.resize(n);
for (int i = 0; i < n; i++) cin >> A[i];
vector<int> B(3);
for (int i = 0; i < n; i++) {
B[A[i] - 1]++;
}
int min_val = 1e9;
for (int i = 0; i < 3; i++) {
int val = n - B[i];
min_val = min(min_val, val);
}
cout << min_val << endl;
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = input();
nums = [int(i) for i in input().split()];
ones = nums.count(1);
twos = nums.count(2);
threes = nums.count(3);
x = max(ones, twos, threes);
print(ones + twos + threes - x);
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | m=input()
l=input()
o=l.count('1')
t=l.count('2')
th=l.count('3')
l=[o,t,th]
l.sort()
print(l[0]+l[1])
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | total_number=input()
my_list=input()
user_list=my_list.split()
a=user_list.count('1')
b=user_list.count('2')
c=user_list.count('3')
d=max(a,b,c)
print (int(total_number)-d)
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(raw_input())
l=raw_input().count
print min(l('1')+l('2'),l('1')+l('3'),l('2')+l('3')) | PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | input()
nums = input().split()
print(min([len(nums)-nums.count("1"), len(nums)-nums.count("2"), len(nums)-nums.count("3")])) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A52 {
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int num=Integer.parseInt(st.nextToken());
st=new StringTokenizer(br.readLine());
int[] counter=new int[3];
for(int i=0;i<num;i++){
switch(Integer.parseInt(st.nextToken())){
case 1:
counter[0]++;
break;
case 2:
counter[1]++;
break;
case 3:
counter[2]++;
break;
}
}
System.out.println(num-Math.max(counter[0], Math.max(counter[1],counter[2])));
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | //package geometry;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.awt.Point;
/**
*
* @author prabhat // use stringbuilder,TreeSet, priorityQueue
*/
public class point4{
public static long[] BIT;
public static int[] tree,lazy;
public static long[] sum;
public static int mod=1000000007;
public static int[][] dp;
public static boolean[][] isPalin;
public static int max1=100005;
public static int[][] g;
public static LinkedList<Pair> l[],pp;
public static Query[] query;
public static int n,m,q,k,t,x,y,a[],b[],arr[],chosen[],pos[],val[],blocksize,count=0,size[],tin[],len,
tout[],discover[],cnt[],pre[],cnty[],cntZ[];
public static long V[],low,high,min=Long.MAX_VALUE,cap[],has[],max,ans;
public static ArrayList<Integer> adj[],al[];
public static TreeSet<Integer> ts;
public static char s[][];
public static int depth[],mini,maxi;
public static boolean visit[][],isPrime[],used[][];
public static int[][] dist;
public static ArrayList<Integer>prime;
public static int[] x1={0,0,-1,1};
public static int[] y1={-1,1,0,0};
public static ArrayList<Integer> divisor[]=new ArrayList[1000005];
public static int[][] subtree,parent,mat;
public static TreeMap<Integer,Integer> tm;
public static LongPoint[] p;
public static int[][] grid,memo;
public static ArrayList<Pair> list[];
public static TrieNode root,node[];
public static LinkedHashMap<String,Integer> map;
public static ArrayList<String> al1;
public static int upper=(int)(1e7);
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception
{
InputReader in = new InputReader(System.in);
PrintWriter pw=new PrintWriter(System.out);
n=in.ii();
a=in.iia(n);
int[] cnt=new int[4];
for(int i:a)cnt[i]++;
int ans=Integer.MAX_VALUE;
ans=Math.min(ans,cnt[1]+cnt[2]);
ans=Math.min(ans,cnt[2]+cnt[3]);
ans=Math.min(ans,cnt[1]+cnt[3]);
pw.println(ans);
pw.close();
}
static void mergeSort(int[] a, int p, int r)
{
if( p < r )
{
int q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q + 1, r);
merge(a, p, q, r);
}
}
static void merge(int[] a, int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int[] L = new int[n1+1], R = new int[n2+1];
for(int i = 0; i < n1; i++) L[i] = a[p + i];
for(int i = 0; i < n2; i++) R[i] = a[q + 1 + i];
L[n1] = R[n2] = Integer.MAX_VALUE;
for(int k = p, i = 0, j = 0; k <= r; k++)
if(L[i] <= R[j])
a[k] = L[i++];
else
a[k] = R[j++];
}
static int determineUsage(int[] distance, int[] height)
{
int maxX=0;
int maxY=0;
int n=height.length;
int[] preX=new int[n+1];
preX[0]=0;
for(int i:height)maxY=Math.max(maxY,i);
for(int i=0;i<distance.length;i++){
preX[i+1]=preX[i]+distance[i];
}
int ans=Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
outer:
for(int j=i+1;j<n;j++)
{
int cnt=0;
for(int k=0;k<n;k++)
{
if(k!=i){
if((-height[i])*(preX[j]-preX[i])-(height[j]-height[i])*(preX[k]-preX[i])>0)continue outer;
int d=(height[k]-height[i])*(preX[j]-preX[i])-(height[j]-height[i])*(preX[k]-preX[i]);
if(d>0)cnt++;
if(d<0)continue outer;
}
}
ans=Math.min(cnt,ans);
}
}
return ans==Integer.MAX_VALUE?n-1:ans;
}
static int ori(int x1,int y1,int x2,int y2,int x3,int y3)
{
int X1=x3-x1;
int Y1=y3-y1;
int X2=x2-x1;
int Y2=y2-y1;
return (X1*Y2-X2*Y1);
}
static class state{
int x,y,t;
public state(int x,int y,int t)
{
this.x=x;
this.y=y;
this.t=t;
}
}
static class Node{
int gcd,cnt;
public Node(int gcd,int cnt)
{
this.gcd=gcd;
this.cnt=cnt;
}
}
static void build(int node,int start,int end){
if(start > end)return;
if(start == end){
tree[node] = arr[start];
return;
}
build(2*node,start,(start + end)/2);
build(2*node + 1,(start + end)/2 + 1,end);
tree[node] = Math.min(tree[2*node],tree[2*node + 1]);
}
static void update(int node,int start,int end,int i,int j,int val){
if(lazy[node] != 0){
tree[node] += lazy[node];
if(start != end){
lazy[2*node] += lazy[node];
lazy[2*node + 1] += lazy[node];
}
lazy[node] = 0;
}
if(start > end || start > j || end < i)return;
if(start >= i && end <= j){
tree[node] += val;
if(start != end){
lazy[2*node] += val;
lazy[2*node + 1] += val;
}
return;
}
update(2*node,start,(start + end)/2,i,j,val);
update(2*node + 1,(start + end)/2 + 1,end,i,j,val);
tree[node] = Math.min(tree[2*node],tree[2*node + 1]);
}
static int query(int node,int start,int end,int i,int j){
if(start > end || start > j || end < i)return Integer.MAX_VALUE;
if(lazy[node] != 0){
tree[node] += lazy[node];
if(start != end){
lazy[2*node] += lazy[node];
lazy[2*node + 1] += lazy[node];
}
lazy[node] = 0;
}
if(start >= i && end <= j)return tree[node];
int q1 = query(2*node,start,(start + end)/2,i,j);
int q2 = query(2*node + 1,(start + end)/2 + 1,end,i,j);
return Math.min(q1,q2);
}
static int get(int u, int d){
for(int i = len - 1; i >= 0; i--){
if(d >= 1 << i){
d -= 1 << i;
u = parent[i][u];
}
}
return u;
}
static void dfs(int u, int p) {
tin[u] = count++;
parent[0][u] = p;
size[u]++;
for (int i = 1; i < len; i++)
parent[i][u] = parent[i - 1][parent[i - 1][u]];
for (int v :adj[u])
if (v != p)
{
depth[v] = depth[u] + 1;
dfs( v, u);
size[u] += size[v];
}
tout[u] = count++;
}
static boolean isParent(int parent, int child) {
return tin[parent] <= tin[child] && tout[child] <= tout[parent];
}
static int lca(int a, int b) {
if (isParent(a, b))
return a;
if (isParent(b, a))
return b;
for (int i = len - 1; i >= 0; i--)
if (!isParent(parent[i][a], b))
a = parent[i][a];
return parent[0][a];
}
static class TrieNode
{
int value; // Only used in leaf nodes
int freq;
TrieNode[] arr = new TrieNode[2];
public TrieNode() {
value = Integer.MAX_VALUE;
freq=0;
arr[0] = null;
arr[1] = null;
}
}
static void insert(int n,int num)
{
TrieNode temp = node[n];
temp.value=Math.min(temp.value,num);
// Start from the msb, insert all bits of
// pre_xor into Trie
for (int i=19; i>=0; i--)
{
// Find current bit in given prefix
int val = (num & (1<<i)) >=1 ? 1 : 0;
// Create a new node if needed
if (temp.arr[val] == null)
temp.arr[val] = new TrieNode();
temp = temp.arr[val];
temp.value=Math.min(temp.value,num);
}
// System.out.println(temp.value);
// Store value at leaf node
//temp.value = pre_xor;
}
static long query(int x,int k,int s)
{
TrieNode temp = node[k];
long ans=0;
if(x%k!=0||temp.value+x>s)return -1;
for (int i=19; i>=0; i--)
{
// Find current bit in given prefix
int val = (x & (1<<i)) >= 1 ? 1 : 0;
if(temp.arr[1^val]!=null&&temp.arr[1^val].value+x<=s)
{
ans+=(1^val)<<i;
temp=temp.arr[1^val];
}
else if(temp.arr[val]!=null){
ans+=val<<i;
temp=temp.arr[val];
if(temp==null||temp.value+x>s)
{
return -1;
}
}
}
return ans;
}
static void update(int indx,long val)
{
while(indx<max1)
{
BIT[indx]=Math.max(BIT[indx],val);
indx+=(indx&(-indx));
}
}
static long query1(int indx)
{
long sum=0;
while(indx>=1)
{
sum=Math.max(BIT[indx],sum);
indx-=(indx&(-indx));
}
return sum;
}
static void Seive()
{
isPrime=new boolean[max1];
Arrays.fill(isPrime,true);
for(int i=1;i<max1;i++)divisor[i]=new ArrayList<Integer>();
isPrime[0]=isPrime[1]=false;
for(int i=1;i<(max1);i++)
{
// if(isPrime[i])
for(int j=i;j<max1;j+=i)
{
//isPrime[j]=false;
divisor[j].add(i);
}
// divisor[i].add(i);
}
}
static int orient(Point a,Point b,Point c)
{
//return (int)(c.x-a.x)*(int)(b.y-a.y)-(int)(c.y-a.y)*(int)(b.x-a.x);
Point p1=c.minus(a);
Point p2=b.minus(a);
return (int)(p1.cross(p2));
}
public static class Polygon {
static final double EPS = 1e-15;
public int n;
public Point p[];
Polygon(int n, Point x[]) {
this.n = n;
p = Arrays.copyOf(x, n);
}
long area(){ //returns 2 * area
long ans = 0;
for(int i = 1; i + 1 < n; i++)
ans += p[i].minus(p[0]).cross(p[i + 1].minus(p[0]));
return ans;
}
boolean PointInPolygon(Point q) {
boolean c = false;
for (int i = 0; i < n; i++){
int j = (i+1)%n;
if ((p[i].y <= q.y && q.y < p[j].y ||
p[j].y <= q.y && q.y < p[i].y) &&
q.x < p[i].x + (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y))
c = !c;
}
return c;
}
// determine if point is on the boundary of a polygon
boolean PointOnPolygon(Point q) {
for (int i = 0; i < n; i++)
if (ProjectPointSegment(p[i], p[(i+1)%n], q).dist(q) < EPS)
return true;
return false;
}
// project point c onto line segment through a and b
Point ProjectPointSegment(Point a, Point b, Point c) {
double r = b.minus(a).dot(b.minus(a));
if (Math.abs(r) < EPS) return a;
r = c.minus(a).dot(b.minus(a))/r;
if (r < 0) return a;
if (r > 1) return b;
return a.plus(b.minus(a).mul(r));
}
}
public static class Point {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point minus(Point b) {
return new Point(x - b.x, y - b.y);
}
public Point plus(Point a){
return new Point(x + a.x, y + a.y);
}
public double cross(Point b) {
return (double)x * b.y - (double)y * b.x;
}
public double dot(Point b) {
return (double)x * b.x + (double)y * b.y;
}
public Point mul(double r){
return new Point(x * r, y * r);
}
public double dist(Point p){
return Math.sqrt(fastHypt( x - p.x , y - p.y));
}
public double fastHypt(double x, double y){
return x * x + y * y;
}
}
static class Query implements Comparable<Query>{
int index,k;
int L;
int R;
public Query(){}
public Query(int a,int b,int index)
{
this.L=a;
this.R=b;
this.index=index;
}
public int compareTo(Query o)
{
if(L/blocksize!=o.L/blocksize)return L/blocksize-o.L/blocksize;
else return R-o.R;
}
}
static double dist(Point p1,Point p2)
{
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
static class coder {
int indx;
String name;
coder(int indx,String name)
{
this.indx=indx;
this.name=name;
}
}
static int gcd(int a, int b)
{
int res=0;
while(a!=0)
{
res=a;
a=b%a;
b=res;
}
return b;
}
static slope getSlope(Point p, Point q)
{
int dx = (int)(q.x - p.x), dy = (int)(q.y - p.y);
int g = gcd(dx, dy);
return new slope(dy / g, dx / g);
}
static class slope implements Comparable<slope>
{
int x,y;
public slope(int y,int x)
{
this.x=x;
this.y=y;
}
public int compareTo(slope s)
{
if(s.y!=y)return y-s.y;
return x-s.x;
}
}
static class Ball implements Comparable
{
int r;
int occ;
public Ball(int r,int occ)
{
this.r = r;
this.occ = occ;
}
@Override
public int compareTo(Object o) {
Ball b = (Ball) o;
return b.occ-this.occ;
}
}
static class E implements Comparable<E>{
int x,y;
char c;
E(int x,int y,char c)
{
this.x=x;
this.y=y;
this.c=c;
}
public int compareTo(E o)
{
if(x!=o.x)return o.x-x;
else return y-o.y;
}
}
public static ListNode removeNthFromEnd(ListNode a, int b) {
int cnt=0;
ListNode ra1=a;
ListNode ra2=a;
while(ra1!=null){ra1=ra1.next; cnt++;}
if(b>cnt)return a.next;
else if(b==1&&cnt==1)return null;
else{
int y=cnt-b+1;
int u=0;
ListNode prev=null;
while(a!=null)
{
u++;
if(u==y)
{
if(a.next==null)prev.next=null;
else
{
if(prev==null)return ra2.next;
prev.next=a.next;
a.next=null;
}
break;
}
prev=a;
a=a.next;
}
}
return ra2;
}
static ListNode rev(ListNode a)
{
ListNode prev=null;
ListNode cur=a;
ListNode next=null;
while(cur!=null)
{
next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
static class ListNode {
public int val;
public ListNode next;
ListNode(int x) { val = x; next = null; }
}
public static String add(String s1,String s2)
{
int n=s1.length()-1;
int m=s2.length()-1;
int rem=0;
int carry=0;
int i=n;
int j=m;
String ans="";
while(i>=0&&j>=0)
{
int c1=(int)(s1.charAt(i)-'0');
int c2=(int)(s2.charAt(j)-'0');
int sum=c1+c2;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
i--; j--;
}
while(i>=0)
{
int c1=(int)(s1.charAt(i)-'0');
int sum=c1;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
i--;
}
while(j>=0)
{
int c2=(int)(s2.charAt(j)-'0');
int sum=c2;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
j--;
}
if(carry>0)ans=carry+ans;
return ans;
}
public static String[] multiply(String A, String B) {
int lb=B.length();
char[] a=A.toCharArray();
char[] b=B.toCharArray();
String[] s=new String[lb];
int cnt=0;
int y=0;
String s1="";
q=0;
for(int i=b.length-1;i>=0;i--)
{
int rem=0;
int carry=0;
for(int j=a.length-1;j>=0;j--)
{
int mul=(int)(b[i]-'0')*(int)(a[j]-'0');
mul+=carry;
rem=mul%10;
carry=mul/10;
s1=rem+s1;
}
s1=carry+s1;
s[y++]=s1;
q=Math.max(q,s1.length());
s1="";
for(int i1=0;i1<y;i1++)s1+='0';
}
return s;
}
public static long nCr(long total, long choose)
{
if(total < choose)
return 0;
if(choose == 0 || choose == total)
return 1;
return nCr(total-1,choose-1)+nCr(total-1,choose);
}
static int get(long s)
{
int ans=0;
for(int i=0;i<n;i++)
{
if(p[i].x<=s&&s<=p[i].y)ans++;
}
return ans;
}
static class LongPoint {
public long x;
public long y;
public LongPoint(long x, long y) {
this.x = x;
this.y = y;
}
public LongPoint subtract(LongPoint o) {
return new LongPoint(x - o.x, y - o.y);
}
public long cross(LongPoint o) {
return x * o.y - y * o.x;
}
}
static int CountPs(String s,int n)
{
boolean b=false;
char[] S=s.toCharArray();
int[][] dp=new int[n][n];
boolean[][] p=new boolean[n][n];
for(int i=0;i<n;i++)p[i][i]=true;
for(int i=0;i<n-1;i++)
{
if(S[i]==S[i+1])
{
p[i][i+1]=true;
dp[i][i+1]=0;
b=true;
}
}
for(int gap=2;gap<n;gap++)
{
for(int i=0;i<n-gap;i++)
{
int j=gap+i;
if(S[i]==S[j]&&p[i+1][j-1]){p[i][j]=true;}
if(p[i][j])
dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
else if(!p[i][j]) dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
//if(dp[i][j]>=1)
// return true;
}
}
// return b;
return dp[0][n-1];
}
static long divCeil(long a,long b)
{
return (a+b-1)/b;
}
static long root(int pow, long x) {
long candidate = (long)Math.exp(Math.log(x)/pow);
candidate = Math.max(1, candidate);
while (pow(candidate, pow) <= x) {
candidate++;
}
return candidate-1;
}
static long pow(long x, int pow)
{
long result = 1;
long p = x;
while (pow > 0) {
if ((pow&1) == 1) {
if (result > Long.MAX_VALUE/p) return Long.MAX_VALUE;
result *= p;
}
pow >>>= 1;
if (pow > 0 && p >= 4294967296L) return Long.MAX_VALUE;
p *= p;
}
return result;
}
static boolean valid(int i,int j)
{
if(i>=0&&i<n&&j>=0&&j<m)return true;
return false;
}
private static class DSU{
int[] parent;
int[] rank;
int cnt;
public DSU(int n){
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++){
parent[i]=i;
rank[i]=1;
}
cnt=n;
}
int find(int i){
while(parent[i] !=i){
parent[i]=parent[parent[i]];
i=parent[i];
}
return i;
}
int Union(int x, int y){
int xset = find(x);
int yset = find(y);
if(xset!=yset)
cnt--;
if(rank[xset]<rank[yset]){
parent[xset] = yset;
rank[yset]+=rank[xset];
rank[xset]=0;
return yset;
}else{
parent[yset]=xset;
rank[xset]+=rank[yset];
rank[yset]=0;
return xset;
}
}
}
public static int[][] packU(int n, int[] from, int[] to, int max) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int i = 0; i < max; i++) p[from[i]]++;
for (int i = 0; i < max; i++) p[to[i]]++;
for (int i = 0; i < n; i++) g[i] = new int[p[i]];
for (int i = 0; i < max; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
public static int[][] parents3(int[][] g, int root) {
int n = g.length;
int[] par = new int[n];
Arrays.fill(par, -1);
int[] depth = new int[n];
depth[0] = 0;
int[] q = new int[n];
q[0] = root;
for (int p = 0, r = 1; p < r; p++) {
int cur = q[p];
for (int nex : g[cur]) {
if (par[cur] != nex) {
q[r++] = nex;
par[nex] = cur;
depth[nex] = depth[cur] + 1;
}
}
}
return new int[][]{par, q, depth};
}
public static int lower_bound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] < target)
low = mid + 1;
else
high = mid;
}
return nums[low] == target ? low : -1;
}
public static int upper_bound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high + 1 - low) / 2;
if (nums[mid] > target)
high = mid - 1;
else
low = mid;
}
return nums[low] == target ? low : -1;
}
public static boolean palin(String s)
{
int i=0;
int j=s.length()-1;
while(i<j)
{
if(s.charAt(i)==s.charAt(j))
{
i++;
j--;
}
else return false;
}
return true;
}
static int lcm(int a,int b)
{
return (a*b)/(gcd(a,b));
}
public static long pow(long n,long p,long m)
{
long result = 1;
if(p==0)
return 1;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>=m)
result%=m;
p >>=1;
n*=n;
if(n>=m)
n%=m;
}
return result;
}
/**
*
* @param n
* @param p
* @return
*/
public static long pow(long n,long p)
{
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
p >>=1;
n*=n;
}
return result;
}
static class Edge implements Comparator<Edge> {
private int u;
private int v;
private long w;
public Edge() {
}
public Edge(int u, int v, long w) {
this.u=u;
this.v=v;
this.w=w;
}
public int getU() {
return u;
}
public void setU(int u) {
this.u = u;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public long getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public long compareTo(Edge e)
{
return (this.getW() - e.getW());
}
@Override
public int compare(Edge o1, Edge o2) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
static class Pair implements Comparable<Pair>
{
int x,y;
Pair (int a,int b)
{
this.x=a;
this.y=b;
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.x!=o.x)
return -Integer.compare(this.x,o.x);
else
return -Integer.compare(this.y, o.y);
//return 0;
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair)o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private SpaceCharFilter filter;
byte inbuffer[] = new byte[1024];
int lenbuffer = 0, ptrbuffer = 0;
final int M = (int) 1e9 + 7;
int md=(int)(1e7+1);
int[] SMP=new int[md];
final double eps = 1e-6;
final double pi = Math.PI;
PrintWriter out;
String check = "";
InputStream obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
public InputReader(InputStream stream)
{
this.stream = stream;
}
int readByte() {
if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) {
ptrbuffer = 0;
try {
lenbuffer = obj.read(inbuffer);
} catch (IOException e) {
throw new InputMismatchException();
}
}
if (lenbuffer <= 0) return -1;
return inbuffer[ptrbuffer++];
}
public int read()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
String is() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ')
{
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int ii() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long il() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip()
{
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
float nf() {
return Float.parseFloat(is());
}
double id() {
return Double.parseDouble(is());
}
char ic() {
return (char) skip();
}
int[] iia(int n) {
int a[] = new int[n];
for (int i = 0; i<n; i++) a[i] = ii();
return a;
}
long[] ila(int n) {
long a[] = new long[n];
for (int i = 0; i <n; i++) a[i] = il();
return a;
}
String[] isa(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++) a[i] = is();
return a;
}
long mul(long a, long b) { return a * b % M; }
long div(long a, long b)
{
return mul(a, pow(b, M - 2));
}
double[][] idm(int n, int m) {
double a[][] = new double[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = id();
return a;
}
int[][] iim(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j <m; j++) a[i][j] = ii();
return a;
}
public String readLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.*;
public class Code {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = Integer.parseInt(in.nextLine().trim());
String b = in.nextLine().replace(" ", "");
int co1=Car(b, "1");
int co2=Car(b,"2");
int co3=Car(b,"3");
if (a==2 || a==1) {
if (a==2) {
System.out.println(1);
}else{
System.out.println(0);
}
}else{
if (co1>co2 && co1>co3) {
System.out.println(co2+co3);
}else if (co2>co1 && co2>co3) {
System.out.println(co1+co3);
}else if (co3>co1 && co3>co2) {
System.out.println(co1+co2);
}else{
System.out.println(co1+co3);
}
}
}
public static int Car(String a , String x){
String c =a.replace(x, "").trim().replace(" ", "");
return a.length()-c.length();
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, s[4];
int main() {
scanf("%d", &n);
for (int x, i = 1; i <= n; i++) scanf("%d", &x), s[x]++;
cout << n - max(s[1], max(s[2], s[3]));
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = raw_input()
k = [int(x) for x in raw_input().split()]
w = len(filter(lambda x: x == 1 , k))
y = len(filter(lambda x: x == 2 , k))
z = len(filter(lambda x: x == 3 , k))
b = max(w,y,z)
print int(n) - b | PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.*;
public class Sequence{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.nextLine();
int[] c = new int[3];
String[] nums = scan.nextLine().split(" ");
for(int i = 0; i < n; i++) c[Integer.parseInt(nums[i]) - 1] += 1;
int max = 0;
for(int i = 0; i < c.length; i++) if(c[i] > c[max]) max = i;
System.out.println(n - c[max]);
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = raw_input()
s = raw_input()
d = {}
for i in s.split():
d[i] = d.get(i, 0) + 1
print sum(d.values()) - max(d.values())
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | a = int(input())
t = list(map(int,input().split()))
g = set(t)
p=[]
for f in g:
p.append(t.count(f))
print(sum(p)-max(p))
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
// public static ArrayList<Integer> list = new ArrayList<Integer>();
// public static ArrayList<Integer> list2 = new ArrayList<Integer>();
//public static ArrayList<Integer> list2= new ArrayList<Integer>();
public static Scanner input = new Scanner(System.in);
public static void main(String[] args)
throws IOException {
// BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
// String str;
// while((str=buffer.readLine())!=null){
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
//while(input.hasNextLine()){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[] = new int [4] , i , j , n = Integer.valueOf(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
while(st.hasMoreTokens()) a[Integer.valueOf( st.nextToken() )] ++;
System.out.println( n - Math.max( Math.max(a[1] , a[2]) , a[3] ) );
}} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int count1 = 0, count2 = 0, count3 = 0;
int N;
cin >> N;
for (int i = 0; i < N; i++) {
int t;
cin >> t;
if (t == 1) count1++;
if (t == 2) count2++;
if (t == 3) count3++;
}
int ans = count1 + count2 + count3 - max(count1, max(count2, count3));
printf("%d\n", ans);
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=input()
a=input()
x=a.count('1')
y=a.count('2')
z=a.count('3')
a=[x,y,z]
a.sort()
print(a[0]+a[1])
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# IN THE NAME OF GOD
# C++ : Done.!
# Python 3 : Loading...
import math
import random
import sys
import time
#----------------------------------> Programmer : Kadi <----------------------------------#
t = int(input())
one = two = three = 0
s = input()
for i in range(0, len(s)):
if s[i] == '1' : one = one + 1
if s[i] == '2' : two = two + 1
if s[i] == '3' : three = three + 1
print(t - max(one, max(two, three)))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[4];
for(int i=0;i<n;i++){
a[sc.nextInt()]++;
}
int max=-2;
for(int i=1;i<=3;i++){
if(a[i]>max)
max=a[i];
}
System.out.println(n-max);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.*;
import java.io.*;
public class Seq123 {
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[] count = new int[4];
String s = in.readLine();
StringTokenizer st = new StringTokenizer(s);
while(n-->0) {
int i = Integer.parseInt(st.nextToken());
count[i]++;
}
int ans = Math.min(count[1]+count[2], count[2]+count[3]);
ans = Math.min(ans, count[3]+count[1]);
System.out.println(ans);
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
int n;
int list[1000010];
int cnt[5];
int Max(int a, int b) { return a > b ? a : b; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &list[i]);
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
for (int i = 1; i <= n; ++i) cnt[list[i]] += 1;
printf("%d\n", n - Max(cnt[1], Max(cnt[2], cnt[3])));
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | from collections import Counter
n = int(input())
xs = input().split()
maxfreq = Counter(xs).most_common(1)[0][1]
print(n - maxfreq)
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(raw_input())
data = map(int,raw_input().split())
data.sort()
one = 0
two = 0
three = 0
for i in range(n):
if data[i]==1:
one = one +1
if data[i]==2:
two = two +1
if data[i]==3:
three = three+1
print min(one+two,one+three,two+three)
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | inp = int(input())
a = [int(i) for i in input().split()]
diction = dict()
for letter in a:
diction[letter] = diction.get(letter, 0) + 1
print(inp - max(diction.values())) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n, a = input (), map (int, raw_input ().split ())
print min (a.count (1) + a.count (2), a.count (1) + a.count (3), a.count (2) + a.count (3))
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long double epsilon = 1e-9;
int main() {
vector<int> mat(3);
int n;
cin >> n;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
mat[x - 1]++;
}
int maxm = 0;
for (int i = 0; i < 3; i++) maxm = max(maxm, mat[i]);
cout << n - maxm << endl;
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, o = 0, t = 0, th = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
switch (x) {
case 1:
o++;
break;
case 2:
t++;
break;
case 3:
th++;
}
}
cout << (n - max(o, max(t, th))) << "\n";
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(input())
a = list(map(int, input().split()))
x = a.count(1)
y = a.count(2)
z = a.count(3)
print(n - max(x, y, z))
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a[] = new int[n];
int result[] = new int[4];
Arrays.fill(result, 0);
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
result[a[i]]++;
}
Arrays.sort(result);
System.out.println(n-result[3]);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | input()
s = input().split()
print(len(s)-max(s.count("1"),s.count("2"),s.count("3")))
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* Date 19.11.2011
* Time 22:39:40
* Author Woodey
* $
*/
public class A1 {
private void Solution() throws IOException {
int n = nextInt();
int[] mas = new int[3];
for (int i = 0; i < n; i ++) {
int a = nextInt();
mas[a-1] ++;
}
System.out.println(n-Math.max(Math.max(mas[0], mas[1]), mas[2]));
}
public static void main(String[] args) {
new A1().run();
}
BufferedReader in;
StringTokenizer tokenizer;
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
Solution();
in.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(in.readLine());
return tokenizer.nextToken();
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.List;
import java.util.Queue;
import java.awt.*;
public class codefors implements Runnable {
private BufferedReader br = null;
private PrintWriter pw = null;
private StringTokenizer stk = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new codefors()).run();
}
public void run() {
/*
* try { // br = new BufferedReader(new FileReader("input.txt")); pw =
* new PrintWriter("output.txt"); } catch (FileNotFoundException e) {
* e.printStackTrace(); }
*/
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
solver();
pw.close();
}
private void nline() {
try {
if (!stk.hasMoreTokens())
stk = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException("KaVaBUnGO!!!", e);
}
}
private String nstr() {
while (!stk.hasMoreTokens())
nline();
return stk.nextToken();
}
private int ni() {
return Integer.valueOf(nstr());
}
private double nd() {
return Double.valueOf(nstr());
}
private BigInteger nbi() {
return new BigInteger(nstr());
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
}
return null;
}
private void solver() {
int n = ni();
int a[] = new int[3];
for(int i=0; i<n; i++){
a[ni()-1]++;
}
Arrays.sort(a);
System.out.println(n-a[2]);
}
void exit() {
System.exit(0);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[]= new int[n];
for (int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int one,two,three;
one=two=three=0;
for (int i=0;i<n;i++){
if (a[i]==1)one++;
else if (a[i]==2)two++;
else three++;
}
// System.out.println(n-Math.max(one,Math.min(two,three)));
int max=Math.max(one,Math.max(two,three));
System.out.println(n-max);
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sortReverse(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
// Collections.sort.(l);
Collections.sort(l,Collections.reverseOrder());
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int freq[4];
int main() {
int n, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
freq[x]++;
}
sort(freq, freq + 4);
cout << abs(freq[3] - n) << endl;
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int max(int a, int b, int c) {
int x = c + b, y = a + c, z = a + b;
if (x <= y && x <= z)
cout << x << endl;
else if (z <= y && x >= z)
cout << z << endl;
else if (x >= y && y <= z)
cout << y << endl;
return 0;
}
int main() {
int n, a = 0, b = 0, c = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 1)
a++;
else if (x == 2)
b++;
else if (x == 3)
c++;
}
max(a, b, c);
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x = 0, q = 0, a[3] = {0, 0, 0};
scanf("%d", &x);
for (int i = 0; i < x; i++) {
scanf("%d", &q);
if (q == 1) {
a[0]++;
} else if (q == 2) {
a[1]++;
} else if (q == 3) {
a[2]++;
}
}
int max = 0;
for (int i = 0; i < 3; i++) {
if (max < a[i]) {
max = a[i];
}
}
printf("%d\n", x - max);
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class A {
static BufferedReader IN;
static {
try {
// IN = new BufferedReader(new FileReader(new File(A.class.getResource("a1.txt").toURI())));
IN = new BufferedReader(new InputStreamReader(System.in));
}
catch(Exception e) {
e.printStackTrace();
}
}
static String inStr() {
try {
return IN.readLine();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
static int inInt() { return Integer.parseInt(inStr()); }
static int[] inIntArr() {
String s = inStr();
if(s == null)
return null;
return toIntArr(s);
}
static int[] toIntArr(String s) {
String[] sArr = s.split(" ");
int[] iArr = new int[sArr.length];
for(int i = 0; i < sArr.length; ++i)
iArr[i] = Integer.parseInt(sArr[i]);
return iArr;
}
static void out(String s) { System.out.println(s); }
static void out(int i) { out(Integer.toString(i)); }
static void out(double x) {
out(format(x));
}
static final DecimalFormat df = new DecimalFormat("0.000000");
private static String format(double x) {
String s = df.format(x);
s = s.replaceAll(",", ".");
return s;
}
static int solve(final int n, final int[] a) {
int[] count = new int[3];
for(int i = 0; i < n; ++i)
++count[a[i] - 1];
int s12 = count[0] + count[1];
int s23 = count[1] + count[2];
int s13 = count[0] + count[2];
return Math.min(s12, Math.min(s13, s23));
}
public static void main(String[] args) {
int n = inInt();
int[] a = inIntArr();
out(solve(n, a));
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
stdin_flag=1
if not stdin_flag:
read_line_index=0
testcasefilename="test.txt"
Stestcase=open(testcasefilename).read()
read_line_datas=Stestcase.split("\n")
def debugs(s):
if not stdin_flag:
print ";;;",s
def puts(s):
import sys
sys.stdout.write(str(s))
#####################################
######################################
def read_line():
global read_line_index
if stdin_flag:
return raw_input()
else:
s=read_line_datas[read_line_index]
read_line_index+=1
return s
def answer():
if stdin_flag:
return solve()
else:
while read_line_proceed():
solve()
def read_line_proceed():
global read_line_index
print"##################"
while 1:
if read_line_index>= len (read_line_datas ):
return False
if read_line_datas[read_line_index]=="%%%":
read_line_index+=1
return True
read_line_index+=1
def readint():
return int (read_line() )
def readints():
return map(int, read_line().split(" "))
def reads():
return read_line()
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
def compute(ss):
m={}
for s in ss:
if s==" ":continue
if s in m:
m[s]+=1
else:
m[s]=1
return m
pass
def pos(s):
return (ord(s[0])-ord("a") , int(s[1])-1)
def dis(p1,p2):
return (p1[0]-p2[0])**2+(p1[1]-p2[1])**2
def solve():
n=readint()
x=readints()
m=sorted(x.count(i) for i in xrange(1,4) )
debugs(m)
print m[0]+m[1]
def test():
pass
test()
answer()
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A052 {
public static void main(String[] args) throws Exception {
BufferedReader r= new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(r.readLine());
int[] ar = new int[4];
StringTokenizer st= new StringTokenizer(r.readLine());
for(int x = 0;x<n;x++) {
ar[Integer.parseInt(st.nextToken())]++;
}
int max = 0;
for(int x = 1;x<=3;x++) {
if(ar[x]>max) max = ar[x];
}
System.out.println(n-max);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedInputStream;
import java.util.Scanner;
public class Sequence123 { //Testing Round #1 - 123-sequence
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int[] numXs = new int[3];
int numInts = sc.nextInt();
for(int i = 0; i < numInts; i++) {
int currentNum = sc.nextInt();
numXs[currentNum - 1]++;
}
int numXsMax = Integer.MIN_VALUE;
for(int i = 0; i < numXs.length; i++) {
if(numXs[i] > numXsMax) {
numXsMax = numXs[i];
}
}
System.out.println(numInts - numXsMax);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
z=list(map(int,input().split()))
one=0
two=0
three=0
for i in z :
if i==1 :
one+=1
elif i==2 :
two+=1
else :
three+=1
if one > two and one > three :
print(n-one)
elif two>one and two >three :
print(n-two)
elif three>one and three>two :
print(n-three)
else :
print(n-max(one,two,three)) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | total = int(input())
c1 = 0
c2 = 0
c3 = 0
for i in input().split():
if i == '1':
c1 += 1
elif i == '2':
c2 += 1
elif i == '3':
c3 += 1
print(total - max(max(c1,c2),c3)) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
a=input()
print(n-max(a.count(x)for x in '123')) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.*;
public class CF102 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
int[] ar=new int[t];
int[] count=new int[4];
for(int i=0;i<t;i++){
int num=sc.nextInt();
count[num]++;
}
int max=Math.max(count[1],Math.max(count[2],count[3]));
int ans=count[1]+count[2]+count[3]-max;
System.out.println(ans);
}
public static int getAns(char[] s1, int i, int j, int[][] dp) {
if (i > j) {
return 0;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
if (s1[i] == s1[j]) {
dp[i][j] = getAns(s1, i + 1, j - 1, dp);
} else {
int ans1 = getAns(s1, i + 1, j, dp);
int ans2 = getAns(s1, i, j - 1, dp);
dp[i][j] = 1 + Math.min(ans1, ans2);
}
return dp[i][j];
}
public static boolean funky(long mid, long n, long orig) {
long vas = 0;
while (n > 0) {
long y = Math.min(mid, n);
n -= y;
vas = vas + y;
n -= n / 10;
}
return 2 * vas >= orig;
}
public static long sweetness(long a) {
long c = 0;
for (long i = 1; i <= a; i++) {
if (gcd(i, a) == 1) {
c++;
}
}
return c;
}
public static long power(long a, long b, long c) {
long ans = 1;
while (b != 0) {
if (b % 2 == 1) {
ans = ans * a;
ans %= c;
}
a = a * a;
a %= c;
b /= 2;
}
return ans;
}
public static long power1(long a, long b, long c) {
long ans = 1;
while (b != 0) {
if (b % 2 == 1) {
ans = multiply(ans, a, c);
}
a = multiply(a, a, c);
b /= 2;
}
return ans;
}
public static long multiply(long a, long b, long c) {
long res = 0;
a %= c;
while (b > 0) {
if (b % 2 == 1) {
res = (res + a) % c;
}
a = (a + a) % c;
b /= 2;
}
return res % c;
}
public static long totient(long n) {
long result = n;
for (long i = 2; i * i <= n; i++) {
if (n % i == 0) {
//sum=sum+2*i;
while (n % i == 0) {
n /= i;
// sum=sum+n;
}
result -= result / i;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
public static boolean[] primes(int n) {
boolean[] p = new boolean[n + 1];
p[0] = false;
p[1] = false;
for (int i = 2; i <= n; i++) {
p[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (p[i]) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
public int minimumLength(String s) {
String s1 = "";
for (int i = 0; i < s.length() - 1; i++) {
char ch = s.charAt(i);
char ch1 = s.charAt(i + 1);
if (ch != ch1) {
s1 = s1 + ch;
}
}
s1 = s1 + s.charAt(s.length() - 1);
String[] ar = s1.split("");
for (int i = 0; i < s1.length() - 1; i++) {
if (i <= s1.length() - 1 - i) {
char ch1 = ar[i].charAt(0);
char ch2 = ar[s1.length() - 1 - i].charAt(0);
if (ch1 != ch2) {
break;
} else {
ar[i] = "";
ar[s1.length() - 1 - i] = "";
}
} else {
break;
}
}
String s2 = "";
for (String s3 : ar) {
s2 = s2 + s3;
}
return s2.length();
}
public static long[] totientfrom1ton(long n) {
long[] totient = new long[(int) (n + 1)];
totient[0] = 0;
totient[1] = 1;
for (int i = 2; i <= n; i++) {
totient[i] = i;
}
for (int i = 2; i <= n; i++) {
if (totient[i] == i) {
totient[i] -= 1;
for (int j = 2 * i; j <= n; j += i) {
totient[j] -= totient[j] / i;
}
}
}
return totient;
}
static long[] LcmSum(int n, long[] phi) {
//ETF();
long[] ans = new long[n + 1];
for (int i = 1; i <= n; i++) {
// Summation of d * ETF(d) where
// d belongs to set of divisors of n
for (int j = i; j <= n; j += i) {
ans[j] += (i * phi[i]);
}
}
for (int i = 1; i <= n; i++) {
ans[i] = ((ans[i] + 1) * i) / 2;
}
/*int answer = ans[m];
answer = (answer + 1) * m;
answer = answer / 2;*/
return ans;
}
static int hackerlandRadioTransmitters(int[] x, int k) {
Arrays.sort(x);
int c = 0;
int lol = 0;
process:
while (lol <= (x.length - 1)) {
int initial = x[lol];
int nextno = initial + 2 * k;
int low = lol + 1;
int high = x.length - 1;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (x[mid] <= nextno) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
if (ans != -1) {
c++;
}
System.out.println(ans + " " + lol);
lol = ans + 1;
}
return c;
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | x = raw_input()
y = raw_input().split()
y = sorted(y)
y = map(int, y)
c1 = y.count(1)
c2 = y.count(2)
c3 = y.count(3)
if c1>=c2 and c1>=c3:
print c2+c3
elif c2>=c1 and c2>=c3:
print c1+c3
elif c3>=c1 and c3>=c2:
print c1+c2
| PYTHON |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int cont[4];
memset(cont, 0, sizeof cont);
for (int i = 0, a; i < n; ++i) {
scanf("%d", &a);
++cont[a];
}
int ans = n;
for (int i = 1; i <= 3; ++i)
ans = min(ans, cont[1] + cont[2] + cont[3] - cont[i]);
printf("%d\n", ans);
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
a=[int(ii) for ii in input().split()]
cnt=[0]*5
for i in a:
cnt[i]+=1
print(len(a)-max(cnt))
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int b[5] = {0, 0, 0, 0, 0};
for (int i = 0; i < n; i++) {
if (a[i] == 1)
b[1]++;
else if (a[i] == 2)
b[2]++;
else if (a[i] == 3)
b[3]++;
}
sort(b, b + 4);
reverse(b, b + 4);
cout << n - b[0];
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
in.readLine();
StringTokenizer a = new StringTokenizer(in.readLine());
int[] q= new int[3];
while (a.hasMoreTokens()){
int temp = a.nextToken().charAt(0)-'1';
q[temp]++;
}
int s1=q[0]+q[1];
int s2=q[2]+q[1];
int s3=q[2]+q[0];
System.out.println(Math.min(Math.min(s1, s2), s3));
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
L=[0,0,0]
M=list(map(int,input().split()))
for i in range(0,n):
if(M[i]==1):
L[0]+=1
elif(M[i]==2):
L[1]+=1
else:
L[2]+=1
L.sort()
print(L[0]+L[1]) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
a = list(map(int, input().split()))
print(n - max(a.count(i) for i in range(1, 4)))
| PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n = int(input())
seq = list(map(int,input().strip().split(' ')))
print(n - max([seq.count(1),seq.count(2),seq.count(3)])) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class seq {
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int n = sc.nextInt();
int s [] = new int[n];
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < s.length; i++) {
s[i] = sc.nextInt();
}
for (int i = 0; i < s.length; i++) {
if(s[i] == 1){
a++;
}else{
if(s[i] == 2){
b++;
}else{
c++;
}
}
}
int y = n - Math.max((Math.max(a , b)) , c);
System.out.println(y);
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.util.*;
public class Slotuin {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int c1 = 0,c2 = 0,c3 = 0;
int n = x.nextInt();
int z = 0;
for(int i = 0;i < n;i++)
{
z = x.nextInt();
if(z == 1)c1++;
if(z == 2)c2++;
if(z == 3)c3++;
}
int d = Math.max(c1, Math.max(c2, c3));
System.out.print(n - d);
x.close();
}
} | JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Test {
public BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out), true);
public StringTokenizer st = new StringTokenizer("");
private String nextString() throws IOException{
while (!st.hasMoreElements()) {
st = new StringTokenizer(input.readLine());
}
return st.nextToken();
}
private int nextInt() throws IOException{
return Integer.valueOf(nextString());
}
private byte nextByte() throws IOException{
return Byte.valueOf(nextString());
}
private long nextLong() throws IOException{
return Long.valueOf(nextString());
}
/////////////////// START /////////////////////////
private void poehali() throws IOException{
int n = nextInt();
int a[] = new int[4];
for (int i = 0; i < n; i++) {
a[nextByte()]++;
}
pw.println(n-Math.max(Math.max(a[1], a[2]), a[3]));
}
////////////////////// MAIN ///////////////////////
public static void main(String[] args) throws IOException {
new Test().poehali();
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 |
n = int(input())
secuencia = input().split()
c1 = 0
c2 = 0
c3 = 0
for i in range(n):
if secuencia[i] == '1':
c1 = c1+1
elif secuencia[i] == '2':
c2 = c2+1
else:
c3 = c3+1
maximo = max(c1,c2,c3)
rpta = c1+c2+c3 - maximo
print(rpta) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int maax(vector<int> w) {
int temp = -1;
for (int k = 0; k < w.size(); k++) {
if (w[k] > temp) {
temp = w[k];
}
}
return temp;
}
int main() {
bool b = false;
bool n = false;
vector<int> e(3);
e[0] = 0;
e[1] = 0;
e[2] = 0;
int x = 0;
cin >> x;
int j = 0;
vector<int> a;
for (int i = 0; i < x; i++) {
cin >> j;
a.push_back(j);
}
for (int o = 0; o < x; o++) {
if (a[o] == 1) {
e[0]++;
}
if (a[o] == 2) {
e[1]++;
}
if (a[o] == 3) {
e[2]++;
}
}
int d = 0;
int z = maax(e);
if (z == e[0]) {
d = e[2] + e[1];
cout << d << endl;
b = true;
}
if (z == e[1] && b == false) {
d = e[0];
d += e[2];
cout << d << endl;
n = true;
}
if (z == e[2] && b == false && n == false) {
d = e[1] + e[0];
cout << d << endl;
}
return 0;
}
| CPP |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.*;
import java.util.*;
public class A
{
String line;
StringTokenizer inputParser;
BufferedReader is;
FileInputStream fstream;
DataInputStream in;
void openInput(String file)
{
if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
else
{
try{
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
is = new BufferedReader(new InputStreamReader(in));
}catch(Exception e)
{
System.err.println(e);
}
}
}
void readNextLine()
{
try {
line = is.readLine();
inputParser = new StringTokenizer(line, " ");
//System.err.println("Input: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
int NextInt()
{
String n = inputParser.nextToken();
int val = Integer.parseInt(n);
//System.out.println("I read this number: " + val);
return val;
}
String NextString()
{
String n = inputParser.nextToken();
return n;
}
void closeInput()
{
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
public static void main(String [] argv)
{
String filePath=null;
if(argv.length>0)filePath=argv[0];
A a = new A(filePath);
}
public A(String inputFile)
{
openInput(inputFile);
readNextLine();
int N=NextInt();
readNextLine();
int [] p = new int[3];
for(int i=0; i<N; i++)
{
int a=NextInt()-1;
p[a]++;
}
int ret=p[0]+p[1];
if(p[1]+p[2]<ret)ret=p[1]+p[2];
if(p[0]+p[2]<ret)ret=p[0]+p[2];
System.out.println(ret);
closeInput();
}
}
| JAVA |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | n=int(input())
l=list(map(int,input().split()))
li=[l.count(1),l.count(2),l.count(3)]
li.sort()
print(li[0]+li[1]) | PYTHON3 |
52_A. 123-sequence | There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
Input
The first line contains an integer n (1 β€ n β€ 106). The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 3).
Output
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
Examples
Input
9
1 3 2 2 2 1 1 2 3
Output
5
Note
In the example all the numbers equal to 1 and 3 should be replaced by 2. | 2 | 7 | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Alex
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.ri();
int[] a = IOUtils.readIntArray(in, n);
int res = Integer.MAX_VALUE;
for(int i = 1; i <= 3; i++) {
int count = 0;
for(int j = 0; j < a.length; j++) if(a[j] != i) count++;
res = Math.min(res, count);
}
out.printLine(res);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ri(){
return readInt();
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct SegmentMaxTree {
SegmentMaxTree(int default_value, int n) : default_value(default_value) {
sz = 1;
while (sz < n) {
sz = sz << 1;
}
items.resize(2 * sz - 1, default_value);
}
void Set(int first, int last, int value) {
return Set(first, last, value, 0, 0, sz - 1);
}
void Set(int first, int last, int value, size_t node, int segment_begin,
int segment_last) {
first = max(first, segment_begin);
last = min(last, segment_last);
if (first > last) {
return;
}
if (first == segment_begin && last == segment_last) {
items[node] = max(value, items[node]);
} else {
auto segment_mid = (segment_begin + segment_last + 1) / 2;
Set(first, last, value, node * 2 + 1, segment_begin, segment_mid - 1);
Set(first, last, value, node * 2 + 2, segment_mid, segment_last);
}
}
int Get(int idx) { return Get(idx, 0, 0, sz - 1); }
int Get(int idx, size_t node, int segment_begin, int segment_last) {
int best_value = items[node];
if (segment_begin == segment_last) {
return best_value;
}
auto segment_mid = (segment_begin + segment_last + 1) / 2;
if (segment_mid <= idx) {
best_value =
max(best_value, Get(idx, 2 * node + 2, segment_mid, segment_last));
} else {
best_value = max(best_value,
Get(idx, 2 * node + 1, segment_begin, segment_mid - 1));
}
return best_value;
}
int default_value, sz;
vector<int> items;
};
struct CompressedSegmentMaxTree {
CompressedSegmentMaxTree(int default_value, const set<long long>& items)
: tree(default_value, items.size()) {
int i = 0;
for (auto el : items) {
element2index[el] = i++;
}
}
void Set(int first, int last, int value) {
tree.Set(element2index.at(first), element2index.at(last), value);
}
int Get(int pos) { return tree.Get(element2index.at(pos)); }
SegmentMaxTree tree;
map<long long, int> element2index;
};
struct Record {
int row, col;
char c;
};
void solve() {
int n, q;
cin >> n >> q;
vector<Record> records(q);
set<long long> coordinates;
for (int(i) = 0; (i) < (q); (i)++) {
Record& rec = records[i];
cin >> rec.col >> rec.row >> rec.c;
rec.row--;
rec.col--;
coordinates.insert(rec.col);
coordinates.insert(rec.row);
}
coordinates.insert(-1);
CompressedSegmentMaxTree max_eaten_by_cols(-1, coordinates);
CompressedSegmentMaxTree max_eaten_by_rows(-1, coordinates);
set<int> seen_left, seen_up;
for (auto& rec : records) {
int row = rec.row;
int col = rec.col;
char c = rec.c;
auto& active_set = (c == 'L') ? max_eaten_by_rows : max_eaten_by_cols;
auto& anti_active_set = (c != 'L') ? max_eaten_by_rows : max_eaten_by_cols;
int point = (c == 'L') ? row : col;
int anti_point = (c != 'L') ? row : col;
int can_eat_upto = active_set.Get(point);
if (seen_left.find(row) != seen_left.end() ||
seen_up.find(col) != seen_up.end()) {
can_eat_upto = anti_point;
} else {
anti_active_set.Set(can_eat_upto, anti_point, point);
}
printf("%d\n", (anti_point - can_eat_upto));
if (c == 'L') {
seen_left.insert(row);
} else {
seen_up.insert(col);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
solve();
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.util.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class Main{
// ArrayList<Integer> lis = new ArrayList<Integer>();
// ArrayList<String> lis = new ArrayList<String>();
// PriorityQueue<P> que = new PriorityQueue<P>();
// PriorityQueue<Integer> que = new PriorityQueue<Integer>();
// Stack<Integer> stack = new Stack<Integer>();
//HashMap<Long,Long> map = new HashMap<Long,Long>();
// static long sum=0;
// 1000000007 (10^9+7)
//static int mod = 1000000007;
static long mod = 1000000007;
//static int mod = 1000000009; ArrayList<Integer> l[]= new ArrayList[n];
// static int dx[]={1,-1,0,0};
// static int dy[]={0,0,1,-1};
// //static int dx[]={1,-1,0,0,1,1,-1,-1};
//static int dy[]={0,0,1,-1,1,-1,1,-1};
//static Set<Integer> set = new HashSet<Integer>();
//static ArrayList<Integer> l[];
//static int parent[][],depth[],node,max_log;
//static ArrayList<Integer> l[];
public static void main(String[] args) throws Exception, IOException{
//Scanner sc =new Scanner(System.in);
Reader sc = new Reader(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt(),m=sc.nextInt();
n++;
TreeSet <Long> ts=new TreeSet<Long>();
long mx=n;
int sf=31,bt=1<<30; bt--;
ts.add(1L);
ts.add(mx<<sf);
for( int i=0; i<m; i++ ){
long x=sc.nextInt(), y=sc.nextInt();
char c=sc.nextString().toCharArray()[0];
long h=x<<sf,g=0,yy=0,d=0;
if( x==(ts.ceiling(h)>>sf) ){ out.println(0); continue; }
if(c=='U'){
g=ts.ceiling(h);
if(g%2==1){ g>>=1; long f=g&bt; g>>=30; yy=abs(n-g); d=f+(-yy+y); }
else { g>>=sf; yy=abs(n-g); d=-yy+y+1; }
g=x<<sf; g+=d<<1; g+=1;
ts.add(g);
out.println(d-1);
}else {
g=ts.floor(h);
if(g%2==0){ g>>=1; long f=g&bt; g>>=30; d=f+(x-g); }
else { g>>=sf; d=x-g+1; }
g=x<<sf; g+=d<<1;
ts.add(g);
out.println(d-1);
}
}
// out.println();
out.flush();
// System.out.println();
}
static long gcd(long a, long b){
if(min(a,b) == 0) return max(a,b);
return gcd(max(a, b) % min(a,b), min(a,b));
}
static void db(Object... os){
System.err.println(Arrays.deepToString(os));
}
static boolean validpos(int x,int y,int r, int c){
return x<r && 0<=x && y<c && 0<=y;
}
static boolean bit(long x,int k){
// weather k-th bit (from right) be one or zero
return ( 0 < ( (x>>k) & 1 ) ) ? true:false;
}
}
class Reader
{
private BufferedReader x;
private StringTokenizer st;
public Reader(InputStream in)
{
x = new BufferedReader(new InputStreamReader(in));
st = null;
}
public String nextString() throws IOException
{
while( st==null || !st.hasMoreTokens() )
st = new StringTokenizer(x.readLine());
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(nextString());
}
public long nextLong() throws IOException
{
return Long.parseLong(nextString());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(nextString());
}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int Q = in.nextInt();
int [] start = new int[Q];
boolean [] isup = new boolean[Q];
HashSet<Integer> done = new HashSet<Integer>();
TreeMap<Integer,Integer> goleft = new TreeMap<Integer,Integer>();
TreeMap<Integer,Integer> goup = new TreeMap<Integer,Integer>();
StringBuilder ret = new StringBuilder();
for(int i=0;i<Q;i++) {
start[i] = in.nextInt();
in.nextInt();
isup[i] = in.next().charAt(0) == 'U';
if(done.contains(start[i])) {
ret.append("0\n");
//System.out.println(0);
continue;
}
done.add(start[i]);
if(isup[i]) {
Entry<Integer,Integer> e = goup.ceilingEntry(start[i]);
int endval=N+1-start[i];
if(e!=null) {
//System.out.println("EU " + (N+1 - e.getValue()));
endval-=(N+1 - e.getValue());
}
ret.append(endval);
ret.append('\n');
//System.out.println(endval);
if(e!=null) {
Entry<Integer,Integer> olde = goleft.floorEntry(e.getValue());
int old = olde==null ? 0 : olde.getValue();
goleft.put(e.getValue(), old);
}
goleft.put(start[i], start[i]);
}
else {
Entry<Integer,Integer> e = goleft.floorEntry(start[i]);
int endval=start[i];
if(e!=null) {
//System.out.println("EL " + e.getValue());
endval-=e.getValue();
}
ret.append(endval);
ret.append('\n');
//System.out.println(endval);
if(e!=null) {
Entry<Integer,Integer> olde = goup.ceilingEntry(e.getValue());
int old = olde==null ? N+1 : olde.getValue();
goup.put(e.getValue(), old);
}
goup.put(start[i], start[i]);
}
}
System.out.println(ret);
}
}
/*
10 1000
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
100000 100000
10 10 L
5 5 U
9 9 L
1 1 U
*/ | JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(200005);
int x[MAXN], y[MAXN], numx[MAXN], numy[MAXN], t[2][4 * MAXN];
char c[MAXN];
map<int, int> tx, ty, kx, ky;
map<pair<int, int>, bool> used;
void init(int i, int f, int l, int c) {
t[c][i] = -INF;
if (f == l) return;
int m = (f + l) / 2;
init(i * 2, f, m, c);
init(i * 2 + 1, m + 1, l, c);
}
int query(int i, int f, int l, int pos, int c) {
if (f == l) return t[c][i];
int m = (f + l) / 2;
if (pos <= m) return max(t[c][i], query(i * 2, f, m, pos, c));
return max(t[c][i], query(i * 2 + 1, m + 1, l, pos, c));
}
void update(int i, int f, int l, int st, int fn, int val, int c) {
if (f == st && l == fn) {
t[c][i] = max(t[c][i], val);
return;
}
int m = (f + l) / 2;
if (fn <= m)
update(i * 2, f, m, st, fn, val, c);
else if (st > m)
update(i * 2 + 1, m + 1, l, st, fn, val, c);
else {
update(i * 2, f, m, st, m, val, c);
update(i * 2 + 1, m + 1, l, m + 1, fn, val, c);
}
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < q; i++) {
scanf("%d%d %c", &x[i], &y[i], &c[i]);
tx[x[i]] = 1;
ty[y[i]] = 1;
}
int numX = 0;
for (typeof(tx.begin()) it = tx.begin(); it != tx.end(); it++) {
kx[it->first] = ++numX;
numx[numX] = it->first;
}
int numY = 0;
for (typeof(ty.begin()) it = ty.begin(); it != ty.end(); it++) {
ky[it->first] = ++numY;
numy[numY] = it->first;
}
init(1, 1, numX, 0);
init(1, 1, numY, 1);
for (int i = 0; i < q; i++) {
if (used[pair<int, int>(x[i], y[i])]) {
printf("0\n");
continue;
}
used[pair<int, int>(x[i], y[i])] = 1;
int first = kx[x[i]], second = ky[y[i]];
if (c[i] == 'U') {
int k = query(1, 1, numX, first, 0);
if (k == -INF) {
printf("%d\n", y[i]);
update(1, 1, numY, 1, second, first, 1);
} else {
printf("%d\n", y[i] - numy[k]);
update(1, 1, numY, k, second, first, 1);
}
} else {
int k = query(1, 1, numY, second, 1);
if (k == -INF) {
printf("%d\n", x[i]);
update(1, 1, numX, 1, first, second, 0);
} else {
printf("%d\n", x[i] - numx[k]);
update(1, 1, numX, k, first, second, 0);
}
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 1 << 30;
struct tournament {
struct Node {
Node* parent;
set<int, greater<int>> blocked;
Node* left = nullptr;
Node* right = nullptr;
Node(Node* parent) : parent(parent) {}
}* root = new Node(nullptr);
set<int, greater<int>>::iterator it;
void update(int x, int start, int end, int lo = 0, int hi = MAXN,
Node* node = nullptr) {
if (node == nullptr) node = root;
if (lo >= end || hi <= start) return;
if (lo >= start && hi <= end) {
node->blocked.insert(x);
return;
}
int mid = (lo + hi) / 2;
if (mid > start) {
if (node->left == nullptr) node->left = new Node(node);
update(x, start, end, lo, mid, node->left);
}
if (mid < end) {
if (node->right == nullptr) node->right = new Node(node);
update(x, start, end, mid, hi, node->right);
}
}
int query(int x, int pos) {
Node* node = root;
int lo = 0, hi = MAXN;
int sol = -1;
while (node != nullptr) {
if ((it = node->blocked.lower_bound(x)) != node->blocked.end() &&
*it > sol)
sol = *it;
int mid = (lo + hi) / 2;
if (pos < mid)
hi = mid, node = node->left;
else
lo = mid, node = node->right;
}
return sol;
}
} rows, cols;
set<int> bio;
int unzipA[200100], unzipB[200100];
pair<int, int> a[200100], b[200100];
char dirs[200100];
int main() {
ios_base::sync_with_stdio(false);
int n, q, i, j, x;
char dir;
cin >> n >> q;
rows.update(-1, 0, MAXN), cols.update(-1, 0, MAXN);
for (i = 0; i < q; ++i) {
cin >> b[i].first >> a[i].first >> dirs[i];
--a[i].first, --b[i].first;
a[i].second = b[i].second = i;
}
sort(a, a + q);
sort(b, b + q);
new (unzipA - 1) int;
new (unzipB - 1) int;
unzipA[-1] = unzipB[-1] = -1;
for (i = 0; i < q; ++i) {
unzipA[i] = a[i].first;
unzipB[i] = b[i].first;
a[i].first = b[i].first = i;
}
sort(a, a + q, [](const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second;
});
sort(b, b + q, [](const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second;
});
for (int k = 0; k < q; ++k) {
i = a[k].first;
j = b[k].first;
dir = dirs[k];
if (bio.count(MAXN + i - j)) {
cout << "0\n";
continue;
}
bio.insert(MAXN + i - j);
switch (dir) {
case 'U':
x = rows.query(i, j);
cout << unzipA[i] - unzipA[x] << endl;
cols.update(j, x, i);
break;
case 'L':
x = cols.query(j, i);
cout << unzipB[j] - unzipB[x] << endl;
rows.update(i, x + 1, j + 1);
break;
}
}
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.