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 |
|---|---|---|---|---|---|
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 2034567891;
const long long int INF64 = 1234567890123456789ll;
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
;
long long int x, y, a, b, c;
cin >> x >> y;
if (2 * y > x) {
cout << "3";
return 0;
}
if (2 * y == x) {
cout << "4";
return 0;
}
a = y, b = y, c = y;
long long int ans = 0;
while (c < x) {
c = b;
b = a;
a = b + c - 1;
ans++;
}
cout << ans;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.io.*;
import java.util.*;
public class No712C {
public static void main (String [] args) throws IOException {
No712C program = new No712C();
program.solve();
}
int x;
int y;
public void solve() throws IOException {
/*Scanner sc = null;
try {
sc = new Scanner(new File("C:/Users/John/workspace/CodeForces/src/input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
int[] triangle = new int[] {y, y, y};
int steps = 0;
while(triangle[0] != x || triangle[1] != x || triangle[2] != x) {
int minIndex = minIndex(triangle);
int cap = triangle[0] + triangle[1] + triangle[2] - triangle[minIndex];
triangle[minIndex] = Math.min(x, cap - 1);
steps++;
}
System.out.println(steps);
}
int minIndex(int[] triangle) {
int minIndex = 0;
int minVal = Integer.MAX_VALUE;
for (int i = 0; i < 3; i++) {
if (triangle[i] < minVal) {
minIndex = i;
minVal = triangle[i];
}
}
return minIndex;
}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.util.Arrays;
import java.util.Scanner;
public class MemoryDevolution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
int[] arr = new int[3];
for(int i = 0; i < 3; i++){
arr[i] = y;
}
int count = 0;
while(arr[0] < x){
arr[0] = Math.min(x, arr[2]+arr[1]-1);
Arrays.sort(arr);
count++;
}
System.out.println(count);
}
}
| JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
while (scanf("%d%d", &n, &m) == 2) {
int a = m, b = m, c = m, cnt = 0;
while (true) {
if (a == n && b == n && c == n) {
break;
}
if (cnt % 3 == 0) {
c = min(n, a + b - 1);
} else if (cnt % 3 == 1) {
a = min(n, b + c - 1);
} else {
b = min(n, a + c - 1);
}
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int arr[] = new int[3];
arr[0] = y;
arr[1] = y;
arr[2] = y;
int answer=0;
while ((arr[0] < x) || (arr[1] < x) || (arr[2] < x)) {
Arrays.sort(arr);
arr[0] = arr[1] + arr[2] - 1;
answer++;
}
System.out.println(answer);
}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Main {
static ContestScanner in;static Writer out;public static void main(String[] args)
{try{in=new ContestScanner();out=new Writer();Main solve=new Main();solve.solve();
in.close();out.flush();out.close();}catch(IOException e){e.printStackTrace();}}
static void dump(int[]a){for(int i=0;i<a.length;i++)out.print(a[i]+" ");out.println();}
static void dump(int[]a,int n){for(int i=0;i<a.length;i++)out.printf("%"+n+"d ",a[i]);out.println();}
static void dump(long[]a){for(int i=0;i<a.length;i++)out.print(a[i]+" ");out.println();}
static void dump(char[]a){for(int i=0;i<a.length;i++)out.print(a[i]);out.println();}
static long pow(long a,int n){long r=1;while(n>0){if((n&1)==1)r*=a;a*=a;n>>=1;}return r;}
static String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');}
static void sort(int[]a){m_sort(a,0,a.length,new int[a.length]);}
static void sort(int[]a,int l){m_sort(a,0,l,new int[l]);}
static void sort(int[]a,int l,int[]buf){m_sort(a,0,l,buf);}
static void sort(int[]a,int s,int l,int[]buf){m_sort(a,s,l,buf);}
static void m_sort(int[]a,int s,int sz,int[]b)
{if(sz<7){for(int i=s;i<s+sz;i++)for(int j=i;j>s&&a[j-1]>a[j];j--)swap(a, j, j-1);return;}
m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int idx=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz;
while(l<le&&r<re){if(a[l]>a[r])b[idx++]=a[r++];else b[idx++]=a[l++];}
while(r<re)b[idx++]=a[r++];while(l<le)b[idx++]=a[l++];for(int i=s;i<s+sz;i++)a[i]=b[i];
} /* qsort(3.5s)<<msort(9.5s)<<<shuffle+qsort(17s)<Arrays.sort(Integer)(20s) */
static void sort(long[]a){m_sort(a,0,a.length,new long[a.length]);}
static void sort(long[]a,int l){m_sort(a,0,l,new long[l]);}
static void sort(long[]a,int l,long[]buf){m_sort(a,0,l,buf);}
static void sort(long[]a,int s,int l,long[]buf){m_sort(a,s,l,buf);}
static void m_sort(long[]a,int s,int sz,long[]b)
{if(sz<7){for(int i=s;i<s+sz;i++)for(int j=i;j>s&&a[j-1]>a[j];j--)swap(a, j, j-1);return;}
m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int idx=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz;
while(l<le&&r<re){if(a[l]>a[r])b[idx++]=a[r++];else b[idx++]=a[l++];}
while(r<re)b[idx++]=a[r++];while(l<le)b[idx++]=a[l++];for(int i=s;i<s+sz;i++)a[i]=b[i];}
static void swap(long[] a,int i,int j){final long t=a[i];a[i]=a[j];a[j]=t;}
static void swap(int[] a,int i,int j){final int t=a[i];a[i]=a[j];a[j]=t;}
static int binarySearchSmallerMax(int[]a,int v)// get maximum index which a[idx]<=v
{int l=-1,r=a.length-1,s=0;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return s;}
static int binarySearchSmallerMax(int[]a,int v,int l,int r)
{int s=-1;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return s;}
@SuppressWarnings("unchecked")
static List<Integer>[]createGraph(int n)
{List<Integer>[]g=new List[n];for(int i=0;i<n;i++)g[i]=new ArrayList<>();return g;}
void solve() throws IOException{
int s = in.nextInt();
int t = in.nextInt();
int[] x = new int[3];
for(int i=0; i<3; i++) x[i] = t;
int cnt = 0;
while(x[0]!=s || x[1]!=s || x[2]!=s){
cnt++;
int sum = 0;
int min = s+1;
int minI = -1;
for(int i=0; i<3; i++){
sum += x[i];
if(x[i]<min){
minI = i;
min = x[i];
}
}
int nxt = sum - min -1;
if(nxt>s) nxt = s;
x[minI] = nxt;
}
System.out.println(cnt);
}
}
@SuppressWarnings("serial")
class MultiSet<T> extends HashMap<T, Integer>{
@Override public Integer get(Object key){return containsKey(key)?super.get(key):0;}
public void add(T key,int v){put(key,get(key)+v);}
public void add(T key){put(key,get(key)+1);}
public void sub(T key){final int v=get(key);if(v==1)remove(key);else put(key,v-1);}
public MultiSet<T> merge(MultiSet<T> set)
{MultiSet<T>s,l;if(this.size()<set.size()){s=this;l=set;}else{s=set;l=this;}
for(Entry<T,Integer>e:s.entrySet())l.add(e.getKey(),e.getValue());return l;}
}
@SuppressWarnings("serial")
class OrderedMultiSet<T> extends TreeMap<T, Integer>{
@Override public Integer get(Object key){return containsKey(key)?super.get(key):0;}
public void add(T key,int v){put(key,get(key)+v);}
public void add(T key){put(key,get(key)+1);}
public void sub(T key){final int v=get(key);if(v==1)remove(key);else put(key,v-1);}
public OrderedMultiSet<T> merge(OrderedMultiSet<T> set)
{OrderedMultiSet<T>s,l;if(this.size()<set.size()){s=this;l=set;}else{s=set;l=this;}
while(!s.isEmpty()){l.add(s.firstEntry().getKey(),s.pollFirstEntry().getValue());}return l;}
}
class Pair implements Comparable<Pair>{
int a,b;final int hash;Pair(int a,int b){this.a=a;this.b=b;hash=(a<<16|a>>16)^b;}
public boolean equals(Object obj){Pair o=(Pair)(obj);return a==o.a&&b==o.b;}
public int hashCode(){return hash;}
public int compareTo(Pair o){if(a!=o.a)return a<o.a?-1:1;else if(b!=o.b)return b<o.b?-1:1;return 0;}
}
class Timer{
long time;public void set(){time=System.currentTimeMillis();}
public long stop(){return time=System.currentTimeMillis()-time;}
public void print(){System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");}
@Override public String toString(){return"Time: "+time+"ms";}
}
class Writer extends PrintWriter{
public Writer(String filename)throws IOException
{super(new BufferedWriter(new FileWriter(filename)));}
public Writer()throws IOException{super(System.out);}
}
class ContestScanner implements Closeable{
private BufferedReader in;private int c=-2;
public ContestScanner()throws IOException
{in=new BufferedReader(new InputStreamReader(System.in));}
public ContestScanner(String filename)throws IOException
{in=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));}
public String nextToken()throws IOException {
StringBuilder sb=new StringBuilder();
while((c=in.read())!=-1&&Character.isWhitespace(c));
while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();}
return sb.toString();
}
public String readLine()throws IOException{
StringBuilder sb=new StringBuilder();if(c==-2)c=in.read();
while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();}
return sb.toString();
}
public long nextLong()throws IOException,NumberFormatException
{return Long.parseLong(nextToken());}
public int nextInt()throws NumberFormatException,IOException
{return(int)nextLong();}
public double nextDouble()throws NumberFormatException,IOException
{return Double.parseDouble(nextToken());}
public void close() throws IOException {in.close();}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.io.*;
import java.util.*;
public class C
{
public static void main(String[] args)
{
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int a = sc.nextInt(), b = sc.nextInt();
ArrayList<Integer> inp = new ArrayList<Integer>();
inp.add(b);
inp.add(b);
inp.add(b);
Collections.sort(inp);
int ans = 0;
while(true) {
//System.out.printf("%d %d %d %d\n", inp.get(0), inp.get(1), inp.get(2), a);
if( inp.get(0).intValue() == inp.get(1).intValue() && inp.get(1).intValue() == inp.get(2).intValue()
&& inp.get(0).intValue() == a )
break;
ans++;
inp.remove(0);
inp.add(Math.min(inp.get(0) + inp.get(1) - 1, a));
Collections.sort(inp);
}
out.println(ans);
out.close();
}
// PrintWriter for faster output
public static PrintWriter out;
// MyScanner class for faster input
public static class MyScanner
{
BufferedReader br;
StringTokenizer st;
public MyScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
return false;
}
}
return true;
}
String next()
{
if (hasNext())
return st.nextToken();
return null;
}
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;
}
}
}
| JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int x, y, a[4], counter = 0;
cin >> x >> y;
a[0] = a[1] = a[2] = y;
while (a[0] != a[1] || a[1] != a[2] || a[1] != x) {
sort(a, a + 3);
a[0] = a[1] + a[2];
if (a[0] > x)
a[0] = x;
else
a[0]--;
counter++;
}
cout << counter << endl;
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, y, a, b, c, ans;
scanf("%d%d", &y, &x);
a = b = c = x;
ans = 0;
while (a < y || b < y || c < y) {
ans++;
a = b + c - 1;
swap(a, b);
swap(b, c);
}
printf("%d\n", ans);
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | x,y = map(int,input().split())
turn = 0
a = b = c = y
while True:
if a >= x and b >= x and c >= x:
print(turn)
break
if turn % 3 == 0:
a = b + c - 1
if turn % 3 == 1:
b = a + c - 1
if turn % 3 == 2:
c = a + b - 1
turn += 1 | PYTHON3 |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
scanf("%d%d", &x, &y);
int a[3], ans = 0;
a[0] = a[1] = a[2] = y;
while (a[0] < x) {
a[0] = a[1] + a[2] - 1;
sort(a, a + 3);
++ans;
}
printf("%d\n", ans);
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
int a[3];
int ans = 0;
int n = 3;
for (int i = 0; i < n; i++) a[i] = y;
while (1) {
sort(a, a + n);
if (a[0] == x) break;
ans++;
a[0] = min(a[1] + a[2] - 1, x);
}
cout << ans << endl;
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws IOException {
FastScanner in= new FastScanner(System.in);
int x= in.nextInt();
int y= in.nextInt();
int [] tri= new int[3];
Arrays.fill(tri, y);
int res= 0;
while(true){
Arrays.sort(tri);
if(tri[0]==x&&tri[1]==x&&tri[2]==x){
System.out.println(res);
return;
}
res++;
tri[0]= Math.min(x, tri[1] + tri[2] - 1);
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
public String next() throws IOException {
if(!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
return next();
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
}
class edge implements Comparable<edge>{
int u;
long cost;
public edge(int a, long b){
u= a;
cost= b;
}
public int compareTo(edge o) {
return Long.signum(cost-o.cost);
}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | n ,m = map(int,input().split())
s = [m, m, m]
ans = 0
while s[0] < n:
temp = min(n, s[1] + s[2] - 1)
s[0] = s[1]
s[1] = s[2]
s[2] = temp
ans += 1
print(ans) | PYTHON3 |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | # your code goes here
a=map(int,raw_input().split( ))
#b=[a[0],a[0],a[0]]
c=[a[1],a[1],a[1]]
if 2*a[1]>a[0]:
print 3
else:
cnt=0
while c[0]!=a[0] or c[1]!=a[0] or c[2]!=a[0]:
if c[0]==c[1] and c[1]==c[2]:
c[0]=2*c[0]-1
cnt+=1
else:
mn=min(c)
mx=max(c)
if c[0]==mn and c[0]!=a[0]:
if c[1]==mx:
c[0]=min(mx-1+c[2],a[0])
else:
c[0]=min(mx-1+c[1],a[0])
cnt+=1
elif c[1]==mn and c[1]!=a[0]:
if c[0]==mx:
c[1]=min(mx-1+c[2],a[0])
else:
c[1]=min(mx-1+c[0],a[0])
cnt+=1
elif c[2]!=a[0]:
if c[0]==mx:
c[2]=min(mx-1+c[1],a[0])
else:
c[2]=min(mx-1+c[0],a[0])
cnt+=1
print cnt
| PYTHON |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
int abs(int a) {
if (a > 0)
return a;
else
return -a;
}
int main() {
int x, y;
scanf("%d%d", &x, &y);
if (x < y) {
int tmp = x;
x = y;
y = tmp;
}
int triangle[3];
int which[3];
int i = 0;
int count = 0;
triangle[0] = y;
triangle[1] = y;
triangle[2] = y;
while (1) {
if (triangle[0] == x && triangle[1] == x && triangle[2] == x) {
break;
}
int smallest = 0;
if (triangle[1] < triangle[smallest]) {
smallest = 1;
}
if (triangle[2] < triangle[smallest]) {
smallest = 2;
}
if (smallest == 0) {
which[0] = 0;
which[1] = 1;
which[2] = 2;
} else if (smallest == 1) {
which[0] = 1;
which[1] = 0;
which[2] = 2;
} else {
which[0] = 2;
which[1] = 1;
which[2] = 0;
}
int max = triangle[which[1]] + triangle[which[2]];
int min = abs(triangle[which[1]] - triangle[which[2]]);
if (x > min && x < max) {
triangle[which[0]] = x;
} else {
triangle[which[0]] = max - 1;
}
count++;
}
printf("%d\n", count);
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #!/usr/bin/env python
#-*-coding:utf-8 -*-
x,a=map(int,input().split())
b=c=a
s=0
while x>a:
a,b,c=b,c,b-1+c
s+=1
print(s)
| PYTHON3 |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | from sys import stdin
if __name__ == '__main__':
x, y = map(int, stdin.readline().rstrip().split())
a = b = c = y
count = 0
while a < x:
temp = b + c -1
a = b
b = c
c = temp
count += 1
print(count) | PYTHON3 |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
int i, j, x, y;
int ans = 0;
void countSteps(int a, int b, int c) {
if (c == x)
return;
else {
ans++;
c = x < a + b - 1 ? x : a + b - 1;
countSteps(c, a, b);
}
}
using namespace std;
int main() {
cin >> x >> y;
countSteps(y, y, y);
cout << ans << endl;
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.io.PrintWriter;
import java.util.*;
public class C {
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static void exit() {
sc.close();
out.close();
}
int n, m;
public C () {
n = sc.nextInt();
m = sc.nextInt();
out.println(solve(m, m, m));
}
int solve(int a, int b, int c) {
int steps = 0;
while (a < n || b < n || c < n) {
// sort a <= b <= c
if (a > b) {
int t = a;
a = b;
b = t;
}
if (a > c) {
int t = a;
a = c;
c = t;
}
if (b > c) {
int t = b;
b = c;
c = t;
}
a = b + c -1;
if (a > n) a = n;
steps ++;
}
return steps;
}
public static void main(String[] args) {
new C();
exit();
}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.util.*;
public class Solution3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(), y = sc.nextInt();
System.out.println(deEvolution(new int[]{y,y,y},x));
}
public static int deEvolution(int[] arr, int x){
if(arr[0]>=x && arr[1]>=x && arr[2]>=x) return 0;
int t = arr[0] = arr[1] + arr[2] -1;
arr[0] = arr[1]; arr[1] = arr[2]; arr[2] = t;
return 1+deEvolution(arr,x);
}
}
| JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ CONTROLLER CLASS ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
public class Main {
public static void main(String[] args) throws Exception {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
int[] dx = {1 , 0, -1, 0 , -1 ,-1 , 1 , 1},
dy = {0 , 1 ,0 ,-1 ,-1 ,1 - 1, 1} ;
//int[] union;
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OTHER USEFULL METHODS ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
// GCD && LCM
long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
long lcm(long a, long b) {
return a * (b / gcd(a, b));
}
// REverse a String
String rev(String s) {
return new StringBuilder(s).reverse().toString();
}
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SOLUTION IS RIGHT HERE^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
// BufferedReader br = new BufferedReader(new
// InputStreamReader(System.in));
// Scanner sc = new Scanner(System.in);
//final long mod = (long)1e9+7;
int x = in.nextInt() , y = in.nextInt();
int[] a = {y , y, y};
int cnt = 0;
while(a[0] != x || a[1] != x || a[2] != x){
a[cnt%3 ] = min(abs(a[(cnt+1)%3] + a[(cnt+2)%3])-1 , x);
//out.println(a[0]+" "+a[1]+" "+a[2]);
cnt++;
}
out.println(cnt);
}
}
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OTHER USEFULL CLASSES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INPUT READER CLASS ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/*``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
static class InputReader {
private byte[] buf = new byte[8000];
private int index;
private int total;
private InputStream in;
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
in = stream;
}
public int scan() {
if (total == -1)
throw new InputMismatchException();
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (total <= 0)
return -1;
}
return buf[index++];
}
public long scanlong() {
long integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
return neg * integer;
}
private int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public double scandouble() {
double doubll = 0;
int n = scan();
int neg = 1;
while (isWhiteSpace(n))
n = scan();
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doubll *= 10;
doubll += n - '0';
n = scan();
}
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doubll += (n - '0') * temp;
n = scan();
}
}
}
return neg * doubll;
}
private float scanfloat() {
float doubll = 0;
int n = scan();
int neg = 1;
while (isWhiteSpace(n))
n = scan();
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doubll *= 10;
doubll += n - '0';
n = scan();
}
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doubll += (n - '0') * temp;
n = scan();
}
}
}
return neg * doubll;
}
public String scanstring() {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n)) {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
public String scan_nextLine() {
int n = scan();
while (isWhiteSpace(n))
n = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(n);
n = scan();
} while (!isEndOfLine(n));
return res.toString();
}
public boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
/// Input module
public int nextInt() {
return scanInt();
}
public long nextLong() {
return scanlong();
}
public double nextDouble() {
return scandouble();
}
public float nextFloat() {
return scanfloat();
}
public String next() {
return scanstring();
}
public String nextLine() throws IOException {
return scan_nextLine();
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, y, cnt = 0;
cin >> x >> y;
vector<int> tri;
for (int i = 0; i < 3; i++) tri.push_back(y);
while (1) {
int f = 1;
for (int i = 0; i < 3; i++)
if (tri[i] != x) f = 0;
if (f) {
cout << cnt << endl;
return 0;
}
sort(tri.begin(), tri.end());
tri[0] = tri[1] + tri[2] - 1;
tri[0] = min(x, tri[0]);
cnt++;
}
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
int arr[3];
for (int i = 0; i < 3; ++i) {
arr[i] = y;
}
int cnt = 0;
int index = 0;
int max = 0;
bool out = false;
while (arr[index] != x) {
int indexSbl1 = (index - 1);
int indexSbl2 = (index - 2);
if (indexSbl1 < 0) indexSbl1 += 3;
if (indexSbl2 < 0) indexSbl2 += 3;
arr[index] = arr[indexSbl1] + arr[indexSbl2] - 1;
if (arr[index] > x) arr[index] = x;
index = (index + 1) % 3;
++cnt;
}
cout << cnt << endl;
return 0;
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | //package cf;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static int[] s = new int[3];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
s[0]=s[1]=s[2]=y;
int cnt = 0;
while(true){
cnt++;
if(s[1]+s[2]<=x){
s[0]=s[1]+s[2]-1;
}else{
s[0]=x;
}
Arrays.sort(s);
if(s[0]==x&&s[1]==x&&s[2]==x){
break;
}
}
System.out.println(cnt);
}
}
| JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import java.util.Scanner;
import java.util.Arrays;
public class Triangles {
public static void main(String[] args) {
int x,y,i,j;
Scanner scan = new Scanner(System.in);
x = scan.nextInt();
y = scan.nextInt();
int res = compute(x,y);
System.out.println(res);
}
static int compute(int x,int y){
int i,count=0;
int arr[] = new int[3];
arr[0] = arr[1] = arr[2] = y;
while(arr[0]!=x && arr[1]!=x && arr[2]!=x){
if(arr[1]+arr[2]>x){
count = count+3;
return count;
}
arr[0] = arr[1]+arr[2]-1;
Arrays.sort(arr);
count++;
}
return count;
}
}
| JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> triangle;
int x, y, start, temp, a, b, c;
cin >> x >> y;
if (x - y < y) {
cout << "3" << endl;
} else {
temp = 1;
triangle.push_back(y + y - 1);
triangle.push_back(y);
triangle.push_back(y);
sort(triangle.begin(), triangle.end());
while (triangle[0] != triangle[1] || triangle[0] != triangle[2]) {
triangle[0] = triangle[1] + triangle[2] - 1;
if (triangle[0] > x) triangle[0] = x;
sort(triangle.begin(), triangle.end());
temp++;
}
cout << temp << endl;
}
}
| CPP |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | import javafx.scene.control.ComboBox;
import java.util.Scanner;
/**
* blablabla
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt(), y = in.nextInt();
Triangle triangle = new Triangle(y, y, y);
System.out.println(triangle.count( x));
}
}
class Triangle{
int a,b,c;
public Triangle(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int count(int x){
int k = 0;
while (true){
a = Math.min(x, b + c - 1);
int buf = a;
a = b;
b = c;
c = buf;
k++;
if(a == x && b == x && c == x) break;
}
return k;
}
} | JAVA |
712_C. Memory and De-Evolution | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 β€ y < x β€ 100 000) β the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do <image>.
In the second sample test, Memory can do <image>.
In the third sample test, Memory can do: <image>
<image>. | 2 | 9 | x,y=map(int,raw_input().split())
c=0
a=[y]*3
while a[-1]<x:
a=a[1:]+[sum(a[1:])-1]
c+=1
print c+2 | PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class CodeForces {
private final BufferedReader reader;
private final PrintWriter writer;
private StringTokenizer tokenizer;
private void solve() {
int n = nextInt();
int[] l = new int[n];
int[] r = new int[n];
int lSum = 0;
int rSum = 0;
for (int i = 0; i < n; i++) {
lSum += l[i] = nextInt();
rSum += r[i] = nextInt();
}
int res = -1;
int maxD = Math.abs(lSum - rSum);
for (int i = 0; i < n; i++) {
if (Math.abs(lSum - l[i] + r[i] - (rSum - r[i] + l[i])) > maxD) {
maxD = Math.abs(lSum - l[i] + r[i] - (rSum - r[i] + l[i]));
res = i;
}
}
writer.print(res == -1 ? 0 : res + 1);
}
public static void main(String[] args) {
new CodeForces(System.in, System.out);
}
private CodeForces(InputStream inputStream, OutputStream outputStream) {
this.reader = new BufferedReader(new InputStreamReader(inputStream));
this.writer = new PrintWriter(outputStream);
solve();
writer.close();
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(readLine());
}
return tokenizer.nextToken();
}
private String readLine() {
String line;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
private int nextInt() {
return Integer.parseInt(next());
}
private long nextLong() {
return Long.parseLong(next());
}
private double nextDouble() {
return Double.parseDouble(next());
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = readLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public static int upperBinarySearch(int[] array, int start, int finish, int key) {
int low = 0;
int high = array.length - 1;
if (high < 0) return -1;
while (low < high) {
int mid = (low + high + 1) >>> 1;
if (array[mid] <= key) low = mid;
else high = mid - 1;
}
if (array[low] != key) {
if (array[low] < key) low++;
low = -(low + 1);
}
return low;
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #B.py
n = int(raw_input())
#nums = []
leftM = (0, 0)
rightM = (0, 0)
leftC, rightC = 0, 0
equalC = 0
totalC = 0
for i in range(n):
l, r = [int(k_) for k_ in raw_input().split()]
v = l - r
if v > 0 :
leftC += 1
leftM = max((v, i+1), leftM)
elif v < 0:
rightC += 1
rightM = min((v, i+1), rightM)
else:
equalC += 1
totalC += v
if leftC == 0 and rightC == 0:
print 0
elif totalC - 2*rightM[0] > -(totalC - 2* leftM[0]):
print rightM[1]
else:
print leftM[1]
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
L, R = 0, 0
D = []
for i in range(n):
l, r = map(int, input().split())
L += l
R += r
D.append((l, r))
ans = abs(L - R)
num = 0
for i in range(n):
l, r = D[i]
L1 = L - l + r
R1 = R - r + l
if ans < abs(L1 - R1):
ans = abs(L1 - R1)
num = i + 1
print(num) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | def abs(x):
if x < 0:
return -x
else:
return x
n = int(raw_input())
l = []
r = []
sl = 0
sr = 0
for i in range(n):
a, b = map(int, raw_input().split())
l += [a]
r += [b]
sl += a
sr += b
maxs = abs(sl - sr)
maxd = -1
for i in range(n):
if abs(sl - l[i] + r[i] - (sr - r[i] + l[i])) > maxs:
maxs = abs(sl - l[i] + r[i] - (sr - r[i] + l[i]))
maxd = i
print maxd + 1 | PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | x=int(raw_input())
m=[]
for i in range(x):
a,b=raw_input().split(' ')
m.append([int(a),int(b)])
ld=0
rd=0
left=0
right=0
rr=0
lr=0
for i in range(x):
x=m[i][0]
y=m[i][1]
if x>=y and x-y>=ld:
ld=x-y
lr=i+1
if x<=y and y-x>=rd:
rd=y-x
rr=i+1
left+=x
right+=y
if left+rd>right+ld:
print(rr)
else:
print(lr)
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
ans = 0
left = 0
right = 0
ml = 0
mr = 0
a = []
for i in range(n):
x,y = map(int,input().split())
a.append((x,y))
left+=x
right+=y
best = abs(left - right)
c = 0
c_ans = 0
for v in a:
c+=1
nl = left
nr = right
nl = nl - v[0] + v[1]
nr = nr - v[1] + v[0]
if abs(nl-nr) > best:
c_ans = c
best = abs(nl-nr)
print(c_ans)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=int(input())
le=0
ri=0
a=[0]*n
b=[0]*n
no=-21
for i in range(n):
l,r=map(int,input().split())
le=le+l
ri=ri+r
a[i]=l
b[i]=r
v=abs(le-ri)
for i in range(n):
l1=le-a[i]+b[i]
r1=ri-b[i]+a[i]
x=abs(l1-r1)
if x>v:
no=i
v=x
if no==-21:
print(0)
else:
print(no+1) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long leftsum = 0, rightsum = 0;
long n, index = -1;
cin >> n;
int left[n], right[n];
for (long i = 0; i < n; ++i) {
cin >> left[i];
cin >> right[i];
leftsum += left[i];
rightsum += right[i];
}
int max = abs(leftsum - rightsum);
int ans;
for (int i = 0; i < n; ++i) {
leftsum -= left[i];
leftsum += right[i];
rightsum -= right[i];
rightsum += left[i];
if (abs(leftsum - rightsum) > max) {
index = i;
max = abs(leftsum - rightsum);
}
leftsum -= right[i];
leftsum += left[i];
rightsum -= left[i];
rightsum += right[i];
}
if (index != -1)
cout << index + 1 << endl;
else
cout << "0" << endl;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | //package baobab;
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) {
Solver solver = new Solver();
}
static class Solver {
IO io;
public Solver() {
this.io = new IO();
try {
solve();
} finally {
io.close();
}
}
/****************************** START READING HERE ********************************/
void solve() {
int n = io.nextInt();
long totalLeft = 0;
long totalRight = 0;
int minDiff = Integer.MAX_VALUE;
int maxDiff = Integer.MIN_VALUE;
int indexForMinD = 0;
int indexForMaxD = 0;
for (int i=1; i<=n; i++) {
int left = io.nextInt();
int right = io.nextInt();
totalLeft += left;
totalRight += right;
int diff = left - right;
if (diff < minDiff) {
minDiff = diff;
indexForMinD = i;
}
if (diff > maxDiff) {
maxDiff = diff;
indexForMaxD = i;
}
}
long totalDiff = totalLeft - totalRight;
long beauty = Math.abs(totalDiff);
long ans = 0;
long altBeauty = Math.abs(totalDiff - 2 * minDiff);
if (altBeauty > beauty) {
beauty = altBeauty;
ans = indexForMinD;
}
altBeauty = Math.abs(totalDiff - 2 * maxDiff);
if (altBeauty > beauty) {
beauty = altBeauty;
ans = indexForMaxD;
}
io.println(ans);
}
/************************** UTILITY CODE BELOW THIS LINE **************************/
long MOD = (long)1e9 + 7;
List<Integer>[] toGraph(IO io, int n) {
List<Integer>[] g = new ArrayList[n+1];
for (int i=1; i<=n; i++) g[i] = new ArrayList<>();
for (int i=1; i<=n-1; i++) {
int a = io.nextInt();
int b = io.nextInt();
g[a].add(b);
g[b].add(a);
}
return g;
}
class Point {
int y;
int x;
public Point(int y, int x) {
this.y = y;
this.x = x;
}
}
class IDval implements Comparable<IDval> {
int id;
long val;
public IDval(int id, long val) {
this.val = val;
this.id = id;
}
@Override
public int compareTo(IDval o) {
if (this.val < o.val) return -1;
if (this.val > o.val) return 1;
return this.id - o.id;
}
}
long pow(long base, int exp) {
if (exp == 0) return 1L;
long x = pow(base, exp/2);
long ans = x * x;
if (exp % 2 != 0) ans *= base;
return ans;
}
class ElementCounter {
private HashMap<Long, Long> elements;
public ElementCounter() {
elements = new HashMap<>();
}
public void add(long element) {
long count = 1;
if (elements.containsKey(element)) count += elements.get(element);
elements.put(element, count);
}
public void remove(long element) {
long count = elements.get(element);
count--;
if (count == 0) elements.remove(element);
else elements.put(element, count);
}
public long get(long element) {
if (!elements.containsKey(element)) return 0;
return elements.get(element);
}
public int size() {
return elements.size();
}
}
class StringCounter {
HashMap<String, Long> elements;
public StringCounter() {
elements = new HashMap<>();
}
public void add(String identifier) {
long count = 1;
if (elements.containsKey(identifier)) count += elements.get(identifier);
elements.put(identifier, count);
}
public void remove(String identifier) {
long count = elements.get(identifier);
count--;
if (count == 0) elements.remove(identifier);
else elements.put(identifier, count);
}
public long get(String identifier) {
if (!elements.containsKey(identifier)) return 0;
return elements.get(identifier);
}
public int size() {
return elements.size();
}
}
class LCAFinder {
/* O(n log n) Initialize: new LCAFinder(graph)
* O(log n) Queries: find(a,b) returns lowest common ancestor for nodes a and b */
int[] nodes;
int[] depths;
int[] entries;
int pointer;
FenwickMin fenwick;
public LCAFinder(List<Integer>[] graph) {
this.nodes = new int[(int)10e6];
this.depths = new int[(int)10e6];
this.entries = new int[graph.length];
this.pointer = 1;
boolean[] visited = new boolean[graph.length+1];
dfs(1, 0, graph, visited);
fenwick = new FenwickMin(pointer-1);
for (int i=1; i<pointer; i++) {
fenwick.set(i, depths[i] * 1000000L + i);
}
}
private void dfs(int node, int depth, List<Integer>[] graph, boolean[] visited) {
visited[node] = true;
entries[node] = pointer;
nodes[pointer] = node;
depths[pointer] = depth;
pointer++;
for (int neighbor : graph[node]) {
if (visited[neighbor]) continue;
dfs(neighbor, depth+1, graph, visited);
nodes[pointer] = node;
depths[pointer] = depth;
pointer++;
}
}
public int find(int a, int b) {
int left = entries[a];
int right = entries[b];
if (left > right) {
int temp = left;
left = right;
right = temp;
}
long mixedBag = fenwick.getMin(left, right);
int index = (int) (mixedBag % 1000000L);
return nodes[index];
}
}
class FenwickMin {
long n;
long[] original;
long[] bottomUp;
long[] topDown;
public FenwickMin(int n) {
this.n = n;
original = new long[n+2];
bottomUp = new long[n+2];
topDown = new long[n+2];
}
public void set(int modifiedNode, long value) {
long replaced = original[modifiedNode];
original[modifiedNode] = value;
// Update left tree
int i = modifiedNode;
long v = value;
while (i <= n) {
if (v > bottomUp[i]) {
if (replaced == bottomUp[i]) {
v = Math.min(v, original[i]);
for (int r=1 ;; r++) {
int x = (i&-i)>>>r;
if (x == 0) break;
int child = i-x;
v = Math.min(v, bottomUp[child]);
}
} else break;
}
if (v == bottomUp[i]) break;
bottomUp[i] = v;
i += (i&-i);
}
// Update right tree
i = modifiedNode;
v = value;
while (i > 0) {
if (v > topDown[i]) {
if (replaced == topDown[i]) {
v = Math.min(v, original[i]);
for (int r=1 ;; r++) {
int x = (i&-i)>>>r;
if (x == 0) break;
int child = i+x;
if (child > n+1) break;
v = Math.min(v, topDown[child]);
}
} else break;
}
if (v == topDown[i]) break;
topDown[i] = v;
i -= (i&-i);
}
}
public long getMin(int a, int b) {
long min = original[a];
int prev = a;
int curr = prev + (prev&-prev); // parent right hand side
while (curr <= b) {
min = Math.min(min, topDown[prev]); // value from the other tree
prev = curr;
curr = prev + (prev&-prev);;
}
min = Math.min(min, original[prev]);
prev = b;
curr = prev - (prev&-prev); // parent left hand side
while (curr >= a) {
min = Math.min(min,bottomUp[prev]); // value from the other tree
prev = curr;
curr = prev - (prev&-prev);
}
return min;
}
}
class FenwickSum {
public long[] d;
public FenwickSum(int n) {
d=new long[n+1];
}
/** a[0] must be unused. */
public FenwickSum(long[] a) {
d=new long[a.length];
for (int i=1; i<a.length; i++) {
modify(i, a[i]);
}
}
/** Do not modify i=0. */
void modify(int i, long v) {
while (i<d.length) {
d[i] += v;
// Move to next uplink on the RIGHT side of i
i += (i&-i);
}
}
/** Returns sum from a to b, *BOTH* inclusive. */
long getSum(int a, int b) {
return getSum(b) - getSum(a-1);
}
private long getSum(int i) {
long sum = 0;
while (i>0) {
sum += d[i];
// Move to next uplink on the LEFT side of i
i -= (i&-i);
}
return sum;
}
}
class SegmentTree {
/** Query sums with log(n) modifyRange */
int N;
long[] p;
public SegmentTree(int n) {
/* TODO: Test that this works. */
N = n;
p = new long[2*N];
}
public void modifyRange(int a, int b, long change) {
muuta(a, change);
muuta(b+1, -change);
}
void muuta(int k, long muutos) {
k += N;
p[k] += muutos;
for (k /= 2; k >= 1; k /= 2) {
p[k] = p[2*k] + p[2*k+1];
}
}
public long get(int k) {
int a = N;
int b = k+N;
long s = 0;
while (a <= b) {
if (a%2 == 1) s += p[a++];
if (b%2 == 0) s += p[b--];
a /= 2;
b /= 2;
}
return s;
}
}
class Zalgo {
public int pisinEsiintyma(String haku, String kohde) {
char[] s = new char[haku.length() + 1 + kohde.length()];
for (int i=0; i<haku.length(); i++) {
s[i] = haku.charAt(i);
}
int j = haku.length();
s[j++] = '#';
for (int i=0; i<kohde.length(); i++) {
s[j++] = kohde.charAt(i);
}
int[] z = toZarray(s);
int max = 0;
for (int i=haku.length(); i<z.length; i++) {
max = Math.max(max, z[i]);
}
return max;
}
public int[] toZarray(char[] s) {
int n = s.length;
int[] z = new int[n];
int a = 0, b = 0;
for (int i = 1; i < n; i++) {
if (i > b) {
for (int j = i; j < n && s[j - i] == s[j]; j++) z[i]++;
}
else {
z[i] = z[i - a];
if (i + z[i - a] > b) {
for (int j = b + 1; j < n && s[j - i] == s[j]; j++) z[i]++;
a = i;
b = i + z[i] - 1;
}
}
}
return z;
}
public List<Integer> getStartIndexesWhereWordIsFound(String haku, String kohde) {
// this is alternative use case
char[] s = new char[haku.length() + 1 + kohde.length()];
for (int i=0; i<haku.length(); i++) {
s[i] = haku.charAt(i);
}
int j = haku.length();
s[j++] = '#';
for (int i=0; i<kohde.length(); i++) {
s[j++] = kohde.charAt(i);
}
int[] z = toZarray(s);
List<Integer> indexes = new ArrayList<>();
for (int i=haku.length(); i<z.length; i++) {
if (z[i] < haku.length()) continue;
indexes.add(i);
}
return indexes;
}
}
class StringHasher {
class HashedString {
long[] hashes;
long[] modifiers;
public HashedString(long[] hashes, long[] modifiers) {
this.hashes = hashes;
this.modifiers = modifiers;
}
}
long P;
long M;
public StringHasher() {
initializePandM();
}
HashedString hashString(String s) {
int n = s.length();
long[] hashes = new long[n];
long[] modifiers = new long[n];
hashes[0] = s.charAt(0);
modifiers[0] = 1;
for (int i=1; i<n; i++) {
hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M;
modifiers[i] = (modifiers[i-1] * P) % M;
}
return new HashedString(hashes, modifiers);
}
/**
* Indices are inclusive.
*/
long getHash(HashedString hashedString, int startIndex, int endIndex) {
long[] hashes = hashedString.hashes;
long[] modifiers = hashedString.modifiers;
long result = hashes[endIndex];
if (startIndex > 0) result -= (hashes[startIndex-1] * modifiers[endIndex-startIndex+1]) % M;
if (result < 0) result += M;
return result;
}
// Less interesting methods below
/**
* Efficient for 2 input parameter strings in particular.
*/
HashedString[] hashString(String first, String second) {
HashedString[] array = new HashedString[2];
int n = first.length();
long[] modifiers = new long[n];
modifiers[0] = 1;
long[] firstHashes = new long[n];
firstHashes[0] = first.charAt(0);
array[0] = new HashedString(firstHashes, modifiers);
long[] secondHashes = new long[n];
secondHashes[0] = second.charAt(0);
array[1] = new HashedString(secondHashes, modifiers);
for (int i=1; i<n; i++) {
modifiers[i] = (modifiers[i-1] * P) % M;
firstHashes[i] = (firstHashes[i-1] * P + first.charAt(i)) % M;
secondHashes[i] = (secondHashes[i-1] * P + second.charAt(i)) % M;
}
return array;
}
/**
* Efficient for 3+ strings
* More efficient than multiple hashString calls IF strings are same length.
*/
HashedString[] hashString(String... strings) {
HashedString[] array = new HashedString[strings.length];
int n = strings[0].length();
long[] modifiers = new long[n];
modifiers[0] = 1;
for (int j=0; j<strings.length; j++) {
// if all strings are not same length, defer work to another method
if (strings[j].length() != n) {
for (int i=0; i<n; i++) {
array[i] = hashString(strings[i]);
}
return array;
}
// otherwise initialize stuff
long[] hashes = new long[n];
hashes[0] = strings[j].charAt(0);
array[j] = new HashedString(hashes, modifiers);
}
for (int i=1; i<n; i++) {
modifiers[i] = (modifiers[i-1] * P) % M;
for (int j=0; j<strings.length; j++) {
String s = strings[j];
long[] hashes = array[j].hashes;
hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M;
}
}
return array;
}
void initializePandM() {
ArrayList<Long> modOptions = new ArrayList<>(20);
modOptions.add(353873237L);
modOptions.add(353875897L);
modOptions.add(353878703L);
modOptions.add(353882671L);
modOptions.add(353885303L);
modOptions.add(353888377L);
modOptions.add(353893457L);
P = modOptions.get(new Random().nextInt(modOptions.size()));
modOptions.clear();
modOptions.add(452940277L);
modOptions.add(452947687L);
modOptions.add(464478431L);
modOptions.add(468098221L);
modOptions.add(470374601L);
modOptions.add(472879717L);
modOptions.add(472881973L);
M = modOptions.get(new Random().nextInt(modOptions.size()));
}
}
private static class Prob {
/** For heavy calculations on probabilities, this class
* provides more accuracy & efficiency than doubles.
* Math explained: https://en.wikipedia.org/wiki/Log_probability
* Quick start:
* - Instantiate probabilities, eg. Prob a = new Prob(0.75)
* - add(), multiply() return new objects, can perform on nulls & NaNs.
* - get() returns probability as a readable double */
/** Logarithmized probability. Note: 0% represented by logP NaN. */
private double logP;
/** Construct instance with real probability. */
public Prob(double real) {
if (real > 0) this.logP = Math.log(real);
else this.logP = Double.NaN;
}
/** Construct instance with already logarithmized value. */
static boolean dontLogAgain = true;
public Prob(double logP, boolean anyBooleanHereToChooseThisConstructor) {
this.logP = logP;
}
/** Returns real probability as a double. */
public double get() {
return Math.exp(logP);
}
@Override
public String toString() {
return ""+get();
}
/***************** STATIC METHODS BELOW ********************/
/** Note: returns NaN only when a && b are both NaN/null. */
public static Prob add(Prob a, Prob b) {
if (nullOrNaN(a) && nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain);
if (nullOrNaN(a)) return copy(b);
if (nullOrNaN(b)) return copy(a);
double x = a.logP;
double y = b.logP;
double sum = x + Math.log(1 + Math.exp(y - x));
return new Prob(sum, dontLogAgain);
}
/** Note: multiplying by null or NaN produces NaN (repping 0% real prob). */
public static Prob multiply(Prob a, Prob b) {
if (nullOrNaN(a) || nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain);
return new Prob(a.logP + b.logP, dontLogAgain);
}
/** Returns true if p is null or NaN. */
private static boolean nullOrNaN(Prob p) {
return (p == null || Double.isNaN(p.logP));
}
/** Returns a new instance with the same value as original. */
private static Prob copy(Prob original) {
return new Prob(original.logP, dontLogAgain);
}
}
public class StronglyConnectedComponents {
/** Kosaraju's algorithm */
ArrayList<Integer>[] forw;
ArrayList<Integer>[] bacw;
/** Use: getCount(2, new int[] {1,2}, new int[] {2,1}) */
public int getCount(int n, int[] mista, int[] minne) {
forw = new ArrayList[n+1];
bacw = new ArrayList[n+1];
for (int i=1; i<=n; i++) {
forw[i] = new ArrayList<Integer>();
bacw[i] = new ArrayList<Integer>();
}
for (int i=0; i<mista.length; i++) {
int a = mista[i];
int b = minne[i];
forw[a].add(b);
bacw[b].add(a);
}
int count = 0;
List<Integer> list = new ArrayList<Integer>();
boolean[] visited = new boolean[n+1];
for (int i=1; i<=n; i++) {
dfsForward(i, visited, list);
}
visited = new boolean[n+1];
for (int i=n-1; i>=0; i--) {
int node = list.get(i);
if (visited[node]) continue;
count++;
dfsBackward(node, visited);
}
return count;
}
public void dfsForward(int i, boolean[] visited, List<Integer> list) {
if (visited[i]) return;
visited[i] = true;
for (int neighbor : forw[i]) {
dfsForward(neighbor, visited, list);
}
list.add(i);
}
public void dfsBackward(int i, boolean[] visited) {
if (visited[i]) return;
visited[i] = true;
for (int neighbor : bacw[i]) {
dfsBackward(neighbor, visited);
}
}
}
class DrawGrid {
void draw(boolean[][] d) {
System.out.print(" ");
for (int x=0; x<d[0].length; x++) {
System.out.print(" " + x + " ");
}
System.out.println("");
for (int y=0; y<d.length; y++) {
System.out.print(y + " ");
for (int x=0; x<d[0].length; x++) {
System.out.print((d[y][x] ? "[x]" : "[ ]"));
}
System.out.println("");
}
}
void draw(int[][] d) {
int max = 1;
for (int y=0; y<d.length; y++) {
for (int x=0; x<d[0].length; x++) {
max = Math.max(max, ("" + d[y][x]).length());
}
}
System.out.print(" ");
String format = "%" + (max+2) + "s";
for (int x=0; x<d[0].length; x++) {
System.out.print(String.format(format, x) + " ");
}
format = "%" + (max) + "s";
System.out.println("");
for (int y=0; y<d.length; y++) {
System.out.print(y + " ");
for (int x=0; x<d[0].length; x++) {
System.out.print(" [" + String.format(format, (d[y][x])) + "]");
}
System.out.println("");
}
}
}
class BaseConverter {
/* Palauttaa luvun esityksen kannassa base */
public String convert(Long number, int base) {
return Long.toString(number, base);
}
/* Palauttaa luvun esityksen kannassa baseTo, kun annetaan luku StringinΓ€ kannassa baseFrom */
public String convert(String number, int baseFrom, int baseTo) {
return Long.toString(Long.parseLong(number, baseFrom), baseTo);
}
/* Tulkitsee kannassa base esitetyn luvun longiksi (kannassa 10) */
public long longify(String number, int baseFrom) {
return Long.parseLong(number, baseFrom);
}
}
class Binary implements Comparable<Binary> {
/**
* Use example: Binary b = new Binary(Long.toBinaryString(53249834L));
*
* When manipulating small binary strings, instantiate new Binary(string)
* When just reading large binary strings, instantiate new Binary(string,true)
* get(int i) returns a character '1' or '0', not an int.
*/
private boolean[] d;
private int first; // Starting from left, the first (most remarkable) '1'
public int length;
public Binary(String binaryString) {
this(binaryString, false);
}
public Binary(String binaryString, boolean initWithMinArraySize) {
length = binaryString.length();
int size = Math.max(2*length, 1);
first = length/4;
if (initWithMinArraySize) {
first = 0;
size = Math.max(length, 1);
}
d = new boolean[size];
for (int i=0; i<length; i++) {
if (binaryString.charAt(i) == '1') d[i+first] = true;
}
}
public void addFirst(char c) {
if (first-1 < 0) doubleArraySize();
first--;
d[first] = (c == '1' ? true : false);
length++;
}
public void addLast(char c) {
if (first+length >= d.length) doubleArraySize();
d[first+length] = (c == '1' ? true : false);
length++;
}
private void doubleArraySize() {
boolean[] bigArray = new boolean[(d.length+1) * 2];
int newFirst = bigArray.length / 4;
for (int i=0; i<length; i++) {
bigArray[i + newFirst] = d[i + first];
}
first = newFirst;
d = bigArray;
}
public boolean flip(int i) {
boolean value = (this.d[first+i] ? false : true);
this.d[first+i] = value;
return value;
}
public void set(int i, char c) {
boolean value = (c == '1' ? true : false);
this.d[first+i] = value;
}
public char get(int i) {
return (this.d[first+i] ? '1' : '0');
}
@Override
public int compareTo(Binary o) {
if (this.length != o.length) return this.length - o.length;
int len = this.length;
for (int i=0; i<len; i++) {
int diff = this.get(i) - o.get(i);
if (diff != 0) return diff;
}
return 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i=0; i<length; i++) {
sb.append(d[i+first] ? '1' : '0');
}
return sb.toString();
}
}
class BinomialCoefficients {
/** Total number of K sized unique combinations from pool of size N (unordered)
N! / ( K! (N - K)! ) */
/** For simple queries where output fits in long. */
public long biCo(long n, long k) {
long r = 1;
if (k > n) return 0;
for (long d = 1; d <= k; d++) {
r *= n--;
r /= d;
}
return r;
}
/** For multiple queries with same n, different k. */
public long[] precalcBinomialCoefficientsK(int n, int maxK) {
long v[] = new long[maxK+1];
v[0] = 1; // nC0 == 1
for (int i=1; i<=n; i++) {
for (int j=Math.min(i,maxK); j>0; j--) {
v[j] = v[j] + v[j-1]; // Pascal's triangle
}
}
return v;
}
/** When output needs % MOD. */
public long[] precalcBinomialCoefficientsK(int n, int k, long M) {
long v[] = new long[k+1];
v[0] = 1; // nC0 == 1
for (int i=1; i<=n; i++) {
for (int j=Math.min(i,k); j>0; j--) {
v[j] = v[j] + v[j-1]; // Pascal's triangle
v[j] %= M;
}
}
return v;
}
}
class GreatestCommonDivisor {
/** Chained calls to Euclidean algorithm. */
long gcd(long... v) {
if (v.length == 1) return v[0];
long ans = gcd(v[1], v[0]);
for (int i=2; i<v.length; i++) {
ans = gcd(ans, v[i]);
}
return ans;
}
/** Euclidean algorithm. */
long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a%b);
}
}
private class IO extends PrintWriter {
private InputStreamReader r;
private static final int BUFSIZE = 1 << 15;
private char[] buf;
private int bufc;
private int bufi;
private StringBuilder sb;
public IO() {
super(new BufferedOutputStream(System.out));
r = new InputStreamReader(System.in);
buf = new char[BUFSIZE];
bufc = 0;
bufi = 0;
sb = new StringBuilder();
}
private void fillBuf() throws IOException {
bufi = 0;
bufc = 0;
while(bufc == 0) {
bufc = r.read(buf, 0, BUFSIZE);
if(bufc == -1) {
bufc = 0;
return;
}
}
}
private boolean pumpBuf() throws IOException {
if(bufi == bufc) {
fillBuf();
}
return bufc != 0;
}
private boolean isDelimiter(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
}
private void eatDelimiters() throws IOException {
while(true) {
if(bufi == bufc) {
fillBuf();
if(bufc == 0) throw new RuntimeException("IO: Out of input.");
}
if(!isDelimiter(buf[bufi])) break;
++bufi;
}
}
public String next() {
try {
sb.setLength(0);
eatDelimiters();
int start = bufi;
while(true) {
if(bufi == bufc) {
sb.append(buf, start, bufi - start);
fillBuf();
start = 0;
if(bufc == 0) break;
}
if(isDelimiter(buf[bufi])) break;
++bufi;
}
sb.append(buf, start, bufi - start);
return sb.toString();
} catch(IOException e) {
throw new RuntimeException("IO.next: Caught IOException.");
}
}
public int nextInt() {
try {
int ret = 0;
eatDelimiters();
boolean positive = true;
if(buf[bufi] == '-') {
++bufi;
if(!pumpBuf()) throw new RuntimeException("IO.nextInt: Invalid int.");
positive = false;
}
boolean first = true;
while(true) {
if(!pumpBuf()) break;
if(isDelimiter(buf[bufi])) {
if(first) throw new RuntimeException("IO.nextInt: Invalid int.");
break;
}
first = false;
if(buf[bufi] >= '0' && buf[bufi] <= '9') {
if(ret < -214748364) throw new RuntimeException("IO.nextInt: Invalid int.");
ret *= 10;
ret -= (int)(buf[bufi] - '0');
if(ret > 0) throw new RuntimeException("IO.nextInt: Invalid int.");
} else {
throw new RuntimeException("IO.nextInt: Invalid int.");
}
++bufi;
}
if(positive) {
if(ret == -2147483648) throw new RuntimeException("IO.nextInt: Invalid int.");
ret = -ret;
}
return ret;
} catch(IOException e) {
throw new RuntimeException("IO.nextInt: Caught IOException.");
}
}
public long nextLong() {
try {
long ret = 0;
eatDelimiters();
boolean positive = true;
if(buf[bufi] == '-') {
++bufi;
if(!pumpBuf()) throw new RuntimeException("IO.nextLong: Invalid long.");
positive = false;
}
boolean first = true;
while(true) {
if(!pumpBuf()) break;
if(isDelimiter(buf[bufi])) {
if(first) throw new RuntimeException("IO.nextLong: Invalid long.");
break;
}
first = false;
if(buf[bufi] >= '0' && buf[bufi] <= '9') {
if(ret < -922337203685477580L) throw new RuntimeException("IO.nextLong: Invalid long.");
ret *= 10;
ret -= (long)(buf[bufi] - '0');
if(ret > 0) throw new RuntimeException("IO.nextLong: Invalid long.");
} else {
throw new RuntimeException("IO.nextLong: Invalid long.");
}
++bufi;
}
if(positive) {
if(ret == -9223372036854775808L) throw new RuntimeException("IO.nextLong: Invalid long.");
ret = -ret;
}
return ret;
} catch(IOException e) {
throw new RuntimeException("IO.nextLong: Caught IOException.");
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.io.*;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;
public class Palindrome {
static double eps=(double)1e-6;
static long mod=(int)1e9+7;
static long fn=0;
public static void main(String args[]){
InputReader in = new InputReader(System.in);
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
Scanner sc=new Scanner(System.in);
//----------My Code----------
int n=in.nextInt();
ArrayList<pair> arr=new ArrayList();
long sum=0;
for(int i=0;i<n;i++){
arr.add(new pair(in.nextInt(),in.nextInt()));
sum+=arr.get(i).x-arr.get(i).y;
}
int ans=0;
long comp=sum;
for(int i=0;i<n;i++){
int temp=-2*arr.get(i).x+2*arr.get(i).y;
if(Math.abs(sum+temp)>Math.abs(comp)){
ans=i+1;
comp=sum+temp;
}
}
System.out.println(ans);
//---------------The End------------------
out.close();
}
static class TrieNode{
boolean isLeaf;
int b;
TrieNode[] a=new TrieNode[256];
}
static void insert(String key,TrieNode root){
TrieNode temp=root;
for(int i=0;i<key.length();i++){
// System.out.println(i);
if(temp.a[key.charAt(i)]==null){
temp.a[key.charAt(i)]=new TrieNode();
}
temp.a[key.charAt(i)].b+=1;
temp=temp.a[key.charAt(i)];
}
temp.isLeaf=true;
}
static TrieNode search(String key,TrieNode root){
TrieNode temp=root;
for(int i=0;i<key.length();i++){
if(temp.a[key.charAt(i)]==null)
return null;
temp=temp.a[key.charAt(i)];
}
return (temp);
}
static String max(String key,TrieNode root){
TrieNode temp=root;
String res="";
for(int i=0;i<key.length();i++){
if(temp.a[(key.charAt(i)-'0')^1]!=null){
// System.out.println("Hetr");
temp=temp.a[(key.charAt(i)-'0')^1];
res+="1";
}
else if(temp.a[(key.charAt(i)-'0')]!=null){
//System.out.println("Hetr");
temp=temp.a[(key.charAt(i)-'0')];
res+="0";
}
}
// System.out.println(res+"jj");
return res;
}
static void remove(String key,TrieNode root){
TrieNode temp=root;
for(int i=0;i<key.length();i++){
if(temp==null)
return;
if(temp.a[(key.charAt(i)-'0')]!=null&&temp.a[key.charAt(i)-'0'].b>1){
temp.a[(key.charAt(i)-'0')].b--;
temp=temp.a[(key.charAt(i)-'0')];
}
else
{
temp.a[key.charAt(i)-'0']=null;
return;
}
}
if(temp!=null)
temp.isLeaf=false;
}
public static void printSorted(TrieNode node, String s) {
for (int ch = 0; ch < node.a.length; ch++) {
TrieNode child = node.a[ch];
if (child != null){
printSorted(child, s + (char)(ch));
}
}
if (node.isLeaf) {
System.out.println(s);
}
}
private static void permutation(String prefix, String str,HashSet<String> hs) {
int n = str.length();
if (n == 0) hs.add(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n),hs);
}
}
static int recursiveBinarySearch(int[] sortedArray, int start, int end, int key) {
if (start <= end) {
int mid = start + (end - start) / 2;
if (key < sortedArray[mid]) {
return recursiveBinarySearch(sortedArray, start, mid-1, key);
} else if (key > sortedArray[mid]) {
return recursiveBinarySearch(sortedArray, mid+1, end , key);
} else {
return mid;
}
}
return -(start + 1);
}
static long modulo(long a,long b,long c) {
long x=1;
long y=a;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}
static long gcd(long x, long y)
{
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
static class pair implements Comparable<pair> {
int x;
int y;
//int i;
pair(int xx,int yy){
x=xx;
y=yy;
//i=ii;
}
@Override
public int compareTo(pair arg0) {
return Long.compare(this.y, arg0.y);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream inputstream) {
reader = new BufferedReader(new InputStreamReader(inputstream));
tokenizer = null;
}
public String nextLine(){
String fullLine=null;
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
fullLine=reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return fullLine;
}
return fullLine;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int tl = 0, tr = 0;
int l[100100], r[100100];
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
tl += l[i];
tr += r[i];
}
int ans = abs(tl - tr);
int idx = 0;
for (int i = 1; i <= n; i++) {
tl -= l[i];
tr -= r[i];
tl += r[i];
tr += l[i];
if (ans < abs(tl - tr)) {
ans = abs(tl - tr);
idx = i;
}
tl -= r[i];
tr -= l[i];
tl += l[i];
tr += r[i];
}
cout << idx << endl;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=int(input())
L=[0]*n
R=[0]*n
for i in range(n):
L[i],R[i]=map(int,input().split())
ll=sum(L)
rr=sum(R)
a=ll-rr
s=[]
m=abs(a)
p=0
b=False
for i in range(n):
s.append(a+2*R[i]-2*L[i])
if abs(s[i])>abs(m):
m=abs(s[i])
p=i
b=True
if not b:
print(0)
else:
print(p+1)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100001][2];
cin >> n;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < 2; j++) cin >> a[i][j];
}
int lsum = 0, rsum = 0;
for (i = 0; i < n; i++) {
lsum += a[i][0];
rsum += a[i][1];
}
int templ, tempr, beauty, maxb, col = 0;
maxb = abs(lsum - rsum);
for (i = 0; i < n; i++) {
templ = lsum;
tempr = rsum;
templ -= a[i][0];
templ += a[i][1];
tempr -= a[i][1];
tempr += a[i][0];
beauty = abs(templ - tempr);
if (beauty > maxb) {
maxb = beauty;
col = i + 1;
}
}
cout << col << endl;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=input()
a=[map(int,raw_input().split()) for _ in range(n)]
b=zip(*a)
t=sum(b[0])-sum(b[1])
u,v=-1,t
for i in range(n):
x=t+2*(a[i][1]-a[i][0])
if abs(x)>abs(v):
u,v=i,x
print u+1 | PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.util.*;
import java.io.*;
import java.math.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
public class Tester {
//static long sum=0,sum1=Long.MAX_VALUE;
//DecimalFormat df = new DecimalFormat("#.#####");
public static final long MOD = (long) (1e9 + 7);
// Driver program to test above function
public static void main(String args[])
{
InputReader in = new InputReader(System.in);
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
int t=in.nextInt();
int l=0;
int r=0;
int left[]=new int[t];
int right[]=new int[t];
for(int i=0;i<t;i++)
{
int a=in.nextInt();
int b=in.nextInt();
left[i]=a;
right[i]=b;
l+=a;
r+=b;
}
int diff=Math.abs(l-r);
int index=0;
for(int i=0;i<t;i++)
{
int g=l-left[i]+right[i];
int h=r-right[i]+left[i];
if(Math.abs(g-h)>diff)
{
diff=Math.abs(g-h);
index=i+1;
}
}
out.println(index);
out.close();
}
static int ceilSearch(long arr[], int low, int high, long x)
{
int mid;
/* If x is smaller than or equal to the first element,
then return the first element */
if(x <= arr[low])
return low;
/* If x is greater than the last element, then return -1 */
if(x > arr[high])
return -1;
/* get the index of middle element of arr[low..high]*/
mid = (low + high)/2; /* low + (high - low)/2 */
/* If x is same as middle element, then return mid */
if(arr[mid] == x)
return mid;
/* If x is greater than arr[mid], then either arr[mid + 1]
is ceiling of x or ceiling lies in arr[mid+1...high] */
else if(arr[mid] < x)
{
if(mid + 1 <= high && x <= arr[mid+1])
return mid + 1;
else
return ceilSearch(arr, mid+1, high, x);
}
/* If x is smaller than arr[mid], then either arr[mid]
is ceiling of x or ceiling lies in arr[mid-1...high] */
else
{
if(mid - 1 >= low && x > arr[mid-1])
return mid;
else
return ceilSearch(arr, low, mid - 1, x);
}
}
static int floorSearch(long arr[], int low, int high, long x)
{
int mid;
/* If x is smaller than or equal to the first element,
then return the first element */
if(x >= arr[high])
return high;
/* If x is greater than the last element, then return -1 */
if(x < arr[low])
return -1;
/* get the index of middle element of arr[low..high]*/
mid = (low + high)/2; /* low + (high - low)/2 */
/* If x is same as middle element, then return mid */
if(arr[mid] == x)
return mid;
/* If x is greater than arr[mid], then either arr[mid + 1]
is ceiling of x or ceiling lies in arr[mid+1...high] */
else if(arr[mid] < x)
{
if(mid + 1 <= high && x < arr[mid+1])
return mid;
else
return floorSearch(arr, mid+1, high, x);
}
/* If x is smaller than arr[mid], then either arr[mid]
is ceiling of x or ceiling lies in arr[mid-1...high] */
else
{
if(mid - 1 >= low && x >= arr[mid-1])
return mid-1;
else
return floorSearch(arr, low, mid - 1, x);
}
}
static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
result=(result%MOD*x%MOD)%MOD;
n--;
}
return result;
}
static long nCr(int n, int r){
long rfact=1, nfact=1, nrfact=1,temp1 = n-r ,temp2 = r;
if(r>n-r)
{
temp1 =r;
temp2 =n-r;
}
for(int i=1;i<=n;i++)
{
if(i<=temp2)
{
rfact = (rfact%MOD* i%MOD)%MOD;
nrfact = (nrfact%MOD* i%MOD)%MOD;
}
else if(i<=temp1)
{
nrfact = (nrfact%MOD* i%MOD)%MOD;
}
nfact = (nfact%MOD* i%MOD)%MOD;
}
return nfact/(rfact*nrfact);
}
public static long find_ncr(long n, long r) {
long result=1;
// System.out.println(factorial(29));
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
public static long factorial(long n) {
long c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
public static long power(long base, long exp) {
long res=1;
while(exp>0) {
if(exp%2==1) res=(res*base)%MOD;
base=(base*base)%MOD;
exp/=2;
}
return res%MOD;
}
public static void computeLPSArray(String pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
if (pat.charAt(i) == pat.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0)
{
len = lps[len-1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
public static void KMPSearch(String pat, String txt)
{
System.out.println("sdfsdf");
int M = pat.length();
int N = txt.length();
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat,M,lps);
int i = 0; // index for txt[]
while (i < N)
{
if (pat.charAt(j) == txt.charAt(i))
{
j++;
i++;
}
if (j == M)
{
System.out.println("Found pattern "+
"at index " + (i-j));
j = lps[j-1];
}
// mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i))
{
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j-1];
else
i = i+1;
}
}
}
public static long power(long i)
{
long c=1;
while(i>1)
{
c=((c%MOD)*(2%MOD))%MOD;
i--;
}
return c;
}
public static long gcd(long p, long q) {
if (q == 0) {
return p;
}
return gcd(q, p % q);
}
static int mid=-1;
static int find(int arr[],int k,int f,int l)
{
if(f>l)
return mid;
mid=(f+l)/2;
if(arr[mid]==k)
return mid;
if(arr[mid]<k)
{
f=mid;
return find(arr,k,f,l);
}
else
{
l=mid;
return find(arr,k,f,l);
}
}
static void merge(long arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
long L[]=new long[n1];
long R[]=new long[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
static void mergeSort(long arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
private static int[] nextIntArray(InputReader in,int n)
{
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=in.nextInt();
return a;
}
public static double dis(double x1,double x2,double y1)
{
return Math.sqrt(((x1-x2)*(x1-x2))+(y1*y1));
}
private static String[] nextStringArray(InputReader in,int n)
{
String[] a=new String[n];
for(int i=0;i<n;i++)
a[i]=in.next();
return a;
}
private static void show(int[] a)
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
private static void show2DArray(char[][] a)
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]);
System.out.println();
}
}
static long fact(long x)
{
long i=x,mul=1;
while(i>0)
{
mul=(mul%1000000007)*(i%1000000007)%1000000007;
i--;
}
return mul;
}
static class LengthComparator implements Comparator<String> {
public int compare(String arg0, String arg1) {
// Use Integer.compare to compare the two Strings' lengths.
return (arg0+arg1).compareTo(arg1+arg0);
}
}
// static long output(ArrayList<Integer> h[],int j,boolean[] v)
// {
// int k;
// v[j]=true;
// sum++;
// for(k=0;k<h[j].size();k++)
// {
// if(v[h[j].get(k)]==false)
// {
// output(h,h[j].get(k),v);
// }
// }
// return sum;
// }
static long func(boolean v[],int j,ArrayList<Integer> h[],long ban[])
{
v[j]=true;
int k;
long sum=0;
sum=sum+ban[j];
//System.out.println(h[j].size());
for(k=0;k<h[j].size();k++)
{
if(v[h[j].get(k)]==false)
{
v[h[j].get(k)]=true;
sum+=func(v,h[j].get(k),h,ban);
}
}
return sum;
}
/*static class Graph {
private static Deque<Integer> stack = new ArrayDeque<Integer>();
private int least,count,v;
Set<Integer>[] cities;
private int[] risk;
Graph(int n,String[] risk){
cities = new HashSet[n];
this.risk = new int[n];
for(int i =0;i<n;i++){
cities[i] = new HashSet<>();
}
for(int i =0;i<n;i++){
this.risk[i] = Integer.parseInt(risk[i]);
}
visited = new boolean[n];
}
public void add(int x,int y){
cities[x].add(y);
cities[y].add(x);
}
}*/
static int root(int arr[],int i)
{
while(arr[i]!=i)
{
i=arr[i];
}
return i;
}
static boolean find(int arr[],int a,int b)
{
if(root(arr,a)==root(arr,b))
{
return true;
}
else
return false;
}
static void weighted_union(int arr[],int size[],int a,int b)
{
int root_a=root(arr,a);
int root_b=root(arr,b);
if(root_a!=root_b)
{
if(size[root_a]<size[root_b])
{
arr[root_a]=arr[root_b];
size[root_b]+=size[root_a];
}
else
{
arr[root_b]=arr[root_a];
size[root_a]+=size[root_b];
}
// count--;
}
}
static class Pair implements Comparable<Pair>
{
private long first;
private long index;
//private long second;;
public Pair(long i, long j)
{
this.first = i;
this.index = j;
}
public Pair() {
// TODO Auto-generated constructor stub
}
public long getFirst() { return first; }
//public long getSecond() { return second; }
public long getIndex() { return index ;}
public void setFirst(long k) { this.first=k ; }
public void setIndex(long k) { this.index=k ;}
//public void setSecond(long k) { this.second=k ;}
@Override
public int compareTo(Pair o)
{
return Long.compare(this.first, o.first);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream inputstream) {
reader = new BufferedReader(new InputStreamReader(inputstream));
tokenizer = null;
}
public String nextLine(){
String fullLine=null;
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
fullLine=reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return fullLine;
}
return fullLine;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | # import sys
# sys.stdin=open("input.in",'r')
# sys.stdout=open("out.out",'w')
n=int(input())
z=0
a=[]
x,y=0,0
for i in range(n):
l,r=map(int,input().split())
a.append((l,r))
x+=l
y+=r
m=abs(x-y)
for i in range(n):
k=abs((x-a[i][0]+a[i][1])-(y-a[i][1]+a[i][0]))
if k>m:
z=i+1
m=k
print(z)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 |
import java.io.*;
import java.util.*;
/**
*
* @author Sourav Kumar Paul (spaul100)
* NIT Silchar
*/
public class SolveB {
public static Reader in;
public static PrintWriter out;
public static long mod = 1000000007;
public static long inf = 100000000000000000l;
public static long fac[],inv[];
public static int union[];
public static void solve(){
int n = in.nextInt();
int ls[] = new int[n];
int rs[] = new int[n];
int left = 0, right =0;
for(int i=0; i<n; i++)
{
ls[i] = in.nextInt();
rs[i] = in.nextInt();
left += ls[i];
right += rs[i];
}
int max = Math.abs(left - right);
int ans = 0;
for(int i=0; i<n; i++)
{
if(Math.abs(left-ls[i] + rs[i] - (right - rs[i] + ls[i])) > max)
{
max = Math.abs(left-ls[i] + rs[i] - (right - rs[i] + ls[i]));
ans = i+1;
}
}
out.println(ans);
}
/**
* ############################### Template ################################
*/
public static class Pair implements Comparable{
int x,y;
Pair(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public int compareTo(Object o)
{
Pair pp = (Pair)o;
if(pp.x == x)
return 0;
else if (x>pp.x)
return 1;
else
return -1;
}
}
public static void init()
{
for(int i=0; i<union.length; i++)
union[i] = i;
}
public static int find(int n)
{
return (union[n]==n)?n:(union[n]=find(union[n]));
}
public static void unionSet(int i ,int j)
{
union[find(i)]=find(j);
}
public static boolean connected(int i,int j)
{
return union[i]==union[j];
}
public static long gcd(long a, long b) {
long x = Math.min(a,b);
long y = Math.max(a,b);
while(x!=0)
{
long temp = x;
x = y%x;
y = temp;
}
return y;
}
public static long modPow(long base, long exp, long mod) {
base = base % mod;
long result =1;
while(exp > 0)
{
if(exp % 2== 1)
{
result = (result * base) % mod;
exp --;
}
else
{
base = (base * base) % mod;
exp = exp >> 1;
}
}
return result;
}
public static void cal()
{
fac = new long[1000005];
inv = new long[1000005];
fac[0]=1;
inv[0]=1;
for(int i=1; i<=1000000; i++)
{
fac[i]=(fac[i-1]*i)%mod;
inv[i]=(inv[i-1]*modPow(i,mod-2,mod))%mod;
}
}
public static long ncr(int n, int r)
{
return (((fac[n]*inv[r])%mod)*inv[n-r])%mod;
}
SolveB() throws IOException {
in = new Reader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
out.close();
}
public static void main(String args[]) {
new Thread(null, new Runnable() {
public void run() {
try {
new SolveB();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "1", 1 << 26).start();
}
public static class Reader {
public BufferedReader reader;
public StringTokenizer st;
public Reader(InputStreamReader stream) {
reader = new BufferedReader(stream);
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() throws IOException{
return reader.readLine();
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
a = []
b = []
for _ in range(n):
x,y = map(int,input().split())
a.append(x)
b.append(y)
x = sum(a)
y = sum(b)
ans = abs(x-y)
ind = 0
for i in range(n):
z = abs(((x-a[i])+b[i])-((y-b[i])+a[i]))
if z > ans:
ans = z
ind = i+1
print(ind)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=int(input())
a=[]
b=[]
for i in range(n):
x=[int(x) for x in input().split(' ')]
a.append(x[0])
b.append(x[1])
sa=sum(a)
sb=sum(b)
anss=[abs(sa-sb)]
for i in range(n):
nsa=sa-a[i]+b[i]
nsb=sb-b[i]+a[i]
anss.append(abs(nsa-nsb))
print(anss.index(max(anss)))
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
li = []
ri = []
l_sum = 0
r_sum = 0
for i in range(n):
a, b = map(int, input().split())
li.append(a)
ri.append(b)
l_sum += a
r_sum += b
j = -1
diff = abs(l_sum - r_sum)
for i in range(n):
m = abs((l_sum - li[i] + ri[i]) - (r_sum - ri[i] + li[i]))
if m > diff:
j = i
diff = m
print(j + 1) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=input()
a=[map(int,raw_input().split()) for i in range(n)]
l=r=0
for ll,rr in a:
l+=ll
r+=rr
ans,k=abs(l-r),0
for i in range(len(a)):
ll,rr=a[i]
ans1=abs(l-r+2*(rr-ll))
if ans<ans1:
ans,k=ans1,i+1
print k
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int> > v;
int cur = 0, max = 0, index = 0, s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
int b, c;
cin >> b >> c;
v.push_back(make_pair(b, c));
s1 += b;
s2 += c;
}
max = abs(s1 - s2);
for (int i = 0; i < n; i++) {
cur = s1 - s2 - 2 * v[i].first + 2 * v[i].second;
if (abs(cur) > max) {
max = abs(cur);
index = i + 1;
}
}
cout << index << endl;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(raw_input())
k = -1
sl, sr = 0, 0
l = []
for i in range(n):
left, right = map(int, raw_input().split())
l.append((left, right))
sl, sr = sl + left, sr + right
# print l
# print sl, sr
m = abs(sl - sr)
for i in range(n):
sl, sr = sl - l[i][0] + l[i][1], sr - l[i][1] + l[i][0]
if abs(sl - sr) > m:
k = i
m = abs(sl - sr)
sl, sr = sl - (-l[i][0] + l[i][1]), sr - (-l[i][1] + l[i][0])
print k + 1 | PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
int n, l[100010], r[100010], tA, tB, Ans, k;
int main() {
scanf("%d", &n);
tA = tB = 0;
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), tA += l[i], tB += r[i];
Ans = abs(tA - tB);
for (int i = 1; i <= n; i++)
if (Ans < abs(tA - tB + ((r[i] - l[i]) << 1))) {
k = i;
Ans = abs(tA - tB + ((r[i] - l[i]) << 1));
}
printf("%d\n", k);
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, flag, i, L = 0, R = 0, a = 0;
cin >> n;
int l[n], r[n], result[n];
for (i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
L += l[i];
R += r[i];
}
int max = abs(L - R);
for (int i = 1; i <= n; i++) {
result[i] = abs((L - l[i] + r[i]) - (R - r[i] + l[i]));
if (max < result[i]) {
max = result[i];
a = i;
}
}
cout << a;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> arr[100000 + 5];
int main() {
cin >> n;
int L = 0, R = 0;
for (int i = 1; i <= n; i++) {
cin >> arr[i].first >> arr[i].second;
L += arr[i].first;
R += arr[i].second;
}
int ans = abs(L - R);
int id = 0;
for (int i = 1; i <= n; i++) {
if (ans < abs(L - R + 2 * (arr[i].second - arr[i].first))) {
ans = abs(L - R + 2 * (arr[i].second - arr[i].first));
id = i;
}
}
cout << id << endl;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | /*
* PDPM IITDM Jabalpur
* Asutosh Rana
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static long MOD = 1000000007;
public static void main (String[] args) throws java.lang.Exception
{
InputReader in=new InputReader(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int t = 1;//in.readInt();
while(t-->0){
int N = in.readInt();
int[][] A = new int[N][2];
int[][] C = new int[N][2];
for(int i=0;i<N;i++){
for(int j=0;j<2;j++){
A[i][j] = in.readInt();
}
}
C[0][0] = A[0][0];C[0][1] = A[0][1];
for(int i=1;i<N;i++)
for(int j=0;j<2;j++){
C[i][j] = A[i][j] + C[i-1][j];
}
int max = Math.abs(C[N-1][0]-C[N-1][1]);
int idx = -1;
for(int i=0;i<N;i++){
int count = 0;
if(i==0){
count = Math.abs(A[0][0]+C[N-1][1]-C[0][1]-A[0][1]-C[N-1][0]+C[0][0]);
}
else if(i!=N-1){
count = Math.abs(C[i-1][0]+C[N-1][0]-C[i][0]+A[i][1] - (C[i-1][1]+C[N-1][1]-C[i][1]+A[i][0]));
}
else{
count = Math.abs(C[N-2][0]+A[N-1][1] - (C[N-2][1]+A[N-1][0]));
}
if(count>max){
max = count;idx = i;
}
}
idx++;
out.println(idx);
}
out.flush();
out.close();
}
}
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 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 void readInt(int[] A){
for(int i=0;i<A.length;i++)
A[i] = readInt();
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long 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 void readLong(long[] A){
for(int i=0;i<A.length;i++)
A[i] = readLong();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public char[] readCharA(){
return readString().toCharArray();
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
} | JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #!/usr/bin/env python3
columns = int(input(''))
values = []
ltotal, rtotal = 0, 0
for i in range(0, columns):
l, r = map(int, input('').split(' '))
values.append((l, r))
ltotal += l
rtotal += r
oldBeauty = abs(ltotal - rtotal)
beauty = []
for i in range(0, columns):
l, r = values[i]
new_ltotal = ltotal - l + r
new_rtotal = rtotal - r + l
beauty.append(abs(new_ltotal - new_rtotal))
toChange = 0
diff = 0
for i in range(0, columns):
if beauty[i] > oldBeauty and beauty[i] - oldBeauty > diff:
toChange = i + 1
diff = beauty[i] - oldBeauty
print(toChange)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
left, right = [], []
for _ in range(n):
l, r = map(int, input().split())
left.append(l)
right.append(r)
lsum = sum(left)
rsum = sum(right)
beauty = abs(lsum - rsum)
ans = -1
for i in range(n):
new_beauty = abs(lsum - left[i] + right[i] - (rsum - right[i] + left[i]))
if new_beauty > beauty:
ans = i
beauty = new_beauty
print(ans + 1)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | //simple input
import java.util.*;
import java.io.*;
public class A{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int[] a1=new int[n+1];
int[] a2=new int[n+1];
int sum1=0;
int sum2=0;
for(int i=1;i<=n;i++){
a1[i]=in.nextInt();
a2[i]=in.nextInt();
sum1=sum1+a1[i];
sum2=sum2+a2[i];
}
int diff=Math.abs(sum1-sum2);
int max=diff;
int z=0;
for(int i=1;i<=n;i++){
int ans=Math.abs((sum1-a1[i]+a2[i])-(sum2-a2[i]+a1[i]));
if(ans>max){
max=ans;
z=i;
}
}
System.out.println(z);
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
int N;
cin >> N;
int tot = 0;
int l[100001], r[100001];
for (int i = 1; i <= N; i++) {
cin >> l[i] >> r[i];
tot += l[i] - r[i];
}
int ma = abs(tot);
int ma_n = 0;
for (int i = 1; i <= N; i++) {
int tmp = tot - 2 * (l[i] - r[i]);
if (abs(tmp) > ma) {
ma = abs(tmp);
ma_n = i;
}
}
cout << ma_n << endl;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 |
if __name__ == '__main__':
n = int(input())
l = []
r = []
max_lr = 0
max_lr_index = -1
max_rl = 0
max_rl_index = -1
for i in range(n):
lr = list(map(int,input().split()))
l.append(lr[0])
r.append(lr[1])
if (lr[0] - lr[1] > max_lr):
max_lr = lr[0] - lr[1]
max_lr_index = i
if (lr[1] - lr[0] > max_rl):
max_rl = lr[1] - lr[0]
max_rl_index = i
if sum(l) + max_rl > sum(r) + max_lr:
print(max_rl_index + 1)
else:
print(max_lr_index + 1)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | Limite = int( raw_input() )
Rpta = 0
Posicion = 0
p = [map(int, raw_input().split()) for _ in xrange( Limite)]
Valores = [pp[0]-pp[1] for pp in p]
AnteRpta = sum( Valores )
ActuRpta = abs( AnteRpta )
for i in xrange( Limite ):
Candidato = abs( AnteRpta - 2*Valores[ i ] )
if ActuRpta < Candidato:
ActuRpta , Posicion = Candidato , i + 1;
print Posicion | PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number_of_columns = sc.nextInt();
int[] left_column = new int[100000]; //Π² Π·Π°Π΄Π°ΡΠ΅ Π±ΡΠ»ΠΎ Π½Π°ΠΏΠΈΡΠ°Π½ΠΎ ΡΡΠΎ ΠΌΠ°ΠΊΡΠΈΠΌΡΠΌ 100
int[] right_column = new int[100000];
int sum_left = 0; // ΡΡΠΌΠΌΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ Π²ΠΎ Π²ΡΠ΅ΠΉ ΠΊΠΎΠ»ΠΎΠ½Π½Π΅
int sum_right = 0;
int index = -1;
for (int i = 0; i < number_of_columns; i++) {
left_column[i] = sc.nextInt();
right_column[i] = sc.nextInt();
sum_left+=left_column[i];
sum_right+=right_column[i];
}
int max = Math.abs(sum_left-sum_right);
// int maxk = 0;
for (int i = 0; i < number_of_columns; i++) {
if (Math.abs((sum_left - left_column[i] + right_column[i]) - (sum_right - right_column[i] + left_column[i])) > max) {
max = Math.abs((sum_left - left_column[i] + right_column[i]) - (sum_right - right_column[i] + left_column[i]));
index = i;
}
}
System.out.println(index+1);
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | N = int(raw_input())
P =[ map(int , raw_input().split()) for _ in xrange(N)]
V = [ PP[0]- PP[1] for PP in P ]
S = sum(V)
Mx = abs(S)
Pos = 0
for ___ in xrange(N):
Nx = abs(S-2*V[___])
if Nx>Mx:
Mx,Pos = Nx , ___ +1
print Pos
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | T = int(raw_input())
M = -float('inf')
m = float('inf')
S = 0
temp = []
newS = []
for i in xrange(T):
a, b = map(int, raw_input().split() )
S += (a - b)
temp.append(b - a)
newS = [abs(S + 2 * temp[i]) for i in xrange(T)]
newS.append(abs(S))
print (newS.index(max(newS)) + 1) % (T + 1) | PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
beauty_of_column = []
for i in range (0, n):
l, r = [int(x) for x in input().split()]
beauty_of_column.append(l - r)
currentBeauty = sum(beauty_of_column)
res = 0
maxBeauty = abs(currentBeauty)
for i in range (0, n):
beauty = currentBeauty - 2 * beauty_of_column[i]
if abs(beauty) > maxBeauty:
maxBeauty = abs(beauty)
res = i + 1
print(res)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, r = 0, l = 0, num = -1, a[100000], b[100000], max;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
l += a[i];
r += b[i];
}
max = abs(l - r);
for (int i = 0; i < n; i++) {
if (max < abs((l - a[i] + b[i]) - (r - b[i] + a[i]))) {
num = i;
max = abs((l - a[i] + b[i]) - (r - b[i] + a[i]));
}
}
cout << ++num;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(raw_input())
matrix = []
for i in xrange(n):
matrix.append(tuple(map(int, raw_input().split())))
L, R = [],[]
for i in matrix:
L.append(i[0])
R.append(i[1])
a = sum(R)
b = sum(L)
ans = abs(a-b)
index = 0
for i in xrange(n):
if abs(b - 2*L[i] + 2*R[i] - a) > ans:
ans = abs(b - 2*L[i] + 2*R[i] - a)
index = i+1
print index
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.lang.Math;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Parade {
public static int sumArr(int[] arr){
int sum = 0;
for(int x : arr)
sum+=x;
return sum;
}
public static void main(String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
int[] left = new int[n];
int[] right = new int[n];
int[] scans = new int[n+1];
for(int i=0; i<n; ++i){
String[] inputs = br.readLine().split(" ");
left[i] = Integer.parseInt(inputs[0]);
right[i] = Integer.parseInt(inputs[1]);
}
int S1 = sumArr(left);
int S2 = sumArr(right);
int temp1 = S1;
int temp2 = S2;
scans[0] = Math.abs(S1-S2);
int max = scans[0];
int holdI = 0;
for(int i=0; i<n; ++i){
S1 = S1 + right[i] -left[i];
S2 = S2 + left[i] - right[i];
scans[i+1] = Math.abs(S1-S2);
if(scans[i+1]>max){
max = scans[i+1];
holdI = i+1;
}
S1 = temp1;
S2 = temp2;
}
System.out.print(holdI);
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
int SL[MAXN], SR[MAXN];
int A[MAXN][2], n;
int main() {
while (~scanf("%d", &n)) {
SL[0] = SR[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &A[i][0], &A[i][1]);
SL[i] = A[i][0] + SL[i - 1];
SR[i] = A[i][1] + SR[i - 1];
}
int maxn = abs(SL[n] - SR[n]);
int ans = 0;
for (int i = 1; i <= n; i++) {
int _SL = SL[n] - A[i][0] + A[i][1];
int _SR = SR[n] - A[i][1] + A[i][0];
int _ans = abs(_SL - _SR);
if (maxn < _ans) {
maxn = _ans;
ans = i;
}
}
printf("%d\n", ans);
}
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | N = int(input())
Groups = []
Left, Right, CurrentMax, Index = 0, 0, 0, 0,
for i in range(N):
Groups.append(list(map(int, input().split())))
Left, Right = Left + Groups[i][0], Right + Groups[i][1]
CurrentMax = abs(Left - Right)
for i in range(N):
Temp = abs((Left + Groups[i][1] - Groups[i][0]) - (Right + Groups[i][0] - Groups[i][1]))
if Temp > CurrentMax:
CurrentMax = Temp
Index = i + 1
print(Index)
# UB_CodeForces
# Advice: Destroy what wants to destroy you
# Location: Behind my desk
# Caption: Preparing myself for the most important test in my life
# CodeNumber: 534
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int Abs(int x) { return x < 0 ? (-x) : x; }
int n, l[100010], r[100010], ans, al, ar, ass;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &l[i], &r[i]);
al += l[i];
ar += r[i];
}
ass = Abs(al - ar);
for (int i = 1; i <= n; ++i)
if (Abs(al - l[i] + r[i] - (ar - r[i] + l[i])) > ass) {
ass = Abs(al - l[i] + r[i] - (ar - r[i] + l[i]));
ans = i;
}
printf("%d\n", ans);
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = 0
a = None
left, rght = -1, -1
def read(t=None):
string = raw_input()
return string if t is None else [t(x) for x in string.split()]
def swap(i):
global left, rght
l = left - a[i][0] + a[i][1]
r = rght - a[i][1] + a[i][0]
b = abs(l-r)
#print "swap: %2d: %3d(%3d, %3d)"%(i+1, b, l, r)
return b
if __name__ == "__main__":
n = int(read())
a = []
for i in xrange(n):
a.append(read(int))
#print a
left, rght = 0, 0
for i in xrange(n):
left += a[i][0]
rght += a[i][1]
bt = abs(left-rght)
idx = -1
#print "raw: %2d: %2d, %2d"%(bt, left, rght)
for i in xrange(n):
b = swap(i)
if b > bt:
#print "idx: %d ---> %d"%(idx+1, i+1)
bt = b
idx = i
print idx+1
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | t = int(raw_input())
left = 0
right = 0
soldiers = []
for i in xrange(1, t+1):
l, r = map(int, raw_input().split())
soldiers.append((l, r))
left += l
right += r
index = 0
start = abs(left-right)
for i in xrange(len(soldiers)):
temp = abs((left - soldiers[i][0] + soldiers[i][1]) - (right + soldiers[i][0] - soldiers[i][1]))
if temp > start:
start = temp
index = i + 1
print index
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
l=[];r=[]
la=0
ra=0
for i in range(n):
L,R = map(int,input().split())
l.append(L)
r.append(R)
la+=l[i]
ra+=r[i]
ans = 0
nice = abs(la-ra)
for i in range(n):
if abs((la-l[i]+r[i])-(ra-r[i]+l[i])) > nice:
ans = i + 1
nice = abs((la-l[i]+r[i])-(ra-r[i]+l[i]))
print(ans) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.io.*;
import java.util.*;
import java.math.*;
public class B {
public static void main(String args[]) {
try {
InputReader in = new InputReader(System.in);
int n = in.readInt();
int l[] = new int[n];
int r[] = new int[n];
int suml = 0;
int sumr = 0;
for (int i = 0; i < n; i++) {
l[i] = in.readInt();
r[i] = in.readInt();
suml += l[i];
sumr += r[i];
}
int originalBeauty = Math.abs(suml - sumr);
int maxBeauty = originalBeauty;
int ind = -1;
for (int i = 0; i < n; i++) {
int newsl = suml - l[i] + r[i];
int newsr = sumr - r[i] + l[i];
int beauty = Math.abs(newsl - newsr);
if (maxBeauty < beauty) {
ind = i + 1;
maxBeauty = beauty;
}
}
if (maxBeauty == originalBeauty) {
System.out.println(0);
}
else {
System.out.println(ind);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
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 peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
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 long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long 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 String readString() {
int length = readInt();
if (length < 0)
return null;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
bytes[i] = (byte) read();
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return readString();
}
public boolean readBoolean() {
return readInt() == 1;
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.io.*;
import java.util.InputMismatchException;
public class Cf1108B {
private static InputReader in = new InputReader(System.in);
private static OutputWriter out = new OutputWriter(System.out);
private static void solve() throws Exception {
int n = in.readInt();
int[] diff = new int[n];
int dif = 0;
for(int i=0; i<n; ++i){
int l = in.readInt();
int r = in.readInt();
diff[i] = l-r;
dif += diff[i];
}
int max = Math.abs(dif);
int index = -1;
for(int i=0; i<n; ++i){
if(Math.abs(dif -2*diff[i])>max){
max = Math.abs(dif -2*diff[i]);
index = i;
}
}
out.println(index+1);
}
public static void main(String[] args) throws Exception {
solve();
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buffer;
private int currentIndex;
private int bytesRead;
public InputReader(InputStream stream) {
this.stream = stream;
buffer = new byte[16384];
}
public InputReader(InputStream stream, int bufferSize) {
this.stream = stream;
buffer = new byte[bufferSize];
}
private int read() throws IOException {
if (currentIndex >= bytesRead) {
currentIndex = 0;
bytesRead = stream.read(buffer);
if (bytesRead <= 0) {
return -1;
}
}
return buffer[currentIndex++];
}
public String readString() throws IOException {
int c = read();
while (!isPrintable(c)) {
c = read();
}
StringBuilder result = new StringBuilder();
do {
result.appendCodePoint(c);
c = read();
} while (isPrintable(c));
return result.toString();
}
public int readInt() throws Exception {
int c = read();
int sign = 1;
while (!isPrintable(c)) {
c = read();
}
if (c == '-') {
sign = -1;
c = read();
}
int result = 0;
do {
if ((c < '0') || (c > '9')) {
throw new InputMismatchException();
}
result *= 10;
result += (c - '0');
c = read();
} while (isPrintable(c));
return sign * result;
}
public long readLong() throws Exception {
int c = read();
int sign = 1;
while (!isPrintable(c)) {
c = read();
}
if (c == '-') {
sign = -1;
c = read();
}
long result = 0;
do {
if ((c < '0') || (c > '9')) {
throw new InputMismatchException();
}
result *= 10;
result += (c - '0');
c = read();
} while (isPrintable(c));
return sign * result;
}
public double readDouble() throws Exception {
int c = read();
int sign = 1;
while (!isPrintable(c)) {
c = read();
}
if (c == '-') {
sign = -1;
c = read();
}
boolean fraction = false;
double multiplier = 1;
double result = 0;
do {
if ((c == 'e') || (c == 'E')) {
return sign * result * Math.pow(10, readInt());
}
if ((c < '0') || (c > '9')) {
if ((c == '.') && (!fraction)) {
fraction = true;
c = read();
continue;
}
throw new InputMismatchException();
}
if (fraction) {
multiplier /= 10;
result += (c - '0') * multiplier;
c = read();
} else {
result *= 10;
result += (c - '0');
c = read();
}
} while (isPrintable(c));
return sign * result;
}
private boolean isPrintable(int c) {
return ((c > 32) && (c < 127));
}
}
private static class OutputWriter {
private PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
} | JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | l = [0] + [sum(map(int, ('-' + input()).split())) for _ in range(int(input()))]
s, a, b = sum(l), min(l), max(l)
print(l.index(b if s < a + b else a))
# Made By Mostafa_Khaled | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
l = 0
r = 0
l1 = 0
l2 = 0
r1 = 0
r2 = 0
maxd1 = 0
maxd2 = 0
k1 = 0
k2 = 0
for i in range(n):
a, b = map(int,input().split())
l = l + a
r = r + b
if a > b and abs(a - b)>maxd1: #max left
l1 = a
r1 = b
maxd1 = abs(a-b)
k1 = i + 1
#print('max left = ',l1,'n = ',k1)
if b > a and abs(a-b)>maxd2: #max right
l2 = a
r2 = b
maxd2 = abs(a-b)
k2 = i + 1
#print('max right = ',r2,'n = ',k2)
#print(l1,r1,l2,r2)
#print(abs(l-r),abs(l-l1+r1-(r-r1+l1)),abs(l-l2+r2-(r-r2+l2)))
if abs(l-r)<abs(l-l2+r2-(r-r2+l2)) and abs(l-l1+r1-(r-r1+l1))<=abs(l-l2+r2-(r-r2+l2)):
print(k2)
elif abs(l-r)<abs(l-l1+r1-(r-r1+l1)) and abs(l-l2+r2-(r-r2+l2))<=abs(l-l1+r1-(r-r1+l1)):
print(k1)
else:
print(0) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
a, b = [], []
for i in range(n):
x, y = map(int, input().split())
a.append(x)
b.append(y)
sa = sum(a)
sb = sum(b)
d = abs(sa - sb)
i = 0
for j in range(n):
t = abs((sa + b[j] - a[j]) - (sb + a[j] - b[j]))
if t > d:
d = t
i = j + 1
print(i) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int lft[1000000 + 5];
int ri8[1000000 + 5];
int main() {
int n;
int rdiff, ldiff, rindx, lindx, sleft, sri8, lx, ly, rx, ry, maxm1, maxm2;
int sLeft, sRi8;
while (cin >> n) {
ldiff = -1, rdiff = -1;
sleft = 0;
sri8 = 0;
sLeft = 0;
sRi8 = 0;
for (int i = 0; i < n; i++) {
scanf("%d %d", &lft[i], &ri8[i]);
if (lft[i] > ri8[i]) {
if (abs(ri8[i] - lft[i]) > ldiff) {
ldiff = abs(ri8[i] - lft[i]);
lindx = i;
}
} else {
if (abs(ri8[i] - lft[i]) > rdiff) {
rdiff = abs(ri8[i] - lft[i]);
rindx = i;
}
}
sleft += lft[i];
sLeft += lft[i];
sri8 += ri8[i];
sRi8 += ri8[i];
}
int diff = abs(sri8 - sleft);
sleft = sleft - lft[lindx];
sleft += ri8[lindx];
sri8 = sri8 - ri8[lindx];
sri8 += lft[lindx];
maxm1 = abs(sri8 - sleft);
sLeft = sLeft - lft[rindx];
sLeft += ri8[rindx];
sRi8 = sRi8 - ri8[rindx];
sRi8 += lft[rindx];
maxm2 = abs(sRi8 - sLeft);
if (maxm1 > diff and maxm1 >= maxm2) {
printf("%d\n", lindx + 1);
} else if (maxm2 > diff and maxm2 > maxm1) {
printf("%d\n", rindx + 1);
} else {
printf("%d\n", 0);
}
}
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum1=0;
int sum2=0;
int L[]=new int[n];
int R[]=new int[n];
for(int i=0;i<n;i++) {
int l=sc.nextInt();
L[i]=l;
int r=sc.nextInt();
R[i]=r;
sum1+=l;
sum2+=r;
}
int beauty=0;
int beauty1=0;
int ans=0;
beauty=Math.abs(sum1-sum2);
int temp1=sum1;
int temp2=sum2;
for(int i=0;i<n;i++) {
sum1=temp1+R[i]-L[i];
sum2=temp2+L[i]-R[i];
beauty1=Math.abs(sum1-sum2);
if(beauty1>beauty) {
beauty=beauty1;
ans=i+1;
}
}
System.out.println(ans);
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 |
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = -1;
int index = -1;
int L = 0;
int R = 0;
int n = in.nextInt();
int tab[][] = new int[2][n];
for (int i = 0; i < n; i++) {
tab[0][i] = in.nextInt();
tab[1][i] = in.nextInt();
L+=tab[0][i];
R+=tab[1][i];
}
max = Math.abs(L-R);
for (int i = 0; i < n; i++) {
if(Math.abs((L-tab[0][i]+tab[1][i])-(R-tab[1][i]+tab[0][i])) > max){
max = Math.abs((L-tab[0][i]+tab[1][i])-(R-tab[1][i]+tab[0][i]));
index = i;
}
}
System.out.println(index+1);
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int max(int a, int b) {
if (a > b) return a;
return b;
}
int main() {
int n, mx = -1, a = 0, b, l = 0, r = 0, nl, nr;
scanf("%d", &n);
int c[n][2];
for (int i = 0; i < n; i++) {
scanf("%d %d", &c[i][0], &c[i][1]);
l += c[i][0];
r += c[i][1];
}
mx = abs(l - r);
for (int i = 0; i < n; i++) {
nl = l + c[i][1] - c[i][0];
nr = r + c[i][0] - c[i][1];
if (abs(nl - nr) > mx) {
mx = abs(nl - nr);
a = i + 1;
}
}
printf("%d\n", a);
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
parade = []
for k in range(n):
x = tuple(map(int,input().split()))
parade.append(x)
l = 0
r = 0
for i in parade:
l += i[0]
r += i[1]
beauty = abs(l-r)
def change(i):
return abs(l-r+2*(parade[i][1]-parade[i][0]))
k,m = 0, beauty
for i in range(len(parade)):
if change(i) > m:
k,m = i, change(i)
if m == beauty:
print(0)
else:
print(k+1)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
a = []
sl = 0
sr = 0
for i in range(n):
q,w = map(int,input().split())
a+=[(q,w)]
sl +=q
sr+=w
cur = abs(sr-sl)
m = 0
for i in range(n):
if abs((sl-a[i][0]+a[i][1]) - (sr-a[i][1]+a[i][0])) > cur:
cur = abs((sl-a[i][0]+a[i][1]) - (sr-a[i][1]+a[i][0]))
m = i+1
print(m)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
s = [[int(j) for j in input().split()] for k in range(n)]
p = [sum(x) for x in zip(*s)]
q = abs(p[0] - p[1])
index = -1
for i in range(n):
l = p[0] - s[i][0] + s[i][1]
r = p[1] - s[i][1] + s[i][0]
if abs(l - r) > q:
q = abs(l - r)
index = i
print(index + 1) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 |
import java.util.*;
import java.io.*;
/**
*
* @author Ninja_white
*/
public class Main extends Lectora {
public static void main(String[] args) throws IOException {
int n = cadena2(), s0 = 0, s1 = 0;
int a[][] = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = cadena2();
s0 += a[i][0];
a[i][1] = cadena2();
s1 += a[i][1];
}
int anteriorstring = 0, ants = s0 - s1, ini = ants;
for (int i = 0; i < n; i++) {
int s = ini - (a[i][0] - a[i][1]) * 2;
if (Math.abs(ants) < Math.abs(s)) {
ants = s;
anteriorstring = i + 1;
}
}
System.out.println(anteriorstring);
}
}
class Lectora {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
/**
* call this method to change InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
}
/**
* obtiene la siguiente palabra
*/
static String cadena1() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int cadena2() throws IOException {
return Integer.parseInt(cadena1());
}
static double cadena3() throws IOException {
return Double.parseDouble(cadena1());
}
static long cadena4() throws IOException {
return Long.valueOf(cadena1());
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
B = []
l = 0
r = 0
for i in range(n):
l += A[i][0]
r += A[i][1]
a = abs(l-r)
B.append(a)
for j in range(n):
if A[j][0]>A[j][1]:
li = A[j][0]-A[j][1]
ln = l-li
rn = r+li
a = abs(ln-rn)
B.append(a)
else:
ri = A[j][1]-A[j][0]
ln = l+ri
rn = r-ri
a = abs(ln-rn)
B.append(a)
print(B.index(max(B)))
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int t, l[100009], r[100009], ans;
long long int L = 0, R = 0, mx;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%d%d", &l[i], &r[i]);
L += l[i];
R += r[i];
}
mx = abs(L - R);
ans = 0;
for (int i = 0; i < t; i++) {
L -= l[i];
L += r[i];
R -= r[i];
R += l[i];
if (mx < abs(L - R)) {
mx = abs(L - R);
ans = i + 1;
}
L -= r[i];
L += l[i];
R -= l[i];
R += r[i];
}
cout << ans << endl;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | l = []
r = []
sum_r = 0
sum_l = 0
max_diff_r = 0
max_r = 0
max_diff_l = 0
max_l = 0
for i in range(0, int(input())):
x, y = input().split(' ')
l.append(int(x))
sum_l+=l[i]
r.append(int(y))
sum_r+=r[i]
if max_diff_r < r[i] - l[i]:
max_r=i+1
max_diff_r=r[i] - l[i]
if max_diff_l < l[i] - r[i]:
max_l=i+1
max_diff_l=l[i] - r[i]
d = max(abs(sum_r-sum_l), sum_l-sum_r+2*max_diff_r, sum_r-sum_l+2*max_diff_l)
if d == abs(sum_r-sum_l):
print(0)
elif d ==sum_r-sum_l+2*max_diff_l:
print(max_l)
else:print(max_r)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int *Mas = new int[n];
int _l, _r;
for (int i = 0; i < n; i++) {
cin >> _l >> _r;
Mas[i] = _l - _r;
}
int S = 0;
for (int i = 0; i < n; i++) S += Mas[i];
int max = abs(S), index = -1;
for (int i = 0; i < n; i++) {
if (i) S += 2 * Mas[i - 1];
S -= 2 * Mas[i];
if (max < abs(S)) {
max = abs(S);
index = i;
}
}
cout << ++index;
delete[] Mas;
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = int(input())
mi = 100000000
ma = -100000000
sum = 0
minI = -1
maxI = -1
for i in range(n):
raw = input().split()
l = int(raw[0])
r = int(raw[1])
dif = l - r
if (mi > dif):
mi = dif
minI = i
# minL = l
# minR = r
if (ma < dif):
ma = dif
maxI = i
# maxL = l
# maxR = r
sum += dif
i = 0
lsum = sum - 2 * mi
rsum = sum - 2 * ma
# print(sum)
# print(lsum)
# print(rsum)
sum = abs(sum)
lsum = abs(lsum)
rsum = abs(rsum)
if (sum >= max(lsum, rsum)):
print(0)
quit()
if (lsum > rsum):
print(minI + 1)
else:
print(maxI + 1)
# if (sum > 0):
# sum -= mi
# i = minI
# else:
# sum += ma
# i = maxI
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Parade
{
public static void main(String[] args)
{
Parade solver = new Parade();
solver.Solve();
}
public FastScanner in;
public PrintWriter out;
public Problem p;
public void Solve()
{
in = new FastScanner();
out = new PrintWriter(System.out);
p = new Problem();
int n = in.nextInt();
int[] c1 = new int[n];
int[] c2 = new int[n];
for(int i = 0; i < n; i++)
{
c1[i] = in.nextInt();
c2[i] = in.nextInt();
}
p.Init(c1, c2);
out.println(p.GetIndexSol2(p.c1, p.c2));
out.flush();
out.close();
}
public class Problem
{
public Problem()
{
}
public int GetMax(int a,int b)
{
int ans = b;
if(a > b)
ans = a;
else
ans = b;
return ans;
}
public int GetArrSum(int[] arr)
{
int ans = 0;
for(int i = 0; i < arr.length; i++)
ans = ans + arr[i];
return ans;
}
public int[] c1;
public int[] c2;
public void Init(int[] _c1,int[] _c2)
{
c1 = _c1;
c2 = _c2;
}
public int GetIndexSol1(int[] _c1,int[] _c2)
{
int ans = -999999;
int max = -999999;
int constSum1 = GetArrSum(_c1);
int constSum2 = GetArrSum(_c2);
for(int i = 0; i < _c1.length; i++)
{
int sumc1 = 0;
int sumc2 = 0;
sumc1 = constSum1;
sumc2 = constSum2;
int diff = sumc1 - sumc2;
if(diff < 0)
diff = diff * -1;
max = GetMax(diff,max);
int revIndex = _c1.length-1 - i;
int tempmem = _c2[revIndex];
_c2[revIndex] = _c1[revIndex];
_c1[revIndex] = tempmem;
sumc1 = GetArrSum(_c1);
sumc2 = GetArrSum(_c2);
diff = sumc1 - sumc2;
if(diff < 0)
diff = diff * -1;
if(diff >= max)
ans = revIndex;
max = GetMax(diff,max);
int reswap = _c2[revIndex];
_c2[revIndex] = _c1[revIndex];
_c1[revIndex] = reswap;
}
ans = ans + 1;
if(ans < 0)
ans = 0;
return ans;
}
public int GetIndexSol2(int[] _c1,int[] _c2)
{
int ans = -999999;
int max = -999999;
int constSum1 = GetArrSum(_c1);
int constSum2 = GetArrSum(_c2);
for(int i = 0; i < _c1.length; i++)
{
int sumc1 = 0;
int sumc2 = 0;
sumc1 = constSum1;
sumc2 = constSum2;
int diff = sumc1 - sumc2;
if(diff < 0)
diff = diff * -1;
max = GetMax(diff,max);
int revIndex = _c1.length-1 - i;
int tempmem = _c2[revIndex];
_c2[revIndex] = _c1[revIndex];
_c1[revIndex] = tempmem;
sumc1 = sumc1 - _c2[revIndex];
sumc1 = sumc1 + _c1[revIndex];
sumc2 = sumc2 - _c1[revIndex];
sumc2 = sumc2 + _c2[revIndex];
diff = sumc1 - sumc2;
if(diff < 0)
diff = diff * -1;
if(diff >= max)
ans = revIndex;
max = GetMax(diff,max);
int reswap = _c2[revIndex];
_c2[revIndex] = _c1[revIndex];
_c1[revIndex] = reswap;
}
ans = ans + 1;
if(ans < 0)
ans = 0;
return ans;
}
}
public class FastScanner
{
public BufferedReader br;
public StringTokenizer st;
public FastScanner(String fileName)
{
try
{
br = new BufferedReader(new FileReader(fileName));
}
catch (FileNotFoundException e)
{
}
}
public FastScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
public String nextToken()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(nextToken());
}
public long nextLong()
{
return Long.parseLong(nextToken());
}
public double nextDouble()
{
return Double.parseDouble(nextToken());
}
}
}
| JAVA |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=int(input())
l1=[]
r1=[]
L=0
R=0
for i in range(n):
l,r=map(int,input().split())
L+=l
R+=r
l1.append(l)
r1.append(r)
max=abs(L-R)
ans=0
for i in range(n):
if(abs((L-l1[i]+r1[i])-(R-r1[i]+l1[i]))>max):
max=abs((L-l1[i]+r1[i])-(R-r1[i]+l1[i]))
ans=i+1
print(ans) | PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n=int(input())
M=[list(map(int,input().split())) for i in range(n) ]
k1=0
k2=0
ma=0
p=0
for i in range(n) :
k1=k1+M[i][0]
k2=k2+M[i][1]
ma=abs(k1-k2)
for i in range(n) :
if abs((k1-M[i][0])-(k2-M[i][1])-M[i][0]+M[i][1])>ma :
ma=abs((k1-M[i][0])-(k2-M[i][1])-M[i][0]+M[i][1])
p=i+1
print(p)
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | n = input()
diffs = []
for _ in xrange(n):
l,r = map(int, raw_input().split())
diffs.append(l-r)
curr_diff = sum(diffs)
best = abs(curr_diff)
best_index = -1
for i in xrange(n):
if abs(curr_diff-2*diffs[i]) > best:
best_index = i
best = abs(curr_diff-2*diffs[i])
print best_index + 1
| PYTHON |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | from sys import exit
n = int(input())
R = 0
L = 0
mr = (0, 0)
ml = (0, 0)
for i in range(1, n+1):
(li, ri) = (int(_) for _ in input().split())
R += ri
L += li
if ri > li:
mr = max(mr, (ri - li, i))
elif li > ri:
ml = max(ml, (li - ri, i))
if R + ml[0] > L + mr[0]:
print(ml[1])
elif R + ml[0] < L + mr[0]:
print(mr[1])
else:
print(ml[1])
| PYTHON3 |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, n1 = 1, n2 = 1;
scanf("%d", &n);
scanf("%d %d", &a, &b);
a -= b;
int mn = a, mx = a, sum = a;
for (int i = 2; i <= n; i++) {
scanf("%d %d", &a, &b);
a -= b;
if (a < mn) {
mn = a;
n1 = i;
}
if (a > mx) {
mx = a;
n2 = i;
}
sum += a;
}
int t = abs(sum), no = 0;
if (abs(sum - 2 * mn) > t) {
t = abs(sum - 2 * mn);
no = n1;
}
if (abs(sum - 2 * mx) > t) {
t = abs(sum - 2 * mn);
no = n2;
}
printf("%d\n", no);
return 0;
}
| CPP |
733_B. Parade | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of columns.
The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
Input
3
5 6
8 9
10 3
Output
3
Input
2
6 5
5 6
Output
1
Input
6
5 9
1 3
4 8
4 5
23 54
12 32
Output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | 2 | 8 | import java.util.*;
import java.io.*;
public class Parade
{
public static void main(String args[]) throws Exception
{
BufferedReader f=new BufferedReader(new InputStreamReader(System.in));
int runs=Integer.parseInt(f.readLine());
int[][] arr=new int[runs][2];
int left=0;
int right=0;
int max=0;
for(int x=0;x<runs;x++)
{
StringTokenizer st=new StringTokenizer(f.readLine());
arr[x][0]=Integer.parseInt(st.nextToken());
arr[x][1]=Integer.parseInt(st.nextToken());
left+=arr[x][0]-arr[x][1];
right+=arr[x][1]-arr[x][0];
}
max=Math.abs(left);
int pos=-1;
for(int x=0;x<runs;x++)
{
int temp=right-2*arr[x][1]+2*arr[x][0];
if(Math.abs(temp)>max)
{
max=Math.abs(temp);
pos=x;
}
}
System.out.println(pos+1);
}
} | JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.