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
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; int main() { char s[100005]; int n, l, r; while ((scanf("%c", &s[++n]) == 1) && (s[n] >= '0') && (s[n] <= '9')) s[n] -= '0'; l = 1; r = --n; if (s[l] != s[r]) { s[l]--; s[l + 1] += 10; if (s[l] == 0) l++; } while (l <= r) { if (s[l] != s[r]) { if ((s[l] - s[r] >= 10) && (s[r] < 10)) { s[r - 1]--; s[r] += 10; } if (s[l] - s[r] == 1) { s[l]--; s[l + 1] += 10; } } if (s[l] != s[r]) break; if (l != r) { s[l] = s[l] - (s[r] >> 1); s[r] >>= 1; } else if (((s[l] >> 1) << 1) == s[l]) s[l] >>= 1; else break; if (s[l] > 9 || s[l] < 0 || s[r] > 9 || s[r] < 0) break; l++; r--; } if (l <= r) { printf("0"); return 0; } l = 1; if (s[l] == 0) l++; while (l <= n) { printf("%d", s[l]); l++; } return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; char num[100005]; int n[100005]; char left[100005]; int len; bool Cout[100005]; int ans[100005]; bool check(int start) { for (int i = start, j = len - 1; j >= i; j--, i++) { int t1 = n[i] + 10 * Cout[i], t2 = n[j] + 10 * Cout[j]; if (t1 == t2) { if (t1 > 18 || t1 < 0 || i == j && t1 & 1) return 0; ans[i] = (t1 + 1) / 2, ans[j] = t1 - ans[i]; } else { if (abs(t1 - t2) == 1) { if (t1 > t2) { if (Cout[i + 1]) return 0; n[i]--, Cout[i + 1] = true, j++, i--; } else { if (Cout[j + 1]) return 0; n[j]--, Cout[j + 1] = true, j += 2, i -= 2; } } else if (abs(t1 - t2) == 9 || abs(t1 - t2) == 11 || abs(t1 - t2) == 10) { if (t1 > t2) { if (Cout[j]) return 0; n[j - 1]--, Cout[j] = true, j++, i--; } else { if (Cout[i] || abs(t1 - t2) == 10) return 0; n[i - 1]--, Cout[i] = true, j += 2, i -= 2; } } else return 0; } } if (ans[start] == 0) return 0; for (int i = start; i < len; i++) printf("%d", ans[i]); puts(""); return 1; } int main() { scanf("%s", num); len = strlen(num); for (int i = 0; i < len; i++) n[i] = num[i] - '0'; if (len == 1 && (num[0] - '0') & 1) return puts("0"), 0; else if (len == 1) return printf("%c\n", (num[0] - '0') / 2 + '0'), 0; memset(Cout, false, sizeof(Cout)); if (check(0)) return 0; if (num[0] == '1') { for (int i = 0; i < len; i++) n[i] = num[i] - '0'; memset(Cout, false, sizeof(Cout)); Cout[1] = true; if (check(1)) return 0; } puts("0"); return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; inline int init() { int now = 0, ju = 1; char c; bool flag = false; while (1) { c = getchar(); if (c == '-') ju = -1; else if (c >= '0' && c <= '9') { now = now * 10 + c - '0'; flag = true; } else if (flag) return now * ju; } } inline long long llinit() { long long now = 0, ju = 1; char c; bool flag = false; while (1) { c = getchar(); if (c == '-') ju = -1; else if (c >= '0' && c <= '9') { now = now * 10 + c - '0'; flag = true; } else if (flag) return now * ju; } } int num[100005], n, ans[100005]; char s[100005]; bool check() { for (int i = 1; i <= (n + 1) / 2;) { if (num[i] == num[n - i + 1]) i++; else if (num[i] == num[n - i + 1] + 1 || num[i] == num[n - i + 1] + 11) { num[i]--; num[i + 1] += 10; } else if (num[i] == num[n - i + 1] + 10) { num[n - i]--; num[n - i + 1] += 10; } else return false; } if (n % 2 == 1) { if ((num[(n + 1) / 2] % 2 == 1) || (num[(n + 1) / 2] > 18) || (num[(n + 1) / 2] < 0)) return false; else ans[(n + 1) / 2] = num[(n + 1) / 2] / 2; } for (int i = 1; i <= (n + 1) / 2; i++) { if (num[i] > 18 || num[i] < 0) return false; ans[i] = (num[i] + 1) / 2; ans[n - i + 1] = num[i] / 2; } return ans[1] > 0; } int main() { int len; scanf("%s", s + 1); n = strlen(s + 1); for (int i = 1; i <= n; i++) { num[i] = s[i] - '0'; } if (check()) for (int i = 1; i <= n; i++) printf("%d", ans[i]); else if (s[1] == '1' && n > 1) { for (int i = 1; i < n; i++) num[i] = s[i + 1] - '0'; n--; num[1] += 10; if (check()) for (int i = 1; i <= n; i++) printf("%d", ans[i]); else printf("0"); } else printf("0"); return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> char s[100010]; int a[100010], ans[100010]; int main() { scanf("%s", s); int len = strlen(s); for (int i = 0; i < len; i++) a[i] = s[i] - '0'; int l = 0, r = len - 1, fa = 1; if (a[l] != a[r]) { a[l]--; a[l + 1] = a[l + 1] + 10; if (a[l] == 0) l++; } while (l <= r) { if (a[l] != a[r]) { if (a[l] - a[r] >= 10) { a[r - 1]--; a[r] = a[r] + 10; } if (a[l] - a[r] == 1) { a[l]--; a[l + 1] = a[l + 1] + 10; } } if (a[r] != a[l]) { fa = 0; break; } if (l != r) { ans[l] = a[l] - a[r] / 2; ans[r] = a[r] / 2; } else { if (a[l] & 1) { fa = 0; break; } ans[l] = a[r] / 2; } if (ans[l] < 0 || ans[l] > 9 || ans[r] < 0 || ans[r] > 9) { fa = 0; break; } l++; r--; } if (l <= r) fa = 0; if (fa == 0) { printf("%d\n", 0); return 0; } int num = 0; if (ans[num] == 0) num++; for (int i = num; i < len; i++) printf("%d", ans[i]); return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; char s[100005]; int n, a[100005], ans[100005]; inline bool find() { int i = 1, j = n; while (i <= j) { if (a[i] == a[j]) { i++; j--; } else if (a[i] == a[j] + 10) { a[j - 1]--; a[j] += 10; } else if ((a[i] == a[j] + 1) || (a[i] == a[j] + 11)) { a[i]--; a[i + 1] += 10; } else return false; } if ((i - 1 == j + 1) && ((a[i - 1] > 18) || (a[i - 1] < 0) || (a[i - 1] % 2 == 1))) return false; if (a[1] == 0) return false; i--; j++; while (i) { if ((a[i] > 18) || (a[i] < 0)) return false; ans[i] = (a[i] + 1) / 2; ans[j] = a[i] - ans[i]; i--; j++; } for (int i = 1; i <= n; i++) printf("%d", ans[i]); printf("\n"); return true; } int main() { scanf("%s", s + 1); for (int i = 1; i <= strlen(s + 1); i++) a[i] = s[i] - '0'; n = strlen(s + 1); if (find()) return 0; if ((s[1] != '1') || (n == 1)) { printf("0\n"); return 0; } for (int i = 1; i <= n; i++) a[i - 1] = s[i] - '0'; a[1] += 10; n--; if (find()) return 0; printf("0\n"); return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; long long powmod(long long a, long long b, long long MOD) { long long res = 1; a %= MOD; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } const int N = 1E5 + 5; char s[N]; int a[N]; char out[N]; bool solve(int n) { for (int l = 0, r = n - 1; l <= r; l++, r--) { if (a[l] >= a[r] + 10) { a[r - 1]--; a[r] += 10; } if (a[l] == a[r] + 1) { a[l]--; a[l + 1] += 10; } if (a[l] != a[r]) return false; int temp = (a[l] + 1) / 2; out[l] = temp + '0'; out[r] = a[l] - temp + '0'; } if (!a[0]) return false; if ((n & 1) && (a[n / 2] & 1)) return false; for (int i = 0; i < n; i++) { if (a[i] < 0 || a[i] > 18) return false; } out[n] = '\0'; puts(out); return true; } int main() { while (gets(s)) { int n = strlen(s); for (int i = 0; i < n; i++) { a[i] = s[i] - '0'; } if (solve(n)) continue; if (s[0] == '1' && n != 1) { for (int i = 1; i < n; i++) { a[i - 1] = s[i] - '0'; } a[0] += 10; if (solve(n - 1)) continue; } puts("0"); } return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
import java.util.Scanner; public class R625D { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); if (s.equals("1")) { System.out.print("0"); return; } int[] n = new int[s.length()]; int[] ans = new int[s.length()]; for (int i=0; i<s.length(); i++) n[s.length()-i-1]=s.charAt(i)-'0'; int upL, upR, upL_; for (int m=n.length-1; m<=n.length; m++) { upR=0; if (m==n.length-1) { if (n[n.length-1]!=1) continue; upL=1; } else { upL=0; } int i; for (i=0; i<(m/2+m%2); i++) { upL_=0; int mod=n[i]-upR; if (mod==-1) {mod=9; upL_=1;} if (i==m-i-1 && (upL*10+mod)%2==1) break; if (mod==n[m-i-1]) { if (mod==9 && upL==1) break; ans[i]=(upL*10+mod)/2; ans[m-i-1]=upL*10+mod-ans[i]; upR=upL|upL_; upL=0; } else if (mod==(upL*10+n[m-i-1]-1)%10) { ans[i]=(upL*10+n[m-i-1]-1)/2; ans[m-i-1]=upL*10+n[m-i-1]-1-ans[i]; if (i==0 && ans[m-i-1]==0) break; upR=(upL*10+n[m-i-1]-1+upR)/10; upL=1; } else break; } if (i==m/2+m%2) { if ((m%2)==0 && (upL!=upR)) break; for (i=m-1; i>=0; i--) System.out.print(ans[i]); return; } } System.out.println("0"); } }
JAVA
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; int a[100010], n, ans[100010]; bool f[100010][10][2][2]; char c; bool dfs(int k, int r1, int r2, int x) { if ((n & 1) == 0 && k > n / 2) return r1 == r2; else if ((n & 1) == 1 && k > n / 2) { if ((r2 * 10 + a[k] - r1) & 1) return f[k][x][r1][r2] = true, false; ans[k] = (r2 * 10 + a[k] - r1) / 2; return true; } if (f[k][x][r1][r2]) return false; for (int i = 0; i < 10; i++) { if (k == 1 && i == 0) continue; int t1 = (a[k] - r1 - i) < 0, t2 = (a[k] - r1 - i + 10) % 10; if (r2 * 10 + a[n - k + 1] - i - t2 > 1 || r2 * 10 + a[n - k + 1] - i - t2 < 0) continue; ans[k] = i; ans[n - k + 1] = t2; if (dfs(k + 1, t1, r2 * 10 + a[n - k + 1] - i - t2, i)) return true; } f[k][x][r1][r2] = true; return false; } int main() { for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) a[++n] = c - '0'; for (int i = 1; i <= n / 2; i++) swap(a[i], a[n - i + 1]); if (dfs(1, 0, 0, 0)) { for (int i = 1; i <= n; i++) printf("%d", ans[i]); } else { if (a[n] != 1) puts("0"); else { n--; memset(f, false, sizeof(f)); if (dfs(1, 0, 1, 0)) { for (int i = 1; i <= n; i++) printf("%d", ans[i]); } else puts("0"); } } return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N]; char s[N]; char out[N]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; i++) a[i] = s[i] - '0'; int l = 1, r = n; if (a[l] != a[r]) { a[l]--; a[l + 1] += 10; if (!a[l]) l++; } while (l <= r) { if (a[l] != a[r]) { if (a[l] >= a[r] + 10 && a[r] < 10) { a[r - 1]--; a[r] += 10; } if (a[l] - 1 == a[r]) { a[l]--; a[l + 1] += 10; } } if (a[l] != a[r]) { return puts("0"), 0; } else { if (l != r) { a[r] /= 2; a[l] -= a[r]; } else { if (a[l] & 1) return puts("0"), 0; a[l] /= 2; } if (a[l] < 0 || a[l] > 9 || a[r] < 0 || a[r] > 9) return puts("0"), 0; } l++, r--; } l = 1; if (!a[l]) l++; for (int i = l; i <= n; i++) printf("%d", a[i]); }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; bool SR(int &x) { return scanf("%d", &x) == 1; } bool SR(long long &x) { return scanf("%lld", &x) == 1; } bool SR(double &x) { return scanf("%lf", &x) == 1; } bool SR(char *s) { return scanf("%s", s) == 1; } bool RI() { return true; } template <typename I, typename... T> bool RI(I &x, T &...tail) { return SR(x) && RI(tail...); } void SP(const int x) { printf("%d", x); } void SP(const long long x) { printf("%lld", x); } void SP(const double x) { printf("%.16lf", x); } void SP(const char *s) { printf("%s", s); } void PL() { puts(""); } template <typename I, typename... T> void PL(const I x, const T... tail) { SP(x); if (sizeof...(tail)) putchar(' '); PL(tail...); } const int maxn = 1e5 + 5; char s[maxn]; int n; void read() { RI(s); n = strlen(s); } int a[maxn]; void build() { for (int i = 0; i < int(n); i++) a[i] = s[i] - '0'; } bool ok(int l, int r) { ; if (a[l] < a[r]) return 0; if (l == r) return 0 <= a[l] && a[l] <= 18 && a[l] % 2 == 0; if (l + 1 == r) { if (a[l] < 0 || a[l] > 19) return 0; if ((a[l] - a[r]) % 10 == 0) return a[l] == a[r] && 0 <= a[l] && a[l] <= 18; if (((a[l] - a[r]) % 10 + 10) % 10 == 1) { a[l]--; a[r] += 10; return a[l] == a[r] && 0 <= a[l] && a[l] <= 18; } return 0; } if (a[l] < 0 || a[l] > 19) return 0; if ((a[l] - a[r]) % 10 == 0) { a[r - 1] -= (a[l] - a[r]) / 10; a[r] = a[l]; return 0 <= a[l] && a[l] <= 18 && ok(l + 1, r - 1); } if (((a[l] - a[r]) % 10 + 10) % 10 == 1) { a[l]--; a[l + 1] += 10; a[r - 1] -= (a[l] - a[r]) / 10; a[r] = a[l]; return 0 <= a[l] && a[l] <= 18 && ok(l + 1, r - 1); } return 0; } void sol() { if (ok(0, n - 1) && a[0] - a[0] / 2) { for (int i = 0; i < int(n / 2); i++) putchar(a[i] - a[i] / 2 + '0'); for (int i = (n / 2); i <= int(n - 1); i++) putchar(a[i] / 2 + '0'); PL(); return; } if (n == 1) { PL(0); return; } for (int i = 0; i < int(n); i++) a[i] = s[i] - '0'; a[1] += a[0] * 10; a[0] = 0; rotate(a, a + 1, a + n); n--; if (ok(0, n - 1) && a[0] - a[0] / 2) { for (int i = 0; i < int(n / 2); i++) putchar(a[i] - a[i] / 2 + '0'); for (int i = (n / 2); i <= int(n - 1); i++) putchar(a[i] / 2 + '0'); PL(); return; } PL(0); } int main() { read(); build(); sol(); return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; int num[102400], len; bool dfs() { int i = 0, j = len - 1; while (i < j) { if (num[i] == num[j]) { i++; j--; continue; } if (num[i] - num[j] == 10) { num[j - 1]--; num[j] += 10; } else if (num[i] - num[j] == 1 || num[i] - num[j] == 11) { num[i + 1] += 10; num[i]--; } else return false; } for (int i = 0; i < len; i++) { if (num[i] > 18 || num[i] < 0) return false; } if (len & 1 && num[len >> 1] & 1) return false; if (((num[0] + 1) >> 1) <= 0) return false; for (int i = 0; i < (len >> 1); i++) printf("%d", (num[i] + 1) >> 1); for (int i = len >> 1; i < len; i++) printf("%d", num[i] >> 1); return true; } int main() { string n; cin >> n; for (int i = 0; i < n.size(); i++) num[i] = n[i] - '0'; len = n.size(); if (dfs()) return 0; if (n[0] == '1' && n.size() != 1) { for (int i = 1; i < n.size(); i++) num[i - 1] = n[i] - '0'; len = n.size() - 1; num[0] += 10; if (dfs()) return 0; } printf("0\n"); return 0; }
CPP
625_D. Finals in arithmetic
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a. As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist. Input The first line of the input contains a single integer n (1 ≀ n ≀ 10100 000). Output If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. Examples Input 4 Output 2 Input 11 Output 10 Input 5 Output 0 Input 33 Output 21 Note In the first sample 4 = 2 + 2, a = 2 is the only possibility. In the second sample 11 = 10 + 1, a = 10 β€” the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes. It's easy to check that there is no suitable a in the third sample. In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
2
10
#include <bits/stdc++.h> using namespace std; char f[100500]; char res[100500]; bool go(int carry, int n, char* f) { int pl = 0, pr = n - 1; int prev_carry = 0; while (pl < pr) { int next_carry = 0; if (f[pl] == (f[pr] - prev_carry + 10) % 10) { if (carry) { res[pl] = 9; res[pr] = 10 + f[pl] - 9; if (res[pr] > 9) return false; } else { res[pl] = f[pl]; res[pr] = 0; } } else if ((f[pl] - 1 + 10) % 10 == (f[pr] - prev_carry + 10) % 10) { next_carry = 1; if (carry) { res[pl] = 9; res[pr] = f[pl]; if (f[pl] == 0) carry = 0; } else { res[pl] = f[pl] - 1; res[pr] = 0; if (res[pl] < 0) return false; if (pl == 0 && res[pl] == 0) return false; } } else return false; prev_carry = res[pl] + res[pr] + prev_carry >= 10; carry = next_carry; ++pl; --pr; } if (pl == pr) { bool ok = false; for (int i = (0); i < (10); ++i) { if ((i + i + prev_carry) % 10 == f[pl] && (i + i + prev_carry) / 10 == carry) { ok = true; res[pl] = i; } } if (!ok) return false; } else { --pl; ++pr; return carry == prev_carry; } return true; } int main() { scanf("%s", f); int n = strlen(f); for (int i = (0); i < (n); ++i) f[i] -= '0'; int len = n; if (!go(0, n, f)) { if (!(n > 1 && f[0] == 1 && go(1, n - 1, f + 1))) { puts("0"); return 0; } else len = n - 1; } for (int i = (0); i < (len); ++i) res[i] += '0'; res[len] = 0; puts(res); }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; int Q, N, M, K, L; long long FAN[100001]; int p[100001]; long long v[100001]; struct comp { bool operator()(const int& a, const int& b) const { if (v[a] != v[b]) return v[a] < v[b]; return a < b; } }; set<int, comp> children[100001]; int deg[100001]; multiset<long long> mulset; long long value_for_other(int a) { return FAN[a] / (2 + deg[a]); } long long value_for_self(int a) { return FAN[a] - (1 + deg[a]) * value_for_other(a); } int main() { ios::sync_with_stdio(false); cin >> N >> M; for (int i = 0; i < N; i++) cin >> FAN[i]; for (int i = 0; i < N; i++) { cin >> p[i]; p[i]--; children[p[i]].insert(i); deg[p[i]]++; } for (int i = 0; i < N; i++) { v[i] += value_for_self(i); for (int child : children[i]) v[i] += value_for_other(child); } for (int i = 0; i < N; i++) { children[p[i]].erase(i); children[p[i]].insert(i); } for (int n = 0; n < N; n++) { long long pv = value_for_other(n); if (!deg[n]) continue; mulset.insert(v[*children[n].begin()] + pv); mulset.insert(v[*children[n].rbegin()] + pv); } for (int q = 0; q < M; q++) { int a, b, c; cin >> a; if (a == 1) { cin >> b >> c; b--; c--; set<int> go = {b, c, p[b], p[c], p[p[b]]}; set<int> go2 = go; for (int n : go) go2.insert(p[n]); for (int n : go2) { if (!deg[n]) continue; long long pv = value_for_other(n); mulset.erase(mulset.find(v[*children[n].begin()] + pv)); mulset.erase(mulset.find(v[*children[n].rbegin()] + pv)); } for (int n : go) children[p[n]].erase(n); for (int n : go) { v[n] -= value_for_self(n); for (int m : go) if (p[m] == n) v[n] -= value_for_other(m); } deg[p[b]]--; p[b] = c; deg[p[b]]++; for (int n : go) { v[n] += value_for_self(n); for (int m : go) if (p[m] == n) v[n] += value_for_other(m); } for (int n : go) children[p[n]].insert(n); for (int n : go2) { if (!deg[n]) continue; long long pv = value_for_other(n); mulset.insert(v[*children[n].begin()] + pv); mulset.insert(v[*children[n].rbegin()] + pv); } } if (a == 2) { cin >> b; b--; cout << v[b] + value_for_other(p[b]) << "\n"; } if (a == 3) { cout << *mulset.begin() << " " << *mulset.rbegin() << "\n"; } } cin >> N; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using D = double; using uint = unsigned int; const int maxn = 100005; ll t[maxn], others[maxn], me[maxn]; int f[maxn]; vector<int> from[maxn]; int kv[maxn]; ll ans[maxn]; int n, m; multiset<ll> global; multiset<ll> cur[maxn]; bool in[maxn], in2[maxn]; void pop(int x) { if (!in[x]) return; in[x] = false; if (cur[x].size() > 0) { global.erase(global.find(others[x] + *cur[x].begin())); global.erase(global.find(others[x] + *cur[x].rbegin())); } } void push(int x) { if (in[x]) return; in[x] = true; if (cur[x].size() > 0) { global.insert(others[x] + *cur[x].begin()); global.insert(others[x] + *cur[x].rbegin()); } } void delfrom(int a, int b) { if (!in2[a]) return; in2[a] = false; cur[b].erase(cur[b].find(ans[a])); } void addto(int a, int b) { if (in2[a]) return; in2[a] = true; cur[b].insert(ans[a]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%lld", &t[i]); for (int i = 0; i < n; i++) { scanf("%d", &f[i]); f[i]--; kv[i]++; kv[f[i]]++; from[f[i]].push_back(i); } for (int i = 0; i < n; i++) { others[i] = t[i] / (kv[i] + 1); me[i] = t[i] - (kv[i] * others[i]); } for (int i = 0; i < n; i++) { ans[i] = me[i]; for (auto z : from[i]) ans[i] += others[z]; } for (int i = 0; i < n; i++) { for (auto z : from[i]) { cur[i].insert(ans[z]); } if (cur[i].size() > 0) { global.insert(others[i] + *cur[i].begin()); global.insert(others[i] + *cur[i].rbegin()); } in[i] = true; in2[i] = true; } for (int i = 0; i < m; i++) { int t; scanf("%d", &t); if (t == 1) { int a, b; scanf("%d%d", &a, &b); a--, b--; int was = f[a]; pop(a); pop(was); pop(f[was]); pop(f[f[was]]); pop(b); pop(f[b]); pop(f[f[b]]); delfrom(a, was); delfrom(was, f[was]); delfrom(f[was], f[f[was]]); delfrom(b, f[b]); delfrom(f[b], f[f[b]]); ans[was] -= others[a]; ans[f[was]] -= others[was]; kv[was]--; others[was] = ::t[was] / (kv[was] + 1); ans[was] -= me[was]; me[was] = ::t[was] - (kv[was] * others[was]); ans[was] += me[was]; ans[f[was]] += others[was]; f[a] = b; ans[b] += others[a]; ans[f[b]] -= others[b]; kv[b]++; others[b] = ::t[b] / (kv[b] + 1); ans[b] -= me[b]; me[b] = ::t[b] - (kv[b] * others[b]); ans[b] += me[b]; ans[f[b]] += others[b]; addto(a, b); addto(was, f[was]); addto(f[was], f[f[was]]); addto(b, f[b]); addto(f[b], f[f[b]]); push(a); push(was); push(f[was]); push(f[f[was]]); push(b); push(f[b]); push(f[f[b]]); } else if (t == 2) { int x; scanf("%d", &x); x--; printf("%lld\n", ans[x] + others[f[x]]); } else { printf("%lld %lld\n", *global.begin(), *global.rbegin()); } } return 0; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; inline int ReadInt() { static int n, ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar(); return n; } inline long long ReadLL() { static long long n; static int ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar(); return n; } const int maxn = 100000 + 3; const long long INF = 1LL << 60; multiset<long long> opt_set, sets[maxn]; int n, q, fa[maxn], s[maxn]; long long t[maxn], val[maxn]; inline long long cal(int u) { return t[u] / (s[u] + 2); } bool in_opt[maxn], in_val[maxn]; inline void erase_val(int a) { int b = fa[a]; if (in_val[a]) { sets[b].erase(sets[b].find(val[a])); in_val[a] = false; } } inline void insert_val(int a) { int b = fa[a]; if (!in_val[a]) { sets[b].insert(val[a]); in_val[a] = true; } } void erase_opt(int a) { if (in_opt[a]) { long long v = cal(a); opt_set.erase(opt_set.find(*sets[a].begin() + v)); opt_set.erase(opt_set.find(*sets[a].rbegin() + v)); in_opt[a] = false; } } void insert_opt(int a) { if (!in_opt[a] && sets[a].size()) { long long v = cal(a); opt_set.insert(*sets[a].begin() + v); opt_set.insert(*sets[a].rbegin() + v); in_opt[a] = true; } } void modify(int a, int c) { int b = fa[a]; erase_opt(b), erase_opt(c), erase_opt(fa[b]), erase_opt(fa[c]), erase_opt(fa[fa[b]]), erase_opt(fa[fa[c]]); erase_val(a), erase_val(b), erase_val(c), erase_val(fa[b]), erase_val(fa[c]); fa[a] = c; long long va_other = cal(a); long long vb_other = cal(b); val[b] -= va_other; val[b] -= t[b] - (s[b] + 1) * vb_other; val[fa[b]] -= vb_other; s[b]--; vb_other = t[b] / (s[b] + 2); val[b] += t[b] - (s[b] + 1) * vb_other; val[fa[b]] += vb_other; long long vc_other = cal(c); val[c] += va_other; val[c] -= t[c] - (s[c] + 1) * vc_other; val[fa[c]] -= vc_other; s[c]++; vc_other = t[c] / (s[c] + 2); val[c] += t[c] - (s[c] + 1) * vc_other; val[fa[c]] += vc_other; insert_val(a), insert_val(b), insert_val(c), insert_val(fa[b]), insert_val(fa[c]); insert_opt(b), insert_opt(c), insert_opt(fa[b]), insert_opt(fa[c]), insert_opt(fa[fa[b]]), insert_opt(fa[fa[c]]); } int main() { n = ReadInt(), q = ReadInt(); for (int i = 0; i < n; ++i) t[i] = ReadLL(); for (int i = 0; i < n; ++i) s[fa[i] = ReadInt() - 1]++; for (int i = 0; i < n; ++i) { long long v = cal(i); val[fa[i]] += v, val[i] += t[i] - (s[i] + 1) * v; } memset(in_opt, 0, sizeof(bool) * n); memset(in_val, 0, sizeof(bool) * n); for (int i = 0; i < n; ++i) insert_val(i); for (int i = 0; i < n; ++i) insert_opt(i); int cnt = 0; while (q--) { int type = ReadInt(); if (type == 1) { int u = ReadInt() - 1, v = ReadInt() - 1; modify(u, v); cnt++; } else if (type == 2) { int u = ReadInt() - 1; printf("%I64d\n", val[u] + cal(fa[u])); } else if (type == 3) { printf("%I64d %I64d\n", *opt_set.begin(), *opt_set.rbegin()); } else assert(false); } return 0; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; inline int ReadInt() { static int n, ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar(); return n; } inline long long ReadLL() { static long long n; static int ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar(); return n; } const int maxn = 100000 + 3; const long long INF = 1LL << 60; multiset<long long> opt_set, sets[maxn]; int n, q, fa[maxn], s[maxn]; long long t[maxn], val[maxn]; inline long long cal(int u) { return t[u] / (s[u] + 2); } bool in_opt[maxn], in_val[maxn]; inline void erase_val(int a) { int b = fa[a]; if (in_val[a]) { sets[b].erase(sets[b].find(val[a])); in_val[a] = false; } } inline void insert_val(int a) { int b = fa[a]; if (!in_val[a]) { sets[b].insert(val[a]); in_val[a] = true; } } inline void erase_opt(int a) { if (in_opt[a]) { long long v = cal(a); opt_set.erase(opt_set.find(*sets[a].begin() + v)); opt_set.erase(opt_set.find(*sets[a].rbegin() + v)); in_opt[a] = false; } } inline void insert_opt(int a) { if (!in_opt[a] && sets[a].size()) { long long v = cal(a); opt_set.insert(*sets[a].begin() + v); opt_set.insert(*sets[a].rbegin() + v); in_opt[a] = true; } } void modify(int a, int c) { int b = fa[a]; erase_opt(b), erase_opt(c), erase_opt(fa[b]), erase_opt(fa[c]), erase_opt(fa[fa[b]]), erase_opt(fa[fa[c]]); erase_val(a), erase_val(b), erase_val(c), erase_val(fa[b]), erase_val(fa[c]); fa[a] = c; long long va_other = cal(a); long long vb_other = cal(b); val[b] -= va_other; val[b] -= t[b] - (s[b] + 1) * vb_other; val[fa[b]] -= vb_other; s[b]--; vb_other = t[b] / (s[b] + 2); val[b] += t[b] - (s[b] + 1) * vb_other; val[fa[b]] += vb_other; long long vc_other = cal(c); val[c] += va_other; val[c] -= t[c] - (s[c] + 1) * vc_other; val[fa[c]] -= vc_other; s[c]++; vc_other = t[c] / (s[c] + 2); val[c] += t[c] - (s[c] + 1) * vc_other; val[fa[c]] += vc_other; insert_val(a), insert_val(b), insert_val(c), insert_val(fa[b]), insert_val(fa[c]); insert_opt(b), insert_opt(c), insert_opt(fa[b]), insert_opt(fa[c]), insert_opt(fa[fa[b]]), insert_opt(fa[fa[c]]); } int main() { n = ReadInt(), q = ReadInt(); for (int i = 0; i < n; ++i) t[i] = ReadLL(); for (int i = 0; i < n; ++i) s[fa[i] = ReadInt() - 1]++; for (int i = 0; i < n; ++i) { long long v = cal(i); val[fa[i]] += v, val[i] += t[i] - (s[i] + 1) * v; } memset(in_opt, 0, sizeof(bool) * n); memset(in_val, 0, sizeof(bool) * n); for (int i = 0; i < n; ++i) insert_val(i); for (int i = 0; i < n; ++i) insert_opt(i); while (q--) { int type = ReadInt(); if (type == 1) { int u = ReadInt() - 1, v = ReadInt() - 1; modify(u, v); } else if (type == 2) { int u = ReadInt() - 1; printf("%I64d\n", val[u] + cal(fa[u])); } else if (type == 3) { printf("%I64d %I64d\n", *opt_set.begin(), *opt_set.rbegin()); } else assert(false); } return 0; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; char ch = getchar(); bool f = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return f ? -x : x; } void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } void writeln(long long x) { write(x); puts(""); } const int N = 300005; int n, m, d[N], f[N]; long long b[N], e[N], l[N], r[N]; multiset<long long> S, s[N]; multiset<long long>::iterator it; void calc(int x) { e[x] = b[x] / (d[x] + 2); l[x] = b[x] - (d[x] + 1) * e[x]; } void add(int x) { s[f[x]].insert(l[x] + r[x]); } void addall(int x) { if (!s[x].size()) return; it = s[x].begin(); S.insert(*it + e[x]); it = s[x].end(); --it; S.insert(*it + e[x]); } void del(int x) { s[f[x]].erase(s[f[x]].find(l[x] + r[x])); } void delall(int x) { if (!s[x].size()) return; it = s[x].begin(); S.erase(S.find(*it + e[x])); it = s[x].end(); --it; S.erase(S.find(*it + e[x])); } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) b[i] = read(); for (int i = 1; i <= n; i++) d[f[i] = read()]++; for (int i = 1; i <= n; i++) calc(i), r[f[i]] += e[i]; for (int i = 1; i <= n; i++) add(i); for (int i = 1; i <= n; i++) addall(i); for (int i = 1; i <= m; i++) { int op = read(); if (op == 1) { int x = read(), y = read(); if (f[x] == y) continue; delall(f[x]); delall(f[f[x]]); delall(f[f[f[x]]]); del(x); del(f[x]); del(f[f[x]]); r[f[x]] -= e[x]; d[f[x]]--; r[f[f[x]]] -= e[f[x]]; calc(f[x]); r[f[f[x]]] += e[f[x]]; add(f[x]); add(f[f[x]]); addall(f[x]); addall(f[f[x]]); addall(f[f[f[x]]]); f[x] = y; delall(f[x]); delall(f[f[x]]); delall(f[f[f[x]]]); del(f[x]); del(f[f[x]]); r[f[x]] += e[x]; d[f[x]]++; r[f[f[x]]] -= e[f[x]]; calc(f[x]); r[f[f[x]]] += e[f[x]]; add(x); add(f[x]); add(f[f[x]]); addall(f[x]); addall(f[f[x]]); addall(f[f[f[x]]]); } else if (op == 2) { int x = read(); printf("%lld\n", l[x] + r[x] + e[f[x]]); } else { it = S.begin(); printf("%lld ", *it); it = S.end(); --it; printf("%lld\n", *it); } } return 0; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 100005; int n, q, k[MAXN], f[MAXN]; long long t[MAXN], sum[MAXN]; multiset<long long> A[MAXN], all; void modifyAll(int x, int del) { if (A[x].empty()) return; if (!del) { all.erase(all.lower_bound(t[x] / (k[x] + 2) + *A[x].begin())), all.erase(all.lower_bound(t[x] / (k[x] + 2) + *--A[x].end())); } else { all.insert(t[x] / (k[x] + 2) + *A[x].begin()), all.insert(t[x] / (k[x] + 2) + *--A[x].end()); } } void modifyA(int x, int del) { if (!del) A[f[x]].erase(A[f[x]].lower_bound(sum[x])); else A[f[x]].insert(sum[x]); } int main() { scanf("%d%d", &n, &q); for (int i = (1); i < (n + 1); i++) scanf("%lld", &t[i]); for (int i = (1); i < (n + 1); i++) scanf("%d", &f[i]), k[f[i]]++; for (int i = (1); i < (n + 1); i++) sum[f[i]] += t[i] / (k[i] + 2), sum[i] += t[i] - t[i] / (k[i] + 2) * (k[i] + 1); for (int i = (1); i < (n + 1); i++) A[f[i]].insert(sum[i]); for (int i = (1); i < (n + 1); i++) modifyAll(i, 1); for (int i = (1); i < (q + 1); i++) { int op; scanf("%d", &op); if (op == 1) { int x, y; scanf("%d%d", &x, &y); set<int> vis; int p[] = {x, f[x], f[f[x]], y, f[y], f[f[f[x]]], f[f[y]]}; for (int i = (0); i < (7); i++) { if (!vis.count(p[i])) { modifyAll(p[i], 0); vis.insert(p[i]); } } vis.clear(); for (int i = (0); i < (7); i++) { if (!vis.count(p[i])) { modifyA(p[i], 0); vis.insert(p[i]); } } int v = f[x]; sum[v] -= t[x] / (k[x] + 2); sum[v] -= t[v] - t[v] / (k[v] + 2) * (k[v] + 1); sum[v] += t[v] - t[v] / (k[v] + 1) * (k[v]); sum[y] += t[x] / (k[x] + 2); sum[y] -= t[y] - t[y] / (k[y] + 2) * (k[y] + 1); sum[y] += t[y] - t[y] / (k[y] + 3) * (k[y] + 2); sum[f[v]] -= t[v] / (k[v] + 2); sum[f[v]] += t[v] / (k[v] + 1); sum[f[y]] -= t[y] / (k[y] + 2); sum[f[y]] += t[y] / (k[y] + 3); k[v]--, k[y]++; f[x] = y; vis.clear(); for (int i = (0); i < (7); i++) { if (!vis.count(p[i])) { modifyA(p[i], 1); vis.insert(p[i]); } } vis.clear(); for (int i = (0); i < (7); i++) { if (!vis.count(p[i])) { modifyAll(p[i], 1); vis.insert(p[i]); } } } else if (op == 2) { int i; scanf("%d", &i); printf("%lld\n", sum[i] + t[f[i]] / (k[f[i]] + 2)); } else { printf("%lld %lld\n", *all.begin(), *--all.end()); } } }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; const long long inf = 4e18; const long long maxn = 2e5 + 10; const long long mod = 1e9 + 7; long long a[maxn], f[maxn], val[maxn], dg[maxn]; set<pair<long long, long long> > s[maxn]; set<pair<long long, long long> > tot; inline void Add(long long x) { if (s[x].empty()) return; long long delta = a[x] / dg[x]; pair<long long, long long> p1 = *s[x].begin(), p2 = *s[x].rbegin(); tot.insert(make_pair(p1.first + delta, p1.second)); tot.insert(make_pair(p2.first + delta, p2.second)); } inline void Erase(long long x) { if (s[x].empty()) return; long long delta = a[x] / dg[x]; pair<long long, long long> p1 = *s[x].begin(), p2 = *s[x].rbegin(); tot.erase(make_pair(p1.first + delta, p1.second)); tot.erase(make_pair(p2.first + delta, p2.second)); } inline void Modify(long long x, long long y) { s[f[x]].erase(make_pair(val[x], x)); val[x] = y; s[f[x]].insert(make_pair(val[x], x)); } inline long long calc(long long x, long long d) { return x - ((d - 1) * (x / d)); } int main() { long long n, q; cin >> n >> q; for (int i = 0; i < int(n); i++) { cin >> a[i]; dg[i] = 1; } for (int i = 0; i < int(n); i++) { cin >> f[i]; f[i]--; dg[i]++; dg[f[i]]++; } for (int i = 0; i < int(n); i++) { val[i] += calc(a[i], dg[i]); val[f[i]] += a[i] / dg[i]; } for (int i = 0; i < int(n); i++) { s[f[i]].insert(make_pair(val[i], i)); } for (int i = 0; i < int(n); i++) { if (s[i].empty()) continue; long long delta = a[i] / dg[i]; pair<long long, long long> p1 = *s[i].begin(), p2 = *s[i].rbegin(); tot.insert(make_pair(p1.first + delta, p1.second)); tot.insert(make_pair(p2.first + delta, p2.second)); } while (q--) { long long task, x, y; cin >> task; if (task == 1) { cin >> x >> y; x--; y--; Erase(x); Erase(y); Erase(f[y]); Erase(f[x]); Erase(f[f[x]]); Erase(f[f[y]]); Erase(f[f[f[x]]]); Modify(y, val[y] + (a[x] / dg[x]) + calc(a[y], dg[y] + 1) - calc(a[y], dg[y])); Modify(f[y], val[f[y]] + (a[y] / (dg[y] + 1)) - (a[y] / dg[y])); Modify(f[x], val[f[x]] - (a[x] / (dg[x])) + calc(a[f[x]], dg[f[x]] - 1) - calc(a[f[x]], dg[f[x]])); Modify(f[f[x]], val[f[f[x]]] + (a[f[x]] / (dg[f[x]] - 1)) - (a[f[x]] / dg[f[x]])); dg[f[x]]--; dg[y]++; s[f[x]].erase(make_pair(val[x], x)); s[y].insert(make_pair(val[x], x)); Add(x); Add(y); Add(f[y]); Add(f[x]); Add(f[f[x]]); Add(f[f[y]]); Add(f[f[f[x]]]); f[x] = y; } if (task == 2) { cin >> x; x--; cout << val[x] + (a[f[x]] / dg[f[x]]) << endl; } if (task == 3) { cout << (tot.begin()->first) << " " << (tot.rbegin()->first) << endl; } } }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; inline int ReadInt() { static int n, ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar(); return n; } inline long long ReadLL() { static long long n; static int ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar(); return n; } const int maxn = 100000 + 3; const long long INF = 1LL << 60; multiset<long long> opt_set, sets[maxn]; int n, q, fa[maxn], s[maxn]; long long t[maxn], val[maxn]; inline long long cal(int u) { return t[u] / (s[u] + 2); } bool in_opt[maxn], in_val[maxn]; inline void erase_val(int a) { int b = fa[a]; if (in_val[a]) { sets[b].erase(sets[b].find(val[a])); in_val[a] = false; } } inline void insert_val(int a) { int b = fa[a]; if (!in_val[a]) { sets[b].insert(val[a]); in_val[a] = true; } } void erase_opt(int a) { if (in_opt[a]) { long long v = cal(a); opt_set.erase(opt_set.find(*sets[a].begin() + v)); opt_set.erase(opt_set.find(*sets[a].rbegin() + v)); in_opt[a] = false; } } void insert_opt(int a) { if (!in_opt[a] && sets[a].size()) { long long v = cal(a); opt_set.insert(*sets[a].begin() + v); opt_set.insert(*sets[a].rbegin() + v); in_opt[a] = true; } } void modify(int a, int c) { int b = fa[a]; erase_opt(b), erase_opt(c), erase_opt(fa[b]), erase_opt(fa[c]), erase_opt(fa[fa[b]]), erase_opt(fa[fa[c]]); erase_val(a), erase_val(b), erase_val(c), erase_val(fa[b]), erase_val(fa[c]); fa[a] = c; long long va_other = cal(a); long long vb_other = cal(b); val[b] -= va_other; val[b] -= t[b] - (s[b] + 1) * vb_other; val[fa[b]] -= vb_other; s[b]--; vb_other = t[b] / (s[b] + 2); val[b] += t[b] - (s[b] + 1) * vb_other; val[fa[b]] += vb_other; long long vc_other = cal(c); val[c] += va_other; val[c] -= t[c] - (s[c] + 1) * vc_other; val[fa[c]] -= vc_other; s[c]++; vc_other = t[c] / (s[c] + 2); val[c] += t[c] - (s[c] + 1) * vc_other; val[fa[c]] += vc_other; insert_val(a), insert_val(b), insert_val(c), insert_val(fa[b]), insert_val(fa[c]); insert_opt(b), insert_opt(c), insert_opt(fa[b]), insert_opt(fa[c]), insert_opt(fa[fa[b]]), insert_opt(fa[fa[c]]); } int main() { n = ReadInt(), q = ReadInt(); for (int i = 0; i < n; ++i) t[i] = ReadLL(); for (int i = 0; i < n; ++i) s[fa[i] = ReadInt() - 1]++; for (int i = 0; i < n; ++i) { long long v = cal(i); val[fa[i]] += v, val[i] += t[i] - (s[i] + 1) * v; } memset(in_opt, 0, sizeof(bool) * n); memset(in_val, 0, sizeof(bool) * n); for (int i = 0; i < n; ++i) insert_val(i); for (int i = 0; i < n; ++i) insert_opt(i); int cnt = 0; while (q--) { int type = ReadInt(); if (type == 1) { int u = ReadInt() - 1, v = ReadInt() - 1; modify(u, v); cnt++; } else if (type == 2) { int u = ReadInt() - 1; cout << val[u] + cal(fa[u]) << endl; } else if (type == 3) { cout << *opt_set.begin() << ' ' << *opt_set.rbegin() << endl; } else assert(false); } return 0; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; char ch = getchar(); bool f = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return f ? -x : x; } void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } void writeln(long long x) { write(x); puts(""); } const int N = 300005; int n, m, d[N], f[N]; long long b[N], e[N], l[N], r[N]; multiset<long long> S, s[N]; multiset<long long>::iterator it; void calc(int x) { e[x] = b[x] / (d[x] + 2); l[x] = b[x] - (d[x] + 1) * e[x]; } void add(int x) { s[f[x]].insert(l[x] + r[x]); } void addall(int x) { if (!s[x].size()) return; it = s[x].begin(); S.insert(*it + e[x]); it = s[x].end(); --it; S.insert(*it + e[x]); } void del(int x) { s[f[x]].erase(s[f[x]].find(l[x] + r[x])); } void delall(int x) { if (!s[x].size()) return; it = s[x].begin(); S.erase(S.find(*it + e[x])); it = s[x].end(); --it; S.erase(S.find(*it + e[x])); } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) b[i] = read(); for (int i = 1; i <= n; i++) d[f[i] = read()]++; for (int i = 1; i <= n; i++) calc(i), r[f[i]] += e[i]; for (int i = 1; i <= n; i++) add(i); for (int i = 1; i <= n; i++) addall(i); for (int i = 1; i <= m; i++) { int op = read(); if (op == 1) { int x = read(), y = read(); if (f[x] == y) continue; delall(f[x]); delall(f[f[x]]); delall(f[f[f[x]]]); del(x); del(f[x]); del(f[f[x]]); r[f[x]] -= e[x]; d[f[x]]--; r[f[f[x]]] -= e[f[x]]; calc(f[x]); r[f[f[x]]] += e[f[x]]; add(f[x]); add(f[f[x]]); addall(f[x]); addall(f[f[x]]); addall(f[f[f[x]]]); f[x] = y; delall(f[x]); delall(f[f[x]]); delall(f[f[f[x]]]); del(f[x]); del(f[f[x]]); r[f[x]] += e[x]; d[f[x]]++; r[f[f[x]]] -= e[f[x]]; calc(f[x]); r[f[f[x]]] += e[f[x]]; add(x); add(f[x]); add(f[f[x]]); addall(f[x]); addall(f[f[x]]); addall(f[f[f[x]]]); } else if (op == 2) { int x = read(); printf("%lld\n", l[x] + r[x] + e[f[x]]); } else { it = S.begin(); printf("%lld ", *it); it = S.end(); --it; printf("%lld\n", *it); } } return 0; }
CPP
673_F. Bearish Fanpages
There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j β€” fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i β€” print the total income of the i-th company. * 3 β€” print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≀ n ≀ 100 000, 1 ≀ q ≀ 100 000) β€” the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≀ fi ≀ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≀ typei ≀ 3) β€” the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has iΒ·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now.
2
12
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; char ch = getchar(); bool f = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return f ? -x : x; } void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } void writeln(long long x) { write(x); puts(""); } const int N = 300005; int n, m, d[N], f[N]; long long b[N], e[N], l[N], r[N]; multiset<long long> S, s[N]; multiset<long long>::iterator it; void calc(int x) { e[x] = b[x] / (d[x] + 2); l[x] = b[x] - (d[x] + 1) * e[x]; } void add(int x) { s[f[x]].insert(l[x] + r[x]); } void addall(int x) { if (!s[x].size()) return; it = s[x].begin(); S.insert(*it + e[x]); it = s[x].end(); --it; S.insert(*it + e[x]); } void del(int x) { s[f[x]].erase(s[f[x]].find(l[x] + r[x])); } void delall(int x) { if (!s[x].size()) return; it = s[x].begin(); S.erase(S.find(*it + e[x])); it = s[x].end(); --it; S.erase(S.find(*it + e[x])); } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) b[i] = read(); for (int i = 1; i <= n; i++) d[f[i] = read()]++; for (int i = 1; i <= n; i++) calc(i), r[f[i]] += e[i]; for (int i = 1; i <= n; i++) add(i); for (int i = 1; i <= n; i++) addall(i); for (int i = 1; i <= m; i++) { int op = read(); if (op == 1) { int x = read(), y = read(); if (f[x] == y) continue; delall(f[x]); delall(f[f[x]]); delall(f[f[f[x]]]); del(x); del(f[x]); del(f[f[x]]); r[f[x]] -= e[x]; d[f[x]]--; r[f[f[x]]] -= e[f[x]]; calc(f[x]); r[f[f[x]]] += e[f[x]]; add(f[x]); add(f[f[x]]); addall(f[x]); addall(f[f[x]]); addall(f[f[f[x]]]); f[x] = y; delall(f[x]); delall(f[f[x]]); delall(f[f[f[x]]]); del(f[x]); del(f[f[x]]); r[f[x]] += e[x]; d[f[x]]++; r[f[f[x]]] -= e[f[x]]; calc(f[x]); r[f[f[x]]] += e[f[x]]; add(x); add(f[x]); add(f[f[x]]); addall(f[x]); addall(f[f[x]]); addall(f[f[f[x]]]); } else if (op == 2) { int x = read(); printf("%lld\n", l[x] + r[x] + e[f[x]]); } else { it = S.begin(); printf("%lld ", *it); it = S.end(); --it; printf("%lld\n", *it); } } return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import sys def main(arr): curr = None m = float("inf") for d, pos in arr: if d == 'L': if curr is not None: m = min(m, pos - curr) elif d == 'R': curr = pos print(m // 2 if m != float("inf") else -1) if __name__ == "__main__": arr = [] for e, line in enumerate(sys.stdin.readlines()): if e == 0: continue if e == 1: arr = list(c for c in line.strip()) else: curr = list(map(int, line.strip().split())) arr = list(zip(arr, curr)) main(arr)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) x=input() a=list(map(int,input().split()));m=[] if 'RL' not in x:print(-1) else: for i in range(n-1): if x[i]+x[i+1]=='RL':m.append((a[i+1]-a[i])//2) print(min(m))
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, arr[200004], m = INT_MAX; char a[200004]; cin >> n; cin >> a; for (i = 0; i < n; i++) cin >> arr[i]; int flag = 0; if (n == 1) cout << "-1\n"; else { i = 0; while (i < n) { while (a[i] == 'L' && i < n) i++; while (a[i] == 'R' && i < n) i++; if (i == n) { break; } else { flag = 1; j = arr[i] - arr[i - 1]; j = j / 2; m = min(j, m); } } if (flag == 1) cout << m; else cout << "-1"; } }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.*; import java.util.*; /** * @author Alexander Tsupko ([email protected]) * (c) Codeforces Round 363. All rights reserved. July 19, 2016. */ public class A { private static class Solve { // instance variables private void solve(int testNumber, InputReader in, OutputWriter out) { long answer = Long.MAX_VALUE; boolean flag = false; int n = in.nextInt(); char[] chars = in.next().toCharArray(); long[] pos = new long[n]; for (int i = 0; i < n; i++) { pos[i] = in.nextLong(); } for (int i = 1; i < n; i++) { if (chars[i - 1] == 'R' && chars[i] == 'L') { flag = true; long current = Math.abs(pos[i - 1] - pos[i]); if (current < answer) { answer = current; } } } out.println(flag ? answer / 2 : -1); } // instance methods } private static class InputReader { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; private InputReader(InputStream inputStream) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 32768); } private void close() throws IOException { bufferedReader.close(); } private String next() { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return stringTokenizer.nextToken(); } private byte nextByte() { return Byte.parseByte(next()); } private short nextShort() { return Short.parseShort(next()); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private float nextFloat() { return Float.parseFloat(next()); } private double nextDouble() { return Double.parseDouble(next()); } private boolean nextBoolean() { return Boolean.parseBoolean(next()); } private char firstChar() { return next().charAt(0); } } private static class OutputWriter { private final PrintWriter printWriter; private OutputWriter(OutputStream outputStream) { printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } private void close() { printWriter.flush(); printWriter.close(); } private void print(Object... objects) { int n = objects.length; for (int i = 0; i < n; i++) { if (i != 0) { printWriter.print(' '); } printWriter.print(objects[i]); } } private void println(Object... objects) { print(objects); printWriter.println(); } } public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Solve solve = new Solve(); // long start = System.nanoTime(); solve.solve(1, in, out); // int testCount = Integer.parseInt(in.next()); // for (int i = 1; i <= testCount; i++) { // solve.solve(i, in, out); // } // in.printTime(Locale.US, "%.1f ms\n", (System.nanoTime()-start)/1e6); // System.out.printf(Locale.US, "%.1f ms\n", (System.nanoTime()-start)/1e6); in.close(); out.close(); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import sys n = int(input()) s = input() k = list(map( int, input().split())) if n == 1: print(-1) sys.exit() ans = [] for i in range(n - 1): if s[i] == 'R' and s[i + 1] == 'L': f = (k[i + 1] - k[i]) // 2 ans.append(f) if ans: print(min(ans)) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { string str; int number, Min = 1000000000, f, s; cin >> number >> str >> f; for (int c = 1; c < number; c++) { cin >> s; if (str[c] == 'L' && str[c - 1] == 'R') Min = min(Min, (s - f) / 2); f = s; } cout << (Min < 1000000000 ? Min : -1); }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.Scanner; public class Ques1 { public static void main(String[] args) { Scanner inp=new Scanner(System.in); int n=inp.nextInt(); inp.nextLine(); String in=inp.nextLine(); int []arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=inp.nextInt(); } int min=-1; for(int i=0;i<n-1;i++) { if(in.charAt(i)=='R' && in.charAt(i+1)=='L') { int temp=(arr[i+1]-arr[i])/2; if(min==-1 || temp<min) min=temp; } } System.out.println(min); inp.close(); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string a; cin >> a; int Arr[N]; for (int i = 0; i < N; i++) { cin >> Arr[i]; } int R = 10000000009; bool done = false; for (int i = 0; i < N; i++) { if (a[i] == 'R' && a[i + 1] == 'L') { R = min(R, (Arr[i + 1] - Arr[i]) / 2); done = true; } } if (done) { cout << R; } else { cout << -1; } return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
def main(): n = int(input()) d = input() x = list(map(int, input().split())) t = -1 for i in range(len(x) - 1): if d[i] == 'R' and d[i+1] == 'L': time = (x[i] + x[i+1]) // 2 - x[i] t = min(1e10 if t < 0 else t, time) print(t) if __name__ == '__main__': main()
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
N=int(input()) a=input() b=input() b=b.split() x=0-1 for i in range(N-1): if a[i]=='R' and a[i+1]=='L': m=(int(b[i+1])-int(b[i]))//2 if x == -1: x=m else: x = min(x, m) print(x)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int INF = 1e9 + 7; struct node { int pos; char c; } p[N]; int main() { int n; scanf("%d", &n); getchar(); for (int i = 0; i < n; i++) scanf("%c", &p[i].c); for (int i = 0; i < n; i++) scanf("%d", &p[i].pos); int minm = INF; for (int i = 0; i < n - 1; i++) if (p[i].c == 'R' && p[i + 1].c == 'L' && p[i + 1].pos - p[i].pos < minm) minm = p[i + 1].pos - p[i].pos; if (minm == INF) puts("-1"); else printf("%d\n", minm / 2); return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
# Description of the problem can be found at http://codeforces.com/problemset/problem/699/A n = int(input()) d = input() l_d = list(map(int, input().split())) p_R = int(1e10) p_L = int(1e10) m_t = int(1e10) for i in range(n): if d[i] == "R": p_R = i if d[i] == "L": p_L = i if p_R != 1e10 and p_L != 1e10: if p_L > p_R: m_t = min(m_t, (l_d[p_L] - l_d[p_R]) // 2) print(-1 if m_t == 1e10 else m_t)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
l=lambda:map(int,raw_input().split()) n=input() s=raw_input() a=l() d=[] i=0 while i<n: if s[i:i+2]=='RL': d.append(a[i+1]-a[i]) i+=1 if d: print min(d)/2 else: print -1
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class LaunchOfCollider { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); char s[] = bf.readLine().toCharArray(); int a[] = new int[n]; StringTokenizer st = new StringTokenizer(bf.readLine()); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); int min = 1000000000; boolean f = false; for (int i = 1; i < n; i++) if (s[i] == 'L' && s[i - 1] == 'R') { f = true; min = Math.min(min, (a[i] - a[i - 1]) >> 1); } if (f) System.out.println(min); else System.out.println(-1); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; char s[200005]; int a[200005]; int main() { int n; int minn = INT_MAX; scanf("%d", &n); getchar(); for (int i = 0; i < n; i++) { scanf("%c", &s[i]); } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { if (s[i] == 'R' && s[i + 1] == 'L') minn = minn < a[i + 1] - a[i] ? minn : a[i + 1] - a[i]; } if (minn == INT_MAX) printf("-1\n"); else printf("%d\n", minn / 2); return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) i = input() x = input().split(" ") cr = [] for a in range(len(x)): x[a] = int(x[a]) a = 0 while(a < n-1): if(i[a] == "R"): if(i[a+1] == "L"): cr.append((x[a+1]-x[a])//2) a += 2 else: a += 1 else: a += 1 if(len(cr) > 0): print(min(cr)) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a[n], i, j, k, cnt = 1e9, tmp = 0; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < s.size() - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'L') { tmp = (a[i + 1] - a[i]) / 2; if (tmp < cnt) cnt = tmp; } } if (cnt == 1e9) cout << -1 << endl; else cout << cnt << endl; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; char s[200000 + 5]; int a[200000 + 5]; int main() { int n; while (~scanf("%d", &n)) { scanf("%s", s); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int minn = 1000000000, flag = 0; for (int i = 0; i < n; i++) { if (s[i] == 'R' && s[i + 1] == 'L') { minn = min(minn, (a[i + 1] - a[i]) / 2); flag = 1; } } if (flag) printf("%d\n", minn); else printf("-1\n"); } }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.Scanner; public class JavaApplication30 { private static Scanner in=new Scanner(System.in); public static void main(String[] args) { int num=in.nextInt(); in.nextLine(); String s=in.nextLine(); int list[]=new int[num]; int sum=1000000000; for(int i=0;i<num;i++) { list[i]=in.nextInt(); } if(s.contains("RL")) { int index; for(index=0;index<s.length()-1;index++) { if(s.charAt(index)=='R'&&s.charAt(index+1)=='L') { sum=Math.min(sum,((list[index]+list[index+1])/2-list[index])); } } } else { System.out.println(-1); return; } System.out.println(sum); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) s=input() arr=[int(i)for i in input().split()] li=[arr[i+1]-arr[i] for i in range(n-1) if s[i]=='R'and s[i+1]=='L'] if li: ans=min(li)//2 print(ans) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = 1e9; for (int i = 0; i < n - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'L') { ans = min(ans, (a[i + 1] - a[i]) / 2); } } if (ans == 1e9) { cout << -1 << "\n"; } else { cout << ans << "\n"; } return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL), cout.precision(15); int N; cin >> N; string s; cin >> s; int res = 1e9 + 100; bool f = false; int prev = -1, cur = -1; for (int i = 0; i < N; i++) { if (i == 0) cin >> prev; else { cin >> cur; if (s[i - 1] == 'R' && s[i] == 'L') res = min(res, (cur - prev) / 2), f = true; prev = cur; } } cout << (f ? res : -1) << "\n"; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; string s; cin >> s; vector<long long> v(n); for (int i = 0; i < n; i++) cin >> v[i]; long long ans = LONG_MAX; for (int i = 0; i < n - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'L') ans = min(ans, (v[i + 1] - v[i]) / 2); } cout << (ans != LONG_MAX ? ans : -1) << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << "\n"; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner SC=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); long n=0; String s=""; if(SC.hasNext()) n=SC.nextLong(); if(SC.hasNext()) s=SC.next(); long diff=-1; char current=s.charAt(0); int curr=SC.nextInt(); for(long i=1;i<n;i++) { int tmp=SC.nextInt(); if(current=='R'&&s.charAt((int)i)=='L') { if(diff==-1) diff=(tmp-curr); else diff=(long)Math.min((tmp-curr),diff); } curr=tmp; current=s.charAt((int)i); } if(diff==-1) System.out.println(-1); else System.out.println((diff/2)); } /*boolean Checkgood(long n,int k) { boolean arr[]=new boolean[k+1]; for(int i=0;i<=k;i++) { arr[i]=false; } while(n>0) { int temp=(int)n%10; if(temp<=k) arr[temp]=true; n/=10; } for(int i=0;i<=k;i++) { if(arr[i]==false) return false; } return true; } */ }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 1e9; for (int i = 0; i < n - 1; i++) if (s[i] == 'R' && s[i + 1] == 'L') ans = min(ans, (a[i + 1] - a[i]) / 2); if (ans == 1e9) cout << -1 << endl; else cout << ans << endl; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) s=input() a=list(map(int,input().split())) res=[a[i+1]-a[i] for i in range(n-1) if s[i]=='R' and s[i+1]=='L'] if(res): print(int(min(res)/2)) else: print(-1) # Made By Mostafa_Khaled
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) ds = input() tl = list(map(int,input().split())) ans=[tl[i+1]-tl[i] for i in range(n-1) if ds[i]=="R" and ds[i+1]=="L"] if len(ans)>0: print(min(ans)//2) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) drctn = list(input()) pos = list(map(int, input().split())) time = set() for i in range(n - 1): if drctn[i] == 'R' and drctn[i + 1] == 'L': time.add((pos[i + 1] - pos[i]) // 2) if len(time) > 0: print(min(time)) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = input() n = int(n) s = input() num = input() num = list((num.split(' '))) if n is 1: print(-1) exit() ans = 0x3f3f3f3f for i in range(n-1): if s[i] is 'R' and s[i+1] is 'L': ans = min((int(num[i+1])-int(num[i]))//2,ans) if ans is 0x3f3f3f3f: print(-1) else: print(ans)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.*; import java.io.*; public class R699A { FastScanner in; PrintWriter out; void solve() { int n=in.nextInt(); String s=in.next(); int[] x = new int[n]; int ans=Integer.MAX_VALUE; for(int i=0; i<n; i++) { x[i]=in.nextInt(); if (i>0 && s.charAt(i)=='L' && s.charAt(i-1)=='R') ans=Math.min(ans, (x[i]-x[i-1])/2); } if (ans==Integer.MAX_VALUE) ans=-1; System.out.println(ans); } void run() { try { in = new FastScanner(new File("CF.in")); out = new PrintWriter(new File("CF.out")); solve(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new R699A().runIO(); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; const long long OO = 1e8; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } const int N = 100; bool adjMatrixBool[N][N]; int adjMatrix[N][N]; vector<int> adjMatrixAll[N][N]; map<pair<int, int>, int> adjMatrixMap; vector<vector<int> > adjList1; vector<vector<pair<int, int> > > adjList2; vector<bool> visited; bool valid(int i, int j) { return 1; } char arr[303][303], x, b; bool prime(long long n) { long long i; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } int main() { ios_base::sync_with_stdio(false); long long n; cin >> n; string s; cin >> s; long long arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } long long ans = INT_MAX; for (int i = 0; i < n - 1; i++) { if (s[i] == 'R' and s[i + 1] == 'L') ans = min(ans, abs((arr[i + 1] - arr[i]) / 2)); } if (ans == INT_MAX) cout << "-1" << endl; else cout << ans; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(raw_input()) dir = raw_input() s= raw_input() l = s.split() flag =0 res =[] for i in range(len(dir)-1): if dir[i] == 'R' and dir[i+1]=='L': flag =1 res.append((int(l[i+1])-int(l[i]))/2) res.sort() if flag: print res[0] else: print -1
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(raw_input()) directions = raw_input() positions = map(int, raw_input().split()) result = -1 for i in xrange(0, n - 1): if directions[i] == 'R' and directions[i + 1] == 'L': if result == -1: result = (positions[i + 1] - positions[i]) / 2 else: result = min(result, (positions[i + 1] - positions[i]) / 2) print(result)
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) s=input() a=list(map(int,input().split())) #print(a) ans=-1 for i in range(1,n): if s[i-1]=='R' and s[i]=='L': ans=(min(ans,(a[i]-a[i-1])//2) if ans>=0 else (a[i]-a[i-1])//2) print(ans)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; char A[1000013]; long long B[1000013]; int main() { long long n; cin >> n; char t; long long j; for (long long i = 0; i < n; i++) { cin >> t; A[i] = t; } for (long long i = 0; i < n; i++) { cin >> j; B[i] = j; } long long min = 1000000000000; for (int i = 0; i < n - 1; i++) { if ((A[i] == 'R') && (A[i + 1] == 'L')) { if (min > (B[i + 1] - B[i]) / 2) { min = (B[i + 1] - B[i]) / 2; } } } if (min == 1000000000000) { cout << -1; } else { cout << min; } }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=input() m=raw_input() a=map(int,raw_input().split()) ans = -1 for i in range(n-1): if m[i] == 'R' and m[i+1] == 'L': x = (a[i+1]-a[i])/2 if ans == -1: ans = x else: ans = min(ans,x) print ans
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
x = input() d = raw_input() cor = map(int, raw_input().split()) ans = -1 r = 0 if d[0] == 'R' else -1 for i in xrange(1, x): curr_t = -1 if d[i] == 'L': if r != -1: curr_t = (cor[i] - cor[r]) / 2 else: r = i if curr_t != -1: ans = curr_t if ans == -1 else ans ans = curr_t if curr_t < ans else ans print ans
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, a[200000], b[200000], x = 0; cin >> n; char s[200000]; cin >> s; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; s[i] != '\0'; i++) { if (s[i] == 'R' && s[i + 1] == 'L') { b[x] = a[i + 1] - a[i]; x++; } } if (x == 0) { cout << "-1"; return 0; } sort(b, b + x); cout << b[0] / 2; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; const int f = 1378; int main() { int n, k = 0, c = 1000000000; cin >> n; string g; cin >> g; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { if (g[i] == 'R' && g[i + 1] == 'L') { k = a[i + 1] - a[i]; if (c > k) { c = k; } } } if (k == 0) { cout << -1; } else { cout << c / 2; } }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n, = map(int, input().split()) d = input() arr = list(map(int, input().split())) ans = float("inf") for i in range(1, n): if d[i] == 'L' and d[i - 1] == 'R': ans = min(ans, (arr[i] - arr[i - 1]) // 2) print(ans if ans < float("inf") else -1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) dir=input() l=list(map(int,input().split())) min=max(l) f=0 for i in range(n-1): if(dir[i]=='R' and dir[i+1]=='L'): f=1 a=(l[i+1]-l[i])//2 if(a<min): min=a if(f==0): print(-1) else: print(min)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int mx = 1e9; int main() { int a[200005]; string s; int n, res = 1e9; cin >> n; cin >> s; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'L') { res = min(res, (a[i + 1] - a[i]) / 2); } } if (mx == res) { cout << "-1" << endl; return 0; } cout << res << endl; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) s=input() x=list(map(int,input().split())) p=sorted([(x[i],s[i]) for i in range(n)]) ans=-1 mi=10**10 for i in range(1,n): if p[i-1][1]=='R' and p[i][1]=='L' and p[i][0]-p[i-1][0]<mi: mi=p[i][0]-p[i-1][0]; ans=mi//2 print(ans)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.*; import java.util.*; public class Main { public void solve() throws IOException { int n = nextInt(); String directions = next(); int x[] = new int [n]; for (int i = 0 ; i < n ; i++) { x[i] = nextInt(); } int ans = 1 << 30; for (int i = 0 ; i + 2 <= n ; i ++) { String two = directions.substring(i, i+2); if (two.equals("RL") ) { int delta = Math.abs(x[i] - x[i+1]) / 2; ans = Math.min(delta, ans); } } out.println( (ans < (1 << 30) ? ans : -1)); } BufferedReader bf; StringTokenizer st; PrintWriter out; public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public Main() throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); bf.close(); out.close(); } public static void main(String args[]) throws IOException { new Main(); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) s=input() c=[int(x) for x in input().split()] l=[] f=0 for i in range(len(s)-1): if s[i]=='R' and s[i+1]=='L': med=(c[i]+c[i+1])/2 l.append(abs(med-c[i])) f=1 l=sorted(l) if f==1: print(int(l[0])) else: print('-1')
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) dir = input() dist = [int(x) for x in input().split()] sz = len(dir) - 1 time = -1 for i in range(sz): if dir[i] == 'R' and dir[i+1] == 'L': x = int((dist[i+1] - dist[i]) / 2) if time == -1 or x < time: time = x print(time)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.*; import java.util.*; public class Code { static InputReader in = new InputReader(System.in); public static void main(String[] args) { //Scanner in = new Scanner(System.in); int n = in.nextInt(); String p = in.nextLine(); int[] d = new int[n]; for (int i = 0; i < d.length; i++) { d[i] = in.nextInt(); } int smallest = 999999999; for (int i = 0; i < p.length() - 1; i++) { if (p.charAt(i) == 'R' && p.charAt(i + 1) == 'L') { int def = (d[i + 1] - d[i]) / 2; if (def < smallest) { smallest = def; i++; } } } if (smallest == 999999999) { System.out.println(-1); } else { System.out.println(smallest); } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) { throw new InputMismatchException(); } if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } 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 String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
'''input 2 RL 0 1000000000 ''' n = int(input()) d = input() x = list(map(int, input().split())) if "RL" in d: m = 10000000000000000 for i in range(n-1): if d[i:i+2] == "RL": m = min(m, x[i+1] - x[i]) print(m//2) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(raw_input()) astr = raw_input() har = map(int,raw_input().split()) ans = 10**9+7 for i in xrange(1,n): if astr[i-1] == 'R' and astr[i] == 'L': ans = min(ans,abs(har[i] - har[i-1])/2) print -1 if ans == 10**9+7 else ans
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.*; import java.util.*; public class A { static class Pair implements Comparable<Pair>{ int num ; char dir; Pair (int n , char d){ num = n; dir = d; } @Override public int compareTo(Pair pair) { return num == pair.num ? 0 : num - pair.num; } } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); char arr [] = sc.next().toCharArray(); int num [] = new int[n]; for (int i = 0 ; i < n ; ++i) num[i] = sc.nextInt(); Pair pair [] = new Pair[n]; for (int i = 0 ; i < n ; ++i) pair[i] = new Pair(num[i] , arr[i]); Arrays.sort(pair); int min = ((int)1e9) + 2; for (int i = 0 ; i < n - 1 ; ++i) if (pair[i].dir == 'R' && pair[i + 1].dir == 'L') min = Math.min(min , (pair[i + 1].num - pair[i].num) >> 1); out.print(min == ((int)1e9) + 2 ? -1 : min); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
//package codeforces; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); String x=in.next(); int ans=(int) 1e9; int []nums=new int[n]; for(int i=0;i<n;i++) nums[i]=in.nextInt(); for(int i=0;i<n-1;i++) if(x.charAt(i)=='R' && x.charAt(i+1) =='L') ans=Math.min(ans, (nums[i+1] - nums[i])/2); if(ans ==1e9) System.out.println(-1); else System.out.println(ans); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) dirct = input() cord = list(map(int, input().split())) ans = [cord[i+1] - cord[i] for i in range(n-1) if dirct[i] == 'R' and dirct[i+1] == 'L'] print(min(ans)//2) if len(ans) > 0 else print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) S=input() l=list(map(int,input().split())) ma=10000000000 for i in range(1,n) : if S[i-1]=='R' and S[i]=='L' : if ma>abs(l[i]-l[i-1])//2 : ma=abs(l[i]-l[i-1])//2 if ma==10000000000 : print(-1) else : print(ma)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) st = input() a = [int(i) for i in input().split()] for i in range(n-1): if(a[i]==a[i+1]): print(0) exit(0) ans = 10**9 for i in range(n-1): if st[i]=='R' and st[i+1]=='L': ans = min(ans,(a[i+1]-a[i])//2) if ans!=10**9: print(ans) else: print(-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long n; string s; vector<long long> v; cin >> n; cin >> s; int z; for (long long i = 0; i < n; i++) { cin >> z; v.push_back(z); } bool col = false; vector<int> diff; int x; int ind = find(v.begin(), v.end(), 'R') - v.end(); for (long long i = ind; i < n - 1; i++) { if (s[i] == 'R') { if (s[i + 1] == 'L') { x = abs(v[i] - v[i + 1]) / 2; diff.push_back(x); col = true; } } } if (col == false) cout << "-1" << endl; else cout << *min_element(diff.begin(), diff.end()); return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.*; public class Main { public static void main (String args[]) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); String word =sc .next(); long arr[] = new long[n]; long count[] = new long[n]; int x=0; if(n>1) { for(int i=0; i<n;i++) { arr[i] =sc.nextLong(); } // System.out.println(word.charAt(0)); for(int i=0; i<n-1;i++) { if(word.charAt(i) == 'R' && word.charAt(i+1) == 'L') { count[x] = (arr[i+1] - arr[i])/2; x++; } else continue; } //System.out.println(count[0]); //int count=0; long min= count[0]; //System.out.println(count[1]); for(int i=0;i<x;i++) { if(count[i] < min ) { min = count[i]; } } if(min >0) System.out.println(min); else System.out.println(-1); } else System.out.println(-1); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.Scanner; public class ColliderLaunch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int testCases = scanner.nextInt(); String input = scanner.next(); int[] coordinates = new int[testCases]; coordinates[0] = scanner.nextInt(); int min = Integer.MAX_VALUE; for (int i = 1; i < testCases; i++) { coordinates[i] = scanner.nextInt(); if (input.charAt(i) == 'L' && input.charAt(i - 1) == 'R') { min = Math.min((coordinates[i] - coordinates[i - 1]) / 2, min); } } if (min == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min); } } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) st = input() lst = list(map(int, input().split())) minn = float("INF") yes = False for i in range(n-1): if st[i] == "R" and st[i+1] == "L": yes = True minn = min(minn, (lst[i+1] - lst[i])//2) if not yes: print(-1) else: print(minn+1-1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.*; public class launchofcollider { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=Integer.parseInt(in.nextLine()); String str=in.nextLine(); int a[]=new int[n]; int i,diff=0,min=Integer.MAX_VALUE; String s[]=in.nextLine().split(" "); for(i=0;i<n;i++) a[i]=Integer.parseInt(s[i]); if(str.contains("RL")==false) System.out.println("-1"); else { for(i=0;i<n-1;i++) { if(str.charAt(i)=='R' && str.charAt(i+1)=='L') { diff=(a[i+1]-a[i])/2; if(diff<min) min=diff; } } System.out.println(min); } } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) t=input() arr1=list(map(int,input().split())) ans=[arr1[i+1]-arr1[i] for i in range(n-1) if (t[i]=='R' and t[i+1]=='L')] if ans: print(min(ans)//2) else: print('-1')
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.*; import java.io.*; public class LaunchofCollider { /************************ SOLUTION STARTS HERE ***********************/ private static void solve(FastScanner s1, PrintWriter out){ int N = s1.nextInt(); String seq = s1.nextLine(); int arr[] = s1.nextIntArray(N); int lastLeft = Integer.MAX_VALUE; int min = Integer.MAX_VALUE; for(int i = N - 1;i >= 0 ;i--){ if(seq.charAt(i) == 'L') lastLeft = arr[i]; else if(lastLeft != Integer.MAX_VALUE) min = Math.min(min,(lastLeft - arr[i]) / 2); } out.println(min == Integer.MAX_VALUE ? -1 : min); } /************************ SOLUTION ENDS HERE ************************/ /************************ TEMPLATE STARTS HERE *********************/ public static void main(String []args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false); solve(in, out); in.close(); out.close(); } static class FastScanner{ BufferedReader reader; StringTokenizer st; FastScanner(InputStream stream){reader=new BufferedReader(new InputStreamReader(stream));st=null;} String next() {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;} st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();} String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} char nextChar() {return next().charAt(0);} int[] nextIntArray(int n) {int[] arr= new int[n]; int i=0;while(i<n){arr[i++]=nextInt();} return arr;} long[] nextLongArray(int n) {long[]arr= new long[n]; int i=0;while(i<n){arr[i++]=nextLong();} return arr;} int[] nextIntArrayOneBased(int n) {int[] arr= new int[n+1]; int i=1;while(i<=n){arr[i++]=nextInt();} return arr;} long[] nextLongArrayOneBased(int n){long[]arr= new long[n+1];int i=1;while(i<=n){arr[i++]=nextLong();}return arr;} void close(){try{reader.close();}catch(IOException e){e.printStackTrace();}} } /************************ TEMPLATE ENDS HERE ************************/ }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.Scanner; import java.util.TreeSet; public class LaunchOfCollider { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); sc.close(); TreeSet<Integer> set = new TreeSet<>(); for(int i = 0; i < n - 1; i++){ if(s.charAt(i) == 'R' && s.charAt(i + 1) == 'L'){ set.add((a[i + 1] - a[i]) / 2); } } if(set.size() > 0) System.out.println(set.first()); else System.out.println(-1); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int n; char dir[200047]; long long l[200047]; int main() { scanf("%d", &n); scanf("%s", dir); for (int i = 0; i < n; i++) scanf("%lld", l + i); long long ans = -1; for (int i = 0; i < n - 1; i++) if ((dir[i] == 'R') && (dir[i + 1] == 'L')) { if (ans == -1) ans = (l[i + 1] - l[i]) / 2; else ans = min(ans, (l[i + 1] - l[i]) / 2); } printf("%lld", ans); return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); String inp = sc.nextLine(); String [] crap = sc.nextLine().split(" "); int [] arr = new int [n]; //int [] dir = new int [n]; for (int i=0; i<n; i++) { arr[i] = Integer.parseInt(crap[i]); } int min = Integer.MAX_VALUE; for (int i=0; i<n-1; i++) { if ((inp.charAt(i)=='R'&&inp.charAt(i+1)=='L')) { if (Math.abs(arr[i]-arr[i+1])<min) { min = Math.abs(arr[i]-arr[i+1]); } } } if (min<Integer.MAX_VALUE) System.out.println(min/2); else System.out.println(-1); } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import math N = int(input()) directions = [x for x in input().split()] speeds =[int(x) for x in input().split()] min = 1000000000 found = 0 for x in range(0,len(directions[0])-1): # print(directions[0][x]) if directions[0][x]=="R" and directions[0][x+1]=="L": time = (speeds[x+1]-speeds[x])/2 #print(time) if time<min: min = time found = 1 if found==0: print("-1") else: print(math.floor( min ))
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) s=input() a=list(map(int, input().split())) ans=2**40 for i in range(1, n): if s[i-1]=='R' and s[i]=='L': ans=min(ans, a[i]-a[i-1]) print(-1 if ans==2**40 else ans>>1)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Main { public static void main(String[] args) { Scanner s1=new Scanner(System.in); int n; n=s1.nextInt(); String h=s1.next(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=s1.nextInt(); } int min=1000000000; int c=0; for(int i=0;i<h.length()-1;i++) { if(h.charAt(i)=='R'&&h.charAt(i+1)=='L') { int h1=arr[i+1]-arr[i]; h1=h1/2; if(min>h1) { min=h1; } c++; } } if(c==0) { System.out.println("-1"); } else { System.out.println(min); } } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import sys n = int(input()) d = input() x = list(map(int, input().split())) r = 0 l = 0 m = sys.maxsize while True: r = d.find('R', l) if r == -1: break l = d.find('L', r + 1) if l == -1: break r = l - 1 if m > (x[l] - x[r]) // 2: m = (x[l] - x[r]) // 2 if m == sys.maxsize: print(-1) else: print(m)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, i = 0, d, c = 0, cd = 1000000000; cin >> n; string s; cin >> s; vector<int> a(n); while (i < n) { cin >> a[i]; i++; } i = 0; while (i < n - 1) { if ((s[i] == 'R') && (s[i + 1] == 'L')) { d = a[i + 1] - a[i]; c++; if (d < cd) { cd = d; } } i++; } if (c != 0) { cout << cd / 2; } else { cout << -1; } return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
from sys import stdin inf=1000*1000*1000 n=int(input()) directions=stdin.readline() depart=list(map(int,stdin.readline().split())) tempsMin=inf for i in range(n-1): direction=directions[i] directionApres=directions[i+1] temps=(depart[i+1]-depart[i])//2 if direction=="R" and directionApres=="L": tempsMin=min(temps,tempsMin) if tempsMin==inf: print(-1) else: print(tempsMin)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) def loadinput(): n = int(input()) di = list(str(input())) vals =list(map(int,input().split())) return di,vals def checkstews(di, vals): ans = -1 for i in range(len(di)-1): if di[i] == 'R' and di[i+1] == 'L': if vals[i] < vals[i+1]: if (int((vals[i+1] - vals[i])/2) < ans) or ans == -1: ans = int((vals[i+1] - vals[i])/2) else: continue return ans if __name__ == '__main__': di, vals = loadinput() ans = checkstews(di, vals) print(ans)
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n=int(input()) d=input() p=input().split(' ') if 'RL' not in d: print (-1) else: l=0 c=1000000000 while 'RL' in d[l:]: l=d[l:].index('RL')+l if ( int(p[l+1]) - int(p[l]) )/2<c: c=(int(p[l+1])-int(p[l]))/2 l+=2 print (int(c))
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(raw_input()) s = raw_input() p = [int(x) for x in raw_input().split()] r = 0 l = 0 ans = -1 while r <n: while(r < n and s[r] != 'R' ) : r+=1 if (l <= r): l = r+1 while(l < n and s[l] != 'L'): l+=1 if l==n or r == n: break if (ans == -1) or (p[l]-p[r])/2 < ans: ans = (p[l]-p[r])/2 r += 1 print ans
PYTHON
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import java.io.*; import java.util.*; public class TaskA { public static void main (String[] args) throws IOException { FastScanner fs = new FastScanner(System.in); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); int n = fs.nextInt(); String s = fs.reader.readLine(); int t = 0; int p = 0; int m = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int a = fs.nextInt(); if (t == 0) { if (s.charAt(i) == 'R') { p = a; t = 1; } } else if (t == 1) { if (s.charAt(i) == 'R') { p = a; } else { if (a-p < m) { m = a-p; } t = 0; } } } if (m == Integer.MAX_VALUE) { pw.println("-1"); } else { m >>= 1; pw.println(m); } pw.close(); } static class FastScanner { BufferedReader reader; StringTokenizer tokenizer; FastScanner(InputStream i) { reader = new BufferedReader(new InputStreamReader(i)); tokenizer = new StringTokenizer(""); } String next() throws IOException { while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } } }
JAVA
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; char s[200010]; int a[200010]; int main() { int n, i, j, k; scanf("%d", &n); scanf("%s", s); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int mi = 2e9; int len = strlen(s); for (i = 0; i < len - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'L') { mi = min(mi, a[i + 1] - a[i]); } } if (mi == 2e9) printf("-1\n"); else printf("%d\n", mi / 2); return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = -1; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'R' && s[i + 1] == 'L') { int temp = (a[i + 1] - a[i]) / 2; if (temp < ans || ans == -1) ans = temp; } } cout << ans << endl; return 0; }
CPP
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
import math n=int(input()) s=input() lt=list(input().split()) l=len(lt)-1 mn=10**10 for i in range(l): if s[i]=='R' and s[i+1]=='L': a=int(lt[i+1])-int(lt[i]) if a<mn: mn=a if mn==10**10: print(-1) else: print(math.ceil(mn/2))
PYTHON3
699_A. Launch of Collider
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movement β€” it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≀ xi ≀ 109) β€” the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Examples Input 4 RLRL 2 4 6 10 Output 1 Input 3 LLR 40 50 60 Output -1 Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
2
7
n = int(input()) d = input() p = list(map(int,input().split())) m=max(p) f=0 for i in range(n-1): if(d[i]=='R' and d[i+1]=='L'): s=(p[i]+p[i+1])//2 - p[i] if(s<m): m=s f=1 if(f==0): print(-1) else: print(m)
PYTHON3