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
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; string ans = ""; bool solve(map<char, int> &mp, int tot, int len) { string s = ""; for (auto i : mp) { int a = (i.second / tot + (i.second % tot > 0)); len -= a; while (a--) s += i.first; } if (len >= 0) { ans = s; return true; } return false; } int main() { string str; cin >> str; int n; cin >> n; int e = str.length(); int s = 0; map<char, int> mp; mp.clear(); for (int i = 0; i < str.length(); i++) { mp[str[i]]++; } if (mp.size() > n) { cout << -1 << endl; return 0; } int x = 0; for (int b = e; b >= 1; b /= 2) { while (!solve(mp, b + x, n)) x += b; } int k = x + 1; cout << k << endl; while (ans.length() < n) ans += 'a'; cout << ans << endl; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; import java.io.*; public class A { public static boolean bg = true; public static BR in = new BR(); public static void main(String[] args) throws Exception { char[] l1 = in.nx().toCharArray(); int n1 = in.ni(); int[] count = new int[26]; for (char e: l1){ count[e-'a']++; } int nos = 0; for (int e: count){ if (e>0) nos++; } if (nos>n1){ pn(-1); ex(); } for (int i = 1;i<=1000+10;i++){ int[] cur = new int[26]; int total = 0; for (int j = 0;j<26;j++){ int temp = count[j]/i; if (count[j]%i!=0) temp++; cur[j] = temp; total += temp; } if (total<=n1){ StringBuilder fin = new StringBuilder(); for (int j = 0;j<26;j++){ for (int k =0;k < cur[j]; k++){ fin.append((char)(j+'a')); } } for (;;){ if (fin.length()>=n1) break; fin.append('a'); } pn(i); pn(fin); System.exit(0); } } } public static void s1() throws Exception { } private static void pn(Object o1) { System.out.println(o1); } private static void ex() { System.exit(0); } private static class BR { BufferedReader k1 = null; StringTokenizer k2 = null; public BR() { k1 = new BufferedReader(new InputStreamReader(System.in)); } public String nx() throws Exception { for (;;) { if (k2 == null) { String temp = k1.readLine(); if (temp == null) return null; k2 = new StringTokenizer(temp); } else if (!k2.hasMoreTokens()) { String temp = k1.readLine(); if (temp == null) return null; k2 = new StringTokenizer(temp); } else break; } return k2.nextToken(); } public int ni() throws Exception { return Integer.parseInt(nx()); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip from collections import Counter import heapq as pq from math import ceil def solve(): s = insr() n = inp() count_map = Counter(s) if n < len(count_map): print(-1) return copy_map = {el: 1 for el in count_map.keys()} used = len(count_map) q = [(-el, key) for key, el in count_map.items()] pq.heapify(q) while used < n and q: copy, c = pq.heappop(q) copy_map[c] += 1 copies = ceil(count_map[c] / copy_map[c]) if copies > 1: pq.heappush(q, (-copies, c)) used += 1 if not q: print(1) else: print(pq.heappop(q)[0] * -1) res = [] for el, copies in copy_map.items(): res.extend([el] * copies) if len(res) < n: res.extend(['a'] * (n - len(res))) print("".join(res)) def main(): t = 1 for i in range(t): solve() BUFSIZE = 8192 def inp(): return (int(input())) def inlt(): return (list(map(int, input().split()))) def insr(): return (input().strip()) def invr(): return (map(int, input().split())) class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char str[1005]; int N; int cword[30]; int main() { scanf("%s", str); scanf("%d", &N); int i; int type = 0; for (i = 0; str[i] != '\0'; i++) { if (cword[(int)(str[i] - 'a' + 1)] == 0) type++; cword[(int)(str[i] - 'a' + 1)]++; } if (N < type) printf("-1\n"); else { int st = 1, ed = i - 1; while (st <= ed) { int Sum = 0; int mid = (st + ed) / 2; for (int i = 1; i <= 26; i++) { Sum += (cword[i] + mid - 1) / mid; } if (Sum > N) { st = mid + 1; } else { ed = mid - 1; } } int ans = ed + 1; int print = 0; printf("%d\n", ans); for (int i = 1; i <= 26; i++) { int k = (cword[i] + ans - 1) / ans; for (int j = 1; j <= k; j++) { printf("%c", i + 'a' - 1); print++; } } while (print < N) { printf("a"); print++; } printf("\n"); } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; void data() { freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); } char s[1100]; int su[27]; int k; int jd(int n) { int cnt = 0; for (int i = 0; i < (26); ++i) { cnt += (su[i] + n - 1) / n; } if (cnt <= k) return 1; else return 0; } void pt(int n) { cout << n << endl; string ss = ""; int cnt = 0; for (int i = 0; i < (26); ++i) { for (int j = 0; j < ((su[i] + n - 1) / n); ++j) { ss += (i + 'a'); cnt++; } } cout << ss; for (int i = 0; i < (k - cnt); ++i) cout << 'a'; cout << endl; } void bit(int l, int r) { while (l < r) { int mid = (l + r) / 2; int kk = jd(mid); if (kk > 0) r = mid; else l = mid + 1; } pt(l); } int main() { while (cin >> s >> k) { for (int i = 0; i < (26); ++i) su[i] = 0; for (int i = 0; i < (strlen(s)); ++i) su[s[i] - 'a']++; int ma = 0, cnt = 0; for (int i = 0; i < (26); ++i) if (su[i]) { cnt++; ma = max(ma, su[i]); } if (k < cnt) cout << -1 << endl; else { bit((strlen(s) + k - 1) / k, ma + 1); } } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int N = 100500; const int inf = 1 << 30; char s[1010]; int cnt[26], n; int main() { cin >> s >> n; for (int i = 0; s[i]; i++) cnt[s[i] - 'a']++; for (int t = 1; t <= 1000; t++) { int sum = 0; for (int i = 0; i < 26; i++) sum += (cnt[i] + t - 1) / t; if (sum <= n) { cout << t << endl; for (int i = 0; i < 26; i++) for (int j = 0; j < (cnt[i] + t - 1) / t; j++) { putchar('a' + i); n--; } for (int i = 0; i < n; i++) putchar('a'); return 0; } } cout << -1; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; string s, ans; int n, k, lo, hi; bool sat(int x) { int occ[26], ret = 0; memset(occ, 0, sizeof(occ)); int len = s.length(); for (int i = 0; i < len; i++) occ[s[i] - 'a']++; for (int i = 0; i < 26; i++) ret += ((occ[i] + x - 1) / x); return ret <= n; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); cin >> s; cin >> n; lo = 0, hi = s.length(); while (lo < hi) { int mid = lo + (hi - lo + 1) / 2; if (sat(mid)) hi = mid - 1; else lo = mid; } if ((lo + 1) > s.length()) { cout << -1 << '\n'; return 0; } int occ[26]; memset(occ, 0, sizeof(occ)); for (int i = 0; i < s.length(); i++) occ[s[i] - 'a']++; for (int i = 0; i < 26; i++) for (int j = 0; j < ((occ[i] + (lo + 1) - 1) / (lo + 1)); j++) ans += (i + 'a'); for (int i = ans.length(); i < n; i++) ans += 'a'; cout << lo + 1 << '\n'; cout << ans << '\n'; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int org[26], req[26], y, n, ans, maxi; int ansr(int l, int r) { if (l >= r) return l; int mid = l + (r - l) / 2; int o = 0; for (int i = 0; i < 26; i++) if (org[i] != 0) { o += org[i] / mid; if (org[i] % mid != 0) o += 1; } if (o > n) return ansr(mid + 1, r); else return ansr(l, mid); } int main() { char s[1003]; cin >> s; cin >> n; for (int i = 0; s[i] != '\0'; i++) org[s[i] - 97]++; for (int i = 0; i < 26; i++) if (org[i] != 0) { ++y; req[i] = 1; if (maxi < org[i]) maxi = org[i]; } if (y > n) cout << "-1\n"; else { ans = ansr(1, maxi); int o = 0, suma = 0; for (int i = 0; i < 26; i++) if (org[i] != 0) { o = 0; o += org[i] / ans; if (org[i] % ans != 0) o += 1; req[i] = o; suma += o; } cout << ans << endl; for (int i = 0; i < 26; i++) while (req[i]--) cout << (char)(i + 97); if (suma < n) for (int i = 1; i <= n - suma; i++) cout << 'a'; cout << endl; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1010; char str[N]; int cnt[26]; int ans[26]; int f(int i) { return cnt[i] / ans[i] + (bool)(cnt[i] % ans[i]); } int main() { scanf("%s", str); int n; scanf("%d", &n); memset(cnt, 0, sizeof(cnt)); int len = strlen(str); for (int i = 0; i < len; i++) cnt[str[i] - 97]++; memset(ans, 0, sizeof(ans)); for (int i = 0; i < 26; i++) if (cnt[i]) { ans[i]++; n--; } if (n < 0) { printf("-1\n"); return 0; } for (int i = 0; i < n; i++) { int k = -1; for (int j = 0; j < 26; j++) if (cnt[j]) { if (k == -1 || f(j) > f(k)) k = j; } ans[k]++; } int t = 0; for (int i = 0; i < 26; i++) if (cnt[i]) t = max(t, f(i)); printf("%d\n", t); for (int i = 0; i < 26; i++) { for (int j = 0; j < ans[i]; j++) printf("%c", 'a' + i); } printf("\n"); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int num[30]; char str[1000010]; int out[30]; int limit; bool ok(int mid) { for (int i = 0; i < 26; i++) { if (num[i] == 0) continue; out[i] = num[i] / mid; if (num[i] % mid != 0) out[i]++; } int cnt = 0; for (int i = 0; i < 26; i++) { if (num[i] == 0) continue; cnt += out[i]; } return cnt <= limit; } int main() { scanf("%s", str); int len = strlen(str); memset(num, 0, sizeof(num)); for (int i = 0; i < len; i++) { int id = str[i] - 'a'; num[id]++; } scanf("%d", &limit); int need = 0; for (int i = 0; i < 30; i++) if (num[i] > 0) need++; if (need > limit) { printf("-1\n"); return 0; } int left = 1; int right = len; int ans = len; while (right - left >= 0) { int mid = (left + right) / 2; if (ok(mid)) { ans = mid; right = mid - 1; } else left = mid + 1; } printf("%d\n", ans); ok(ans); string f = ""; for (int i = 0; i < 26; i++) { char now = 'a' + i; while (out[i]--) f += now; } len = f.size(); while (len < limit) { f += 'a'; len++; } cout << f << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { int d[256] = {0}, n; string s; cin >> s >> n; for (int i = 0; i < int(s.size()); i++) d[s[i]]++; int cntDif = 0; for (char c = 'a'; c <= 'z'; c++) if (d[c]) cntDif++; if (cntDif > n) puts("-1"); else for (int ans = 1; ans <= 10000; ans++) { int need = 0; for (char c = 'a'; c <= 'z'; c++) need += (d[c] + ans - 1) / ans; if (need <= n) { cout << ans << endl; for (char c = 'a'; c <= 'z'; c++) for (int i = 0; i < (d[c] + ans - 1) / ans; i++) cout << c; while (need < n) need++, cout << 'a'; cout << endl; break; } } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s=raw_input() n=input() l=[0]*26 ans='' for i in s: if i not in ans: ans+=i else: l[ord(i)-97]+=1 if len(ans)>n: print -1 elif len(s)==n: print 1 print s else: chk=n-len(ans) while 1: if chk==0 or sum(l)==0:break mc=mi=0 for i in range(26): ct=ans.count(chr(i+97)) # if ct:k=(l[i]+ct-1)/ct+1 if ct: k=(s.count(chr(i+97))+ct-1)/ct if k>mc: mc=k mi=i if mc>0: ans+=chr(mi+97) l[mi]-=1 chk-=1 c=k=1 for i in range(26): if l[i]>0: kari=ans.count(chr(i+97)) # c=max(c,(l[i]+kari-1)/kari+1) c=max(c,(s.count(chr(i+97))+kari-1)/kari) print c print ans if len(ans)==n else ans+'a'*(n-len(ans))
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int NMax = 1000001; const int cntTree = 262145; const long double eps = 1e-9; const double PI = 3.141592653589793238462; const int MD = (int)1e9 + 7; FILE *stream; string s; int n; int c[27], c1[27]; int main() { cin >> s >> n; for (int i = 0; i < (int)(s).size(); i++) c[s[i] - 'a']++; int cnt = 0; for (int i = 0; i < 27; i++) if (c[i] > 0) cnt++; if (cnt > n) { cout << -1 << endl; return 0; } string ans = ""; for (char ch = 'a'; ch <= 'z'; ch++) if (c[ch - 'a'] > 0) { ans += ch; n--; c1[ch - 'a']++; } int cntMax; while (true) { char tmp = '#'; cntMax = 0; for (char ch = 'a'; ch <= 'z'; ch++) if (c[ch - 'a'] > 0) { if (c[ch - 'a'] % c1[ch - 'a'] == 0) cnt = 0; else cnt = 1; if (cntMax < c[ch - 'a'] / c1[ch - 'a'] + cnt) { cntMax = c[ch - 'a'] / c1[ch - 'a'] + cnt; tmp = ch; } } if (n == 0) break; ans += tmp; c1[tmp - 'a']++; n--; } cout << cntMax << endl << ans << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int dir[4][2] = {-1, 0, 0, 1, 0, -1, 1, 0}; char s[1010]; int cnt[30] = {0}; int n; bool can(int mid) { int k = 0; for (int i = 0; i < 26; i++) { if (cnt[i] == 0) continue; int t = cnt[i] - mid; if (t > 0) { k += (t + mid - 1) / mid; } } return k <= n; } int main() { int nn; scanf("%s", s); scanf("%d", &n); for (int i = 0; s[i]; i++) { cnt[s[i] - 'a']++; } nn = n; for (int i = 0; i < 26; i++) if (cnt[i]) n--; if (n < 0) { puts("-1"); return 0; } int l = 1, r = 1000, ans; while (l <= r) { int mid = (l + r) >> 1; if (can(mid)) ans = mid, r = mid - 1; else l = mid + 1; } printf("%d\n", ans); string res = ""; for (int i = 0; i < 26; i++) { if (cnt[i] == 0) continue; int t = cnt[i] - ans; res += (char)(i + 'a'); if (t <= 0) continue; int k = t / ans; if (t % ans) k++; while (k--) res += (char)(i + 'a'); } while (res.length() < nn) res += 'a'; cout << res << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class CF335A { static final int CHARS = 26; public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); char[] s = sc.nextToken().toCharArray(); int n = sc.nextInt(); int[] occs = new int[CHARS]; int distinct = 0; for (char ch : s) { if (occs[ch - 'a'] == 0) distinct++; occs[ch - 'a']++; } if (distinct > n) { pw.println(-1); pw.flush(); return; } int lo = 1, hi = 1000; while (lo <= hi) { int mid = (lo + hi) / 2; int n1 = 0; for (int occ : occs) if (occ > 0) n1 += (occ + mid - 1) / mid; // Math.ceil(occ/sheets) if (n1 > n) lo = mid + 1; else hi = mid - 1; } int sheets = lo; pw.println(sheets); StringBuilder sb = new StringBuilder(); for (int i = 0; i < CHARS; i++) { int x = (occs[i] + sheets - 1) / sheets; while (x-- > 0) sb.append((char) ('a' + i)); } while (sb.length() < n) sb.append('a'); pw.println(sb); pw.flush(); } static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner() { this.in = new BufferedReader(new InputStreamReader(System.in)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public void close() throws IOException { in.close(); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from collections import Counter def main(): s = raw_input().strip() l = int(raw_input()) d = Counter(s) if len(d) > l: print -1 return lo = 0 hi = 10000 while lo + 1 < hi: mid = (lo + hi) / 2 c = 0 for x in d.itervalues(): c += (x + mid - 1) / mid if c > l: lo = mid else: hi = mid print hi ans = [] for x in d.iteritems(): ans.append(x[0] * ((x[1] + hi - 1) / hi)) t = ''.join(ans) if len(t) < l: t += 'a' * (l - len(t)) print t main()
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class ProblemA { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String s = in.next(); int n = in.nextInt(); int[] cnt = new int[26]; for (int i = 0 ; i < s.length() ; i++) { cnt[s.charAt(i)-'a']++; } boolean found = false; for (int use = 1 ; use <= 1000 ; use++) { StringBuilder bl = new StringBuilder(); for (int c = 0 ; c < 26 ; c++) { if (cnt[c] >= 1) { int hv = (cnt[c] + use - 1) / use; for (int h = 0 ; h < hv ; h++) { bl.append((char)('a' + c)); } } } if (bl.length() <= n) { found = true; for (int i = bl.length() ; i < n ; i++) { bl.append('z'); } out.println(use); out.println(bl.toString()); break; } } if (!found) { out.println(-1); } out.flush(); } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const double PI = 2 * acos(0); const double EPS = 1e-9; const long long oo = 1e18; const long long MOD = 1e9 + 7; const int N = 1e3 + 5; const int AL = 26; char in[N]; int freq[AL], freq2[AL]; int main() { int n; scanf("%s%d", &in, &n); string s = in; int cnt = 0; for (int i = 0; i < int((s).size()); i++) { int c = s[i] - 'a'; if (!freq[c]) cnt++; freq[c]++; } if (cnt > n) return puts("-1"); string out; for (int i = 0; i < AL; i++) { if (freq[i]) out.push_back(i + 'a'), freq2[i]++; } while (int((out).size()) < n) { int mx = 0; int idx; for (int i = 0; i < AL; i++) { if (freq2[i]) { int num = (freq[i] + freq2[i] - 1) / freq2[i]; if (num > mx) mx = num, idx = i; } } freq2[idx]++; out.push_back(idx + 'a'); } int ans = 0; for (int i = 0; i < AL; i++) if (freq2[i]) ans = max(ans, (freq[i] + freq2[i] - 1) / freq2[i]); printf("%d\n%s", ans, out.c_str()); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
//package prac; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class MA { InputStream is; PrintWriter out; String INPUT = ""; void solve() { char[] s = ns().toCharArray(); int n = ni(); int[] f = new int[26]; for(char c : s){ f[c-'a']++; } int low = 0, high = 10000; while(high - low > 1){ int x = high+low>>>1; int ne = 0; for(int i = 0;i < 26;i++){ ne += (f[i]+x-1)/x; } if(ne <= n){ high = x; }else{ low = x; } } if(high > 3000){ out.println(-1); }else{ out.println(high); int ct = 0; for(int i = 0;i < 26;i++){ int u = (f[i]+high-1)/high; for(int j = 0;j < u;j++){ out.print((char)('a'+i)); ct++; } } while(ct < n){ out.print("z"); ct++; } out.println(); } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new MA().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s,n,q,a=input(),int(input()),{},"" for i in s:q[i]=[q[i][0]+1,1]if i in q else [1,1] if len(q)>n:print(-1) else: for i in range(n-len(q)): o=0 for j in q: m=(q[j][0]+q[j][1]-1)//q[j][1] if m>o:o,w=m,j q[w][1]=q[w][1]+1 o=0 for i in q: a+=i*q[i][1] m=(q[i][0]+q[i][1]-1)//q[i][1] if m>o:o=m print(o,"\n",a)
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner in = new Scanner(System.in); char c[] = in.next().toCharArray(); int n = in.nextInt(); int a[] = new int[26]; for (int i = 0; i < c.length; i++) { a[c[i] - 'a']++; } int left = 1; int right = 10000; int found = -1; String result = ""; while(left <= right){ int mid = (left + right)/2; String str = ""; for (int i = 0; i < a.length; i++) { int count = (a[i] + mid - 1)/mid; for (int j = 0; j < count; j++) { str += (char)(i + 'a'); } } if(str.length() <= n){ found = mid; result = str; right = mid - 1; } else { left = mid + 1; } } while(result.length() < n) result += "z"; if(found != -1){ System.out.println(found); System.out.println(result); } else { System.out.println("-1"); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; public class maximus { public static void main(String [] args){ Scanner in=new Scanner(System.in); String str=in.next(); int n=in.nextInt(); int array[]=new int[26]; for(int i=0;i<str.length();i++)array[str.charAt(i)-97]++; int high=1001; int low=0; int cnt=0; for(int i=0;i<26;i++)if(array[i]!=0)cnt++; if(cnt>n)System.out.print("-1"); else{ int temp=0; while(low<high){ int mid=(high+low)/2; temp=0; for(int i=0;i<26;i++)temp+=Math.ceil((double)array[i]/mid); if(temp>n)low=mid+1; else high=mid; //System.out.println(low+" "+high+" "+temp); } temp=0; for(int i=0;i<26;i++)temp+=Math.ceil((double)array[i]/(low)); if(temp<=n && low>0){ System.out.println(low); StringBuilder sb=new StringBuilder(); for(int i=0;i<26;i++) for(int j=0;j<Math.ceil((double)array[i]/low);j++)sb.append((char)(i+97)); for(int i=sb.length();i<n;i++)sb.append('a'); System.out.print(sb); } else{ System.out.println(low+1); StringBuilder sb=new StringBuilder(); low++; for(int i=0;i<26;i++) for(int j=0;j<Math.ceil((double)array[i]/low);j++)sb.append((char)(i+97)); for(int i=sb.length();i<n;i++)sb.append('a'); System.out.print(sb); } } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char s[1024]; int n; int cnt[26], kind; int mem[26]; char res[1024]; bool chk(int sz) { for (int i = 0; i < 26; i++) mem[i] = cnt[i]; int ch = 0; for (int i = 0; i < n; i++) { while (ch < 26 && mem[ch] <= 0) ch++; if (ch == 26) res[i] = 'z'; else { res[i] = ch + 'a'; mem[ch] -= sz; } } for (int i = 0; i < 26; i++) if (mem[i] > 0) return false; return true; } int main() { scanf("%s", s); scanf("%d", &n); for (int i = 0; i < 26; i++) cnt[i] = 0; for (int i = 0; s[i]; i++) cnt[s[i] - 'a']++; kind = 0; for (int i = 0; i < 26; i++) if (cnt[i]) kind++; if (kind > n) printf("-1\n"); else { int lb = 1, ub = strlen(s); while (lb < ub) { int md = lb + (ub - lb) / 2; if (chk(md)) ub = md; else lb = md + 1; } printf("%d\n", lb); chk(lb); printf("%s\n", res); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; bool solved[26][1003]; char s[1003]; int C[26], M[26][1003], P[26][1003]; int DP(int i, int j) { if (i == 26) return 0; if (solved[i][j]) return M[i][j]; solved[i][j] = true; if (!C[i]) return M[i][j] = DP(i + 1, j); M[i][j] = 2147483647; for (int k = 1, r; k <= C[i] && k <= j; ++k) { r = max((C[i] + k - 1) / k, DP(i + 1, j - k)); if (M[i][j] > r) M[i][j] = r, P[i][j] = k; } return M[i][j]; } int print_soln(int i, int j) { if (i < 26) { for (int k = 0; k < P[i][j]; ++k) putchar('a' + i); return P[i][j] + print_soln(i + 1, j - P[i][j]); } return 0; } void solve(int n) { int soln, i; for (i = 0; s[i]; ++i) C[s[i] - 'a']++; soln = DP(0, n); if (soln < 2147483647) { int c; printf("%d\n", soln); c = print_soln(0, n); for (i = c; i < n; ++i) putchar('a'); } else puts("-1"); } int main() { int n; scanf("%s %d", s, &n); solve(n); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; char s[200010]; int sum[333], t[333]; int n, m; bool check() { int time = 0; for (int i = ('a'); i <= ('z'); ++i) time += (sum[i] > 0); return time <= n; } bool ok(int limit) { memset(t, 0, sizeof(t)); int time = 0; for (int i = ('a'); i <= ('z'); ++i) { t[i] = (sum[i] + limit - 1) / limit; time += t[i]; } return time <= n; } int main() { scanf("%s%d", s, &n); m = strlen(s); memset(sum, 0, sizeof(sum)); for (int i = 0; i < (m); ++i) sum[(int)s[i]]++; if (check()) { int l = 1, r = m, mid, ans = m; while (l <= r) { mid = (l + r) >> 1; if (ok(mid)) ans = mid, r = mid - 1; else l = mid + 1; } ok(ans); printf("%d\n", ans); for (int i = ('a'); i <= ('z'); ++i) for (int j = 0; j < (t[i]); ++j) putchar(i), n--; while (n--) putchar('a'); puts(""); } else puts("-1"); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s,n,q,a=input(),int(input()),{},"" for i in s:q[i]=[q[i][0]+1,1]if i in q else [1,1] if len(q)>n:print(-1) else: for i in range(n-len(q)): o=0 for j in q: m=(q[j][0]+q[j][1]-1)//q[j][1] if m>o:o,w=m,j q[w][1]=q[w][1]+1 for i in q:a+=i*q[i][1] o=0 for i in q: m=(q[i][0]+q[i][1]-1)//q[i][1] if m>o:o=m print(o,"\n",a)
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/Stack:256000000") using namespace std; const int INF = (int)1e9; const double EPS = 1e-9; const int MOD = 1e9 + 7; int solve(); int gen(); bool check(); int main() { solve(); return 0; } int solve() { cin.tie(0); cin.sync_with_stdio(0); string s; int n; cin >> s >> n; vector<int> have(256); for (int i = 0; i < s.length(); ++i) have[s[i]] += 1; int cnt = 0; for (int i = 0; i < 256; ++i) { cnt += have[i] ? 1 : 0; } if (cnt > n) { cout << -1 << endl; return 0; } string ans; int L = 0, R = 10000; while (R - L > 1) { int mid = (L + R) / 2; int cnt = 0; for (int i = 0; i < 256; ++i) { if (have[i]) cnt += (have[i] + mid - 1) / mid; } if (cnt > n) L = mid; else R = mid; } for (int i = 0; i < 256; ++i) { for (int j = 0; j < (have[i] + R - 1) / R; ++j) ans += char(i); } while (ans.length() < n) ans += 'z'; cout << R << endl << ans << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; string s; int n; vector<int> cnt; bool comp(int k) { int need = 0; for (int i = 0; i < 30; i++) { if (cnt[i] == 0) continue; need += (cnt[i] - 1) / k + 1; } return need <= n; } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n_case; n_case = 1; while (n_case--) { cin >> s; cin >> n; cnt = vector<int>(30, 0); int diff = 0; for (int i = 0; i < (int)s.size(); i++) cnt[s[i] - 'a']++; for (int i = 0; i < 30; i++) if (cnt[i]) diff++; if (diff > n) { cout << "-1\n"; continue; } int l = 1, r = 1000; while (l < r) { int m = (l + r) / 2; if (comp(m)) r = m; else l = m + 1; } cout << l << "\n"; vector<char> ans; for (int i = 0; i < 30; i++) { if (cnt[i] == 0) continue; int need = (cnt[i] - 1) / l + 1; for (int j = 0; j < need; j++) ans.push_back('a' + i); } while ((int)ans.size() != n) ans.push_back('a'); for (int i = 0; i < n; i++) cout << ans[i]; cout << "\n"; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from collections import Counter from heapq import heappushpop def main(): cnt, n = Counter(input()), int(input()) if n < len(cnt): print(-1) return h = list((1 / v, 1, c) for c, v in cnt.most_common()) res = list(cnt.keys()) _, v, c = h.pop(0) for _ in range(n - len(cnt)): res.append(c) v += 1 _, v, c = heappushpop(h, (v / cnt[c], v, c)) print((cnt[c] + v - 1) // v) print(''.join(res)) if __name__ == '__main__': main()
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.Scanner; public class A335 { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] S = in.next().toCharArray(); int N = in.nextInt(); int[] stat = new int[26]; for (char c : S) { stat[c-'a']++; } int letters = 0; for (int count : stat) { if (count != 0) { letters++; } } if (letters > N) { System.out.println("-1"); return; } int low = 1; int high = S.length; while (low < high) { int mid = (low+high)/2; int onSheet = 0; for (int count : stat) { onSheet += (count + mid - 1)/mid; } if (onSheet > N) { low = mid+1; } else { high = mid; } } StringBuilder sb = new StringBuilder(); for (int i=0; i<26; i++) { char c = (char) ('a' + i); for (int j=0; j<(stat[i] + low - 1)/low; j++) { sb.append(c); } } while (sb.length() < N) { sb.append('x'); } System.out.println(low); System.out.println(sb); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; import javax.sound.midi.Synthesizer; public class Problem5 { static ArrayList<Integer> primes; static void sieve(int N) { boolean[] isComposite = new boolean[N]; primes = new ArrayList<Integer>(N / 10); for(int i = 2; i < N; ++i) if(!isComposite[i]) { primes.add(i); if(1l * i * i < N) for(int j = i * i; j < N; j += i) isComposite[j] = true; } } /* static ArrayList<Pair> primeFactors(int N) *{ ArrayList<Pair> factors = new ArrayList<Pair>(); int idx = 0, p = primes.get(0); while(1l * p * p <= N) { int e = 0; while(N % p == 0) { N /= p; ++e; } if(e != 0) factors.add(new Pair(p, e)); p = primes.get(++idx); } if(N != 1) factors.add(new Pair(N, 1)); return factors; }**/ static double eps= 1e-12; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String s=sc.nextLine(); int n=sc.nextInt(); HashSet<Character> set=new HashSet<>(); char c[]=s.toCharArray(); StringBuilder sb=new StringBuilder(); int d[]=new int[26]; int count[]=new int[26]; PriorityQueue<Pair> pq=new PriorityQueue<Pair>(); TreeMap<Character,Integer> map=new TreeMap<>(); for (int i = 0; i < c.length; i++) { count[c[i]-'a']++; } for (int i = 0; i < c.length; i++) { if(!set.contains(c[i])) { set.add(c[i]); sb.append(c[i]); d[c[i]-'a']++; } } if(set.size()>n) { System.out.println(-1); return; } n=n-set.size(); char c1='a'; for (int i = 0; i < count.length; i++) { if(count[c1-'a']>0) pq.add(new Pair(c1,count[c1-'a'])); c1++; } while(!pq.isEmpty()&&n>0) { Pair p=pq.remove(); sb.append(p.p); d[p.p-'a']++; pq.add(new Pair(p.p,(int) Math.ceil(1.0*count[p.p-'a']/d[p.p-'a']*1.0))); n--; } while(n>0) { sb.append('a'); d[0]++; n--; } int max=1; for (int i = 0; i < d.length; i++) { for (int j = 2; j <=1000; j++) { if(d[i]>=count[i]) { break; } else if(d[i]*j>=count[i]) { max=Math.max(j, max); break; } } } System.out.println(max); System.out.println(sb); /**for(Map.Entry<Character,Integer> entry : map.entrySet()) { char key = entry.getKey(); Integer value = entry.getValue(); }**/ out.flush(); } static class Pair implements Comparable<Pair> { char p;int e; Pair(char x, int y) { p = x; e = y; } @Override public int compareTo(Pair o) { if(this.e<o.e) return 1; else return -1; } } 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();} public boolean nextEmpty() throws IOException { String s = br.readLine(); st = new StringTokenizer(s); return s.isEmpty(); } public void waitForInput() throws InterruptedException {Thread.sleep(2500);} } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { public static void main(String[] args) throws Throwable { // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // BufferedReader br = new BufferedReader(new FileReader("out1")); // PrintWriter out = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileReader("out1")); String s = sc.nextLine(); char[] arr = s.toCharArray(); int n = sc.nextInt(); if(n>=arr.length) { System.out.println(1); StringBuilder sb = new StringBuilder(s); n -= s.length(); while(n-->0) { sb.append(s.charAt(0)); } System.out.println(sb); return; } int[] count = new int[26]; int total = 0; for (int i = 0; i < arr.length; i++) { if(count[arr[i]-'a']==0) total++; count[ arr[i]-'a' ]++; } if(total>n) { System.out.println(-1); return; } int[] res = new int[26]; for (int i = 0; i < count.length; i++) { if(count[i]>0) { n--; res[i]++; count[i]--; } } while(n>0) { int max = 0; int ind = 0; for (int i = 0; i < count.length && n>0; i++) { if(count[i]>0) { int cur = (int) Math.ceil((1.0*count[i])/res[i]); if(cur>max) { max = cur; ind = i; } } } res[ind]++; count[ind]--; n--; } int ans = 0; count = new int[26]; for(int i = 0;i<s.length();i++) { count[ arr[i]-'a' ]++; } for (int i = 0; i < count.length; i++) { ans = Math.max(ans, (int)Math.ceil((1.0*count[i])/res[i])); } System.out.println(ans); for (int i = 0; i < res.length; i++) { while(res[i]-->0) { System.out.print((char)(i+'a')); } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(FileReader r){ br = new BufferedReader(r);} 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
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; public class sheets{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int n = sc.nextInt(); int low =0; int high=100000; TreeMap<Character,Integer> map = new TreeMap<>(); for(int i=0;i<s.length();i++){ map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1); } int size = map.size(); if(n<size){ System.out.println("-1"); return; } while(low<high){ int mid = (low+high)/2; int sum=0; for (Map.Entry<Character,Integer> entry : map.entrySet()){ sum+=Math.ceil((double)entry.getValue()/mid); } if(sum>n){ low=mid+1; } else{ high=mid; } } System.out.println(high); String ans=""; for(Map.Entry<Character,Integer> entry : map.entrySet()){ char[] t = new char[(int)Math.ceil((double)entry.getValue()/high)]; Arrays.fill(t,entry.getKey()); String k = new String(t); ans+=k; } if(ans.length()<n){ while(ans.length()<n){ ans=ans+"a"; } } System.out.println(ans); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> long i, j, k, x, r, y, n, f[150], z, R; char s[1002], a[1002]; int main() { scanf("%s", s); scanf("%ld", &n); for (i = 0; s[i]; i++) f[s[i]]++; for (i = 'a'; i <= 'z'; i++) if (f[i]) j++; if (j > n) { printf("-1\n"); return 0; } x = 1; y = 1000000; dos: z = (x + y) / 2; r = 0; for (i = 'a'; i <= 'z'; i++) { r += f[i] / z; if (f[i] % z != 0) r++; } if (r <= n) { R = z; if (x != y) { y = z - 1; if (y > 0) goto dos; } } else { x = z + 1; if (x <= y) goto dos; } k = 0; for (j = 0, i = 'a'; i <= 'z'; i++) { k += f[i] / R; if (f[i] % R != 0) k++; while (j < k) { a[j] = i; j++; } } while (j < n) { a[j] = 'a'; j++; } printf("%ld\n%s\n", R, a); }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; int n; string s; int cnt[2100]; bool check(int m) { int x = n; for (int i = ('a'), _b = ('z'); i <= _b; i++) { x -= cnt[i] / m + (cnt[i] % m > 0); } return x >= 0; } int find() { int l = 1, r = 1000, res = 0; while (l <= r) { int m = (l + r) / 2; if (check(m)) { res = m; r = m - 1; } else l = m + 1; } return res; } void process(int d) { if (d == 0) { cout << -1 << endl; return; } cout << d << endl; for (int i = ('a'), _b = ('z'); i <= _b; i++) { int x = cnt[i] / d + (cnt[i] % d > 0); for (int j = 0, _n = (x); j < _n; j++) cout << (char)i; n -= x; } for (int i = 0, _n = (n); i < _n; i++) cout << 'a'; } int main() { cin >> s >> n; for (int i = 0, _n = (s.length()); i < _n; i++) { cnt[s[i]]++; } int d = find(); process(d); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int cnt[27]; int num[27]; int main() { string s; int k, mx; int ans; int n; string t; int m; cin >> s >> n; for (int i = 0; i < s.size(); ++i) { ++cnt[s[i] - 'a']; } m = 0; for (int i = 0; i < 26; ++i) { if (cnt[i]) ++m; } if (n < m) { puts("-1"); } else { t = ""; for (int i = 0; i < 26; ++i) { if (cnt[i]) { t += (char)('a' + i); ++num[i]; } } n -= m; while (n--) { double tem = 0; double x; for (int i = 0; i < 26; ++i) { if (cnt[i]) { x = (cnt[i] * 1.0 / num[i]); if (x > tem) { tem = x; ans = i; } } } t += (char)('a' + ans); ++num[ans]; } mx = 0; for (int i = 0; i < 26; ++i) { if (cnt[i]) { mx = max(mx, cnt[i] / num[i] + (0 == cnt[i] % num[i] ? 0 : 1)); } } cout << mx << endl; cout << t << endl; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; long long i, j, n, c[1223456], d[11], b[223465], a[456123], e, l, r, s, t, tt, k, x, y, z, m; string p, q, du, qq; pair<long long, long long> u[132456], w[123456]; int main() { cin >> p >> n; l = p.size(); for (i = 0; i < l; i++) { if (a[p[i] - 'a'] == 0) { s++; } a[p[i] - 'a']++; } if (s > n) { cout << -1; return 0; } for (i = 1; i < 12344; i++) { t = 0; for (j = 0; j < 26; j++) t += (i - 1 + a[j]) / i; if (t <= n) { cout << i << endl; for (j = 0; j < 26; j++) { x = (i - 1 + a[j]) / i; for (k = 0; k < x; k++) cout << char(j + 'a'); } while (t < n) { t++; cout << 'a'; } return 0; } } cout << (n + 2) / 3; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; long long prr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499}; long long powmod(long long a, long long b, long long modulo) { if (b == 0 || a == 1) return 1; long long half = powmod(a, (b / 2), modulo) % modulo; long long full = (half * half) % modulo; if (b % 2) return (full * a) % modulo; return full % modulo; } long long invmod(long long a, long long modulo) { long long check = powmod(a, modulo - 2, modulo) % modulo; return check; } 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; } bool isPrime[5000005]; int prime[5000005]; void sieve(long long N) { for (long long i = 0; i <= N; ++i) { isPrime[i] = true; } isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= N; ++i) { if (isPrime[i] == true) { for (int j = i * i; j <= N; j += i) isPrime[j] = false; } } int j = 0; for (int i = 2; i <= N; i++) { if (isPrime[i]) { prime[j] = i; j++; } } } bool palindrome(string s) { for (long long i = 0; i < s.length() / 2; i++) { if (s[i] != s[s.length() - i - 1]) return 0; } return 1; } void solve() { string s; cin >> s; int n; cin >> n; map<char, int> m; for (char ch : s) m[ch]++; if (m.size() > n) { cout << -1 << "\n"; return; } else { string res = ""; map<char, int> m1; for (auto it : m) { res += it.first; m1[it.first]++; } priority_queue<pair<int, char>> pq; for (auto it : m) { pq.push({it.second, it.first}); } int rem = n - res.size(); while (rem > 0) { int temp = pq.top().first; char ch = pq.top().second; m1[ch]++; res += ch; pq.pop(); if (m[ch] % m1[ch] > 0) { pq.push({m[ch] / m1[ch] + 1, ch}); } else { pq.push({m[ch] / m1[ch], ch}); } rem--; } int count = pq.top().first; cout << count << "\n"; cout << res << "\n"; } } int main() { clock_t start, end; start = clock(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t = 1; while (t--) { solve(); } end = clock(); double time = double(end - start) / (double)(CLOCKS_PER_SEC); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; string s; int num[1500]; int lets; int n, len; int l, r, m; bool ok(int x) { int cur = 0; for (char c = 'a'; c <= 'z'; c++) if (num[c] > 0) { if (num[c] % x == 0) cur += num[c] / x; else cur += (num[c] / x) + 1; } if (cur > n) return false; else return true; } int main() { getline(cin, s); scanf("%d", &n); len = (int)s.length(); for (int i = 0; i < len; i++) { if (num[s[i]] == 0) lets++; num[s[i]]++; } if (lets > n) { printf("-1"); return 0; } l = 1; r = 1000; while (l < r) { m = (l + r) / 2; if (ok(m)) r = m; else l = m + 1; } printf("%d\n", l); int cur = 0; for (char c = 'a'; c <= 'z'; c++) if (num[c] > 0) { int add = 0; if (num[c] % l == 0) add = num[c] / l; else add = (num[c] / l) + 1; cur += add; for (int j = 0; j < add; j++) printf("%c", c); } while (cur < n) { printf("a"); cur++; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; public class A { static int result = -1; public static int ceiling(int n,int m){ int r = n % m; if(r == 0)return n/m; return n / m +1; } /** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); String str = s.nextLine(); int n = s.nextInt(); final HashMap charToCount = new HashMap<>(); ArrayList ele = new ArrayList<>(); int totalCount = 0; for (char c : str.toCharArray()) { totalCount++; if (charToCount.containsKey(c)) { int oldCount = (int) charToCount.get(c); charToCount.put(c, oldCount + 1); } else { ele.add(c); charToCount.put(c, 1); } } Collections.sort(ele, new Comparator<Character>() { @Override public int compare(Character o1, Character o2) { // TODO Auto-generated method stub if ((int) charToCount.get(o1) > (int) charToCount.get(o2)) return -1; else if ((int) charToCount.get(o1) == (int) charToCount.get(o2)) return 0; return 1; } }); if (ele.size() > n) { System.out.println(-1); } else { int min = ceiling(totalCount, n); int max = (int) charToCount.get(ele.get(0)); String r = solve(min, max, ele, charToCount, n); System.out.println(result); System.out.println(r); } s.close(); } private static String solve(int min, int max, ArrayList ele, HashMap charToCount, int n) { // TODO Auto-generated method stub if(min == max){ result=min; return func(min,n,ele,charToCount); } else{ int mid = (min + max) / 2; if(func(mid,n,ele,charToCount) == null) return solve(mid+1,max,ele,charToCount, n); else{ return solve(min,mid,ele,charToCount, n); } } } private static String func(int group, int n, ArrayList ele, HashMap charToCount) { // TODO Auto-generated method stub StringBuffer sb=new StringBuffer(); for(Object c: ele){ int count=(int)charToCount.get((char)c); int num=ceiling(count,group); while(num!=0){ sb.append((char)c); n--; num--; } if(n < 0)return null; } while(n--!=0)sb.append('a'); return sb.toString(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; public class A { BufferedReader in; StringTokenizer st; PrintWriter out; String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } void solve() throws Exception { String s = next(); int q = nextInt(); int cnt[] = new int[666]; for (char ch : s.toCharArray()) { cnt[ch - 'a']++; } int max = 1005; String ans[] = new String[max]; for (int n = 1; n < max; n++) { StringBuilder c = new StringBuilder(); for (int i = 0; i < 26; i++) { for (int j = 0; j < (cnt[i] + n - 1) / n; j++) c.append((char) ('a' + i)); } String ca = c.toString(); if (ca.length() <= q) { out.println(n); out.print(ca); for (int i = 0; i < q - ca.length(); i++) out.print('z'); return; } } out.println(-1); } void run() { try { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public static void main(String[] args) { new A().run(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class Banana { public static boolean can(int []a,int n,int len) { int needed=0; for(int x:a) needed+=((x+n-1)/n); if(len<needed) return false; return true; } public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); String s=sc.nextLine();int n=sc.nextInt(); int []count =new int[26]; for(char c:s.toCharArray()) count[c-'a']++; int distinct=0; for(int x:count) if(x>0) distinct++; if(distinct>n) { System.out.println(-1); return; } int lo=1; int ans=0; int hi=1000; while(lo<=hi) { int mid=(lo+hi)/2; if(can(count,mid,n)) { ans=mid; hi=mid-1; } else lo=mid+1; } int left=n; System.out.println(ans); for(int j=0;j<26;j++) { int x=count[j];char c=(char)('a'+j); int rep=(x+ans-1)/ans; for(int i=0;i<rep;i++) { System.out.print(c); left--; } } while(left-->0) System.out.print('a'); } } 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
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; import java.io.*; public class CodeForces { public static void main(String[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); int n = Integer.parseInt(in.readLine()); numStamp(s, n); } public static void numStamp(String s, int n) { int[] letters = new int[26]; int num_letters = 0; for (int i = 0; i < s.length(); i++) { if (letters[s.charAt(i) - 'a'] == 0) num_letters++; letters[s.charAt(i) - 'a']++; } if (num_letters > n) { System.out.println(-1); return; } int[] ratios = new int[26]; double[] used = new double[26]; StringBuilder stamp = new StringBuilder(""); for (int letter = 0; letter < 26; letter++) { ratios[letter] = letters[letter]; if (letters[letter] > 0) { used[letter]++; stamp.append( (char) (letter + 'a')); } } for (int space = num_letters; space <= n; space++) { int maxRatio = 0; int maxInd = -1; for (int letter = 0; letter < 26; letter++) { if (ratios[letter] > maxRatio) { maxRatio = ratios[letter]; maxInd = letter; } } if (space == n) { System.out.println(maxRatio); System.out.println(stamp.toString()); } used[maxInd]++; stamp.append((char) (maxInd + 'a')); ratios[maxInd] = (int) Math.ceil(letters[maxInd] / used[maxInd]); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int charcount[26]; int res[26]; int n, tempcount, tnum; char c; int main() { for (int i = 0; i < 1024; ++i) { scanf("%c", &c); if (!(c - 'a' >= 0) && (c - 'a' < 26)) { break; } ++charcount[c - 'a']; } scanf("%d", &n); for (int i = 0; i < 26; ++i) if (charcount[i] != 0) ++tnum; if (tnum > n) return printf("-1"), 0; for (int i = 1; i < 1001; ++i) { tempcount = 0; for (int j = 0; j < 26; ++j) { tempcount += res[j] = (charcount[j] + i - 1) / i; } if (tempcount <= n) { printf("%d\n", i); for (int j = 0; j < 26; ++j) for (int k = 0; k < res[j]; ++k) printf("%c", j + 'a'); for (int j = tempcount; j < n; ++j) printf("a"); printf("\n"); return 0; } } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; template <typename T> inline string toString(T a) { ostringstream os(""); os << a; return os.str(); } template <typename T> inline long long toLong(T a) { long long res; istringstream os(a); os >> res; return res; } template <typename T> inline T SQ(T a) { return a * a; } template <typename T> inline T GCD(T a, T b) { if (b == 0) return a; else return GCD(b, a % b); } template <typename T> inline unsigned long long BIGMOD(T a, T b, T m) { if (b == 0) return 1; else if (b % 2 == 0) return SQ(BIGMOD(a, b / 2, m)) % m; else return (a % m * BIGMOD(a, b - 1, m)) % m; } template <typename T> inline vector<string> PARSE(T str) { vector<string> res; string s; istringstream os(str); while (os >> s) res.push_back(s); return res; } template <typename T> inline unsigned long long DIST(T A, T B) { unsigned long long res = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); return res; } template <typename T> inline double cosAngle(T a, T b, T c) { double res = a * a + b * b - c * c; res = res / (2 * a * b); res = acos(res); return res; } template <typename T> inline T POWER(T base, int po) { T res = 1; if (base == 0) return 0; for (int i = (0); i < (po); i++) res *= base; return res; } template <typename T> inline bool IS_ON(T mask, T pos) { return mask & (1 << pos); } template <typename T> inline int OFF(T mask, T pos) { return mask ^ (1 << pos); } template <typename T> inline int ON(T mask, T pos) { return mask | (1 << pos); } template <typename T> inline bool INSIDE_GRID(int R, int C, int ro, int clm) { if (R >= 0 && C >= 0 && R < ro && C < clm) return 1; return 0; } int arr[125]; int taken[130]; int main() { string s, str = ""; cin >> s; int l = s.size(); int n; cin >> n; int len = 0; for (int i = (0); i < (l); i++) { if (arr[s[i] - 'a'] == 0) { str += s[i]; len++; taken[s[i] - 'a']++; } arr[s[i] - 'a']++; } if (str.size() > n) { cout << -1 << endl; return 0; } while (len < n) { int mx = -INT_MAX / 3, ind = -1; for (int i = 0; i < 26; i++) if (taken[i] && arr[i]) { int tmp = (arr[i] / taken[i]); if (arr[i] % taken[i]) tmp++; if (tmp > mx) { mx = arr[i] / taken[i]; ind = i; } } if (ind != -1) { char ch = ('a' + ind); str += ch; len++; taken[ind]++; } } bool state = 1; int res = 0; while (state) { state = 0; for (int i = 0; i < 26; i++) if (arr[i] - taken[i] > 0) { arr[i] -= taken[i]; state = 1; } res++; } cout << res << endl; cout << str << endl; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class A { public static void main(String[] args) throws Exception{ InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); String s = in.nextToken(); int n = in.nextInt(); int[] count = new int[26]; boolean[] appear = new boolean[26]; for(int i = 0 ; i < s.length(); i++){ count[s.charAt(i) - 'a']++; appear[s.charAt(i) - 'a'] = true; } int countApp = 0; for(int i = 0; i < appear.length;i++){ if(appear[i]) countApp++; } if(countApp > n){ out.println(-1); }else{ for(int numSheet = 1; numSheet <= 1000; numSheet++){ int req = 0; int[] counter = new int[26]; for(int ch = 0 ; ch < 26; ch++){ int eachSheet = count[ch] / numSheet; if(eachSheet * numSheet < count[ch]) eachSheet++; counter[ch] = eachSheet; req += eachSheet; } if(req <= n){ out.println(numSheet); StringBuilder sb = new StringBuilder(); for(int ch = 0 ; ch< 26; ch++){ for(int i= 0; i < counter[ch]; i++){ sb.append((char)('a'+ch)); } } while(sb.length() < n) { sb.append('a'); } out.println(sb.toString()); out.close(); return; } } } out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1010; char B[MAXN]; int C[26]; int main() { scanf("%s", B); int l = strlen(B); for (int i = 0; i < l; i++) C[B[i] - 'a']++; int n; scanf("%d", &n); for (int k = 1; k <= l; k++) { int s = 0; for (int i = 0; i < 26; i++) s += (C[i] + k - 1) / k; if (s <= n) { printf("%d\n", k); int q = 0; for (int i = 0; i < 26; i++) for (int j = 0; j < (C[i] + k - 1) / k; j++) { printf("%c", i + 'a'); q++; } for (; q < n; q++) printf("z"); printf("\n"); return 0; } } printf("-1\n"); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { char C[1047]; int n; scanf("%s %d", C, &n); string s = C; vector<int> P, V; P.resize(300, 0); V.resize(300, 0); for (int i = 0; i < (s.length()); i++) P[s[i]]++; int poc = 0; for (int i = 0; i < (300); i++) { if (P[i] == 0) continue; V[i]++; poc++; } if (poc > n) { printf("-1\n"); return 0; } for (int i = 0; i < n - poc; i++) { int naj = -1, maxi = -1; for (int j = 0; j < (300); j++) { if (P[j] == 0) continue; int pot = (P[j] + V[j] - 1) / V[j]; if (pot > maxi) { maxi = pot; naj = j; } } V[naj]++; } int maxi = -1; for (int i = 0; i < (300); i++) { if (P[i] == 0) continue; maxi = max(maxi, (P[i] + V[i] - 1) / V[i]); } printf("%d\n", maxi); for (int i = 0; i < (300); i++) { for (int j = 0; j < (V[i]); j++) printf("%c", i); } printf("\n"); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; map<char, int> mp; for (auto i : s) mp[i]++; for (int i = 1; i <= 1000; i++) { string ans = ""; for (auto c : mp) { int x = ceil(1.0 * c.second / i); for (int i = 0; i < x; i++) ans += c.first; } if (ans.size() <= n) { while (ans.size() < n) ans += 'a'; cout << i << endl << ans; return 0; } } cout << -1; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; public class CF3 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); String s = br.readLine(); int n = Integer.parseInt(br.readLine()); StringBuilder ret = new StringBuilder(); int[] countS = new int['z' - 'a' + 1]; int[] count = new int['z' - 'a' + 1]; for (int i = 0; i < s.length(); i++) { countS[s.charAt(i) - 'a']++; count[s.charAt(i) - 'a'] = 1; } for (int i = 0; i < count.length; i++) { if (count[i] > 0) ret.append((char)(i + 'a')); } while (ret.length() < n) { char maxRatioChar = 'a'; int maxRatio = Integer.MIN_VALUE; for (int i = 0; i < count.length; i++) { if (countS[i] == 0) continue; // 10 / 3 = 3.333 // 10 / 4 = 2.5 // 10 / 5 = 2 int ratio = (int) Math.ceil((double) countS[i] / count[i]); if (ratio > maxRatio) { maxRatio = ratio; maxRatioChar = (char)(i + 'a'); } } count[maxRatioChar - 'a']++; ret.append(maxRatioChar); } if (ret.length() > n) { pw.println(-1); pw.close(); return; } int[] newCount = new int['z' - 'a' + 1]; int j = 0; while (less(newCount, countS)) { for (int i = 0; i < ret.length(); i++) { newCount[ret.charAt(i) - 'a']++; } j++; } pw.println(j); pw.println(ret.toString()); pw.close(); } // Returns whether at least one of arr1's elements is less than arr2's corresponding element private static boolean less(int[] arr1, int[] arr2) { for (int i = 0; i < arr1.length; i++) { if (arr1[i] < arr2[i]) return true; } return false; } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char str[1010]; int n; int cnt[26]; bool check(int x) { int len = 0; for (int i = 0; i < 26; ++i) len += (cnt[i] + x - 1) / x; return len <= n; } int main() { scanf("%s%d", str, &n); for (int i = 0; str[i]; ++i) cnt[str[i] - 'a']++; int c = 0; for (int i = 0; i < 26; ++i) if (cnt[i]) c++; if (c > n) { puts("-1"); return 0; } int l = 1, r = 10000, mid; while (r > l) { mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid + 1; } printf("%d\n", l); int len = 0; for (int i = 0; i < 26; ++i) { for (int j = 0; j < (cnt[i] + l - 1) / l; ++j) putchar('a' + i), len++; } for (int i = 0; i < n - len; ++i) putchar('a'); puts(""); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { int q, w, e, r, t, c, s[26]; char a[1001]; cin >> a >> q; for (w = 0; w < 26; w++) s[w] = 0; for (w = 0; a[w]; w++) s[a[w] - 97]++; w = 1001; e = 512; while (e) { r = w - e; if (r > 0) { t = q; for (c = 0; c < 26; c++) { t -= s[c] / r; if (s[c] % r) t--; } if (t >= 0) w = r; } e /= 2; } if (w == 1001) cout << -1; else { cout << w << "\n"; t = q; for (c = 0; c < 26; c++) { for (e = 0; e < s[c] / w; e++) cout << (char)(c + 97); t -= s[c] / w; if (s[c] % w) { cout << (char)(c + 97); t--; } } for (; t > 0; t--) cout << 'a'; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class C_1 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); char[] s = sc.next().toCharArray(); int[] occ = new int[26]; int[] or = new int[26]; int N = sc.nextInt(); for (int i = 0; i < s.length; i++) { occ[s[i] - 'a']++; or[s[i] - 'a']++; } int d = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < 26; i++) if (occ[i] > 0) { d++; } if (d > N) { System.out.println(-1); } else { int M = 1; boolean found = false; while (!found) { int len = 0; for (int i = 0; i < 26; i++) if (occ[i] > 0) len += (int) Math.ceil(occ[i] * 1.0 / M); if (len <= N) found = true; M++; } if (found) { int ret = M - 1; for (int i = 0; i < 26; i++) if (occ[i] > 0) { int use = (int) Math.ceil(occ[i] * 1.0 / ret); while (use-- > 0) ans.append((char) (i + 'a')); } while(ans.length() < N) ans.append("a"); System.out.println(ret); System.out.println(ans); } else System.out.println(-1); } } 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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Banana { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int n = Integer.parseInt(br.readLine()); int[] countOcc = new int[26]; int distinctCount = 0; for (int i = 0; i < s.length(); i++) { int c = s.charAt(i) - 'a'; if (countOcc[c] == 0) distinctCount++; countOcc[c]++; } if (distinctCount > n) { System.out.println(-1); return; } int sheetsCount = 1; while (true) { int lettersCount = 0; for (int i = 0; i < 26; i++) { lettersCount += Math.ceil((countOcc[i] * 1.0) / (sheetsCount * 1.0)); } if (lettersCount <= n) { System.out.println(sheetsCount); for (int i = 0; i < 26; i++) { int occ = (int) Math.ceil((countOcc[i] * 1.0) / (sheetsCount * 1.0)); char c = (char) (i + 'a'); for (int j = 0; j < occ; j++) System.out.print(c); } for (int i = lettersCount; i < n; i++) { System.out.print('a'); } break; } sheetsCount ++; } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; public class A { void solve() throws IOException { String s = next(); int n = nextInt(); int[] cnt = new int[26]; for (int i = 0; i<s.length(); i++) { cnt[s.charAt(i) - 'a']++; } for (int i = 1; i<=s.length(); i++) { int[] must = new int[26]; int calc = 0; for (int j = 0; j<26; j++) { must[j] = (cnt[j] +i-1)/i; calc+= must[j]; } if (calc <= n) { must[0] += n-calc; out.println(i); for (int j = 0; j<26; j++) { for (int k = 0; k<must[j]; k++) { out.print((char)('a'+j)); } } out.println(); return; } } out.println("-1"); } void run() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new A().run(); } BufferedReader br; PrintWriter out; StringTokenizer str; String next() throws IOException { while (str == null || !str.hasMoreTokens()) { str = new StringTokenizer(br.readLine()); } return str.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int force = 27; const int inf = 1000 * 1000 * 1000; string s; int n, a[force], b[force]; int main() { cin >> s; cin >> n; for (int i = 0; i < force; i++) { a[i] = 0; b[i] = 0; } int dif = 0; string ans = ""; for (int i = 0; i < s.length(); i++) { a[int(s[i]) - int('a')]++; if (a[int(s[i]) - int('a')] == 1) { dif++; ans += s[i]; b[int(s[i]) - int('a')] = 1; } } if (dif > n) { cout << -1; return 0; } int ind, maxcnt, mod; while (n > int(ans.length())) { maxcnt = -1; for (int i = 0; i < force; i++) { if (a[i] != 0) { mod = 0; if (a[i] % b[i] > 0) mod++; if (maxcnt < a[i] / b[i] + mod) { maxcnt = a[i] / b[i] + mod; ind = i; } } } ans += char(ind + int('a')); b[ind]++; } maxcnt = -1; for (int i = 0; i < force; i++) { if (a[i] != 0) { mod = 0; if (a[i] % b[i] > 0) mod++; maxcnt = max(maxcnt, a[i] / b[i] + mod); } } cout << maxcnt << '\n'; cout << ans; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; void solve() { string s; string s1; map<char, double> m1; map<char, double> m2; map<char, double> m3; double temp; char c; long long n, i, max, index; cin >> s; cin >> n; for (i = 0; i < s.size(); i++) { m2[s[i]]++; m1[s[i]]++; } if (m1.size() <= n || n > s.size()) { for (auto x : m1) { x.second--; s1 = s1 + x.first; n--; m3[x.first]++; } while (n != 0) { max = 0; for (auto x : m1) { temp = ceil((m2[x.first]) / (m3[x.first] * 1.0)); if (temp > max) { max = temp; c = x.first; } } s1 = s1 + c; m1[c]--; m3[c]++; n--; } max = -1; for (auto x : m1) { double temp1 = m2[x.first]; double temp2 = m3[x.first]; temp = ceil(temp1 / temp2 * 1.0); if (temp > max) max = temp; } cout << max << endl; cout << s1; } else cout << "-1" << endl; } int main() { solve(); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s = raw_input() n = int(raw_input()) d = dict() for c in s: d[c] = d.get(c, 0) + 1 if len(d.keys()) > n: print -1 else: l = 1 r = len(s) while r - l > 0: m = (r + l) / 2 lst = [] sm = 0 for k in d.keys(): cnt = d[k] / m + (1 if d[k] % m > 0 else 0) #cnt = cnt if cnt > 0 else 1 lst.append((k, cnt)) sm += cnt if sm > n: l = m + 1 elif sm < n: r = m else: r = m #l = m print l lst = [] for k in d.keys(): cnt = d[k] / l + (1 if d[k] % l > 0 else 0) #cnt = cnt if cnt > 0 else 1 lst.append((k, cnt)) res = '' for ll in lst: res += ll[0]*ll[1] dif = n - len(res) if dif > 0: res += s[0] * dif print res
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#!/usr/bin/env python from collections import Counter from operator import itemgetter s = raw_input() n = int(raw_input().strip()) if len(set(s)) > n: print '-1' exit() chars = Counter(s) def generate_paper(n, count, chars): if count <= 0: return '' paper = [] paper_size = n for char, char_count in sorted(chars.iteritems(), key=itemgetter(1), reverse=True): char_count_per_paper = char_count // count if char_count % count != 0: char_count_per_paper += 1 paper_size -= char_count_per_paper if paper_size < 0: return '' paper.append(char * char_count_per_paper) paper = ''.join(paper) gap_size = n - len(paper) if gap_size > 0: paper += paper[0] * gap_size return paper prev_p = '' for count in xrange(1001, -1, -1): p = generate_paper(n, count, chars) if p: prev_p = p else: print count + 1 print prev_p break
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { char name[1050]; int A[100]; int c, n, k, num, v; scanf("%s", name); scanf("%d", &n); fill(A, A + 27, 0); for (int(i) = 0; (i) < (strlen(name)); (i)++) { A[int(name[i] - 'a')]++; } c = 0; for (int(i) = 0; (i) < (26); (i)++) if (A[i] > 0) c++; if (c > n) printf("-1\n"); else { for (int(k) = (1); (k) <= (1000); (k)++) { v = 0; for (int(i) = 0; (i) < (26); (i)++) v += ceil((float)A[i] / k); if (v <= n) { num = k; break; } } printf("%d\n", num); c = 0; for (int(i) = 0; (i) < (26); (i)++) { for (int(j) = 0; (j) < (int(ceil((float)A[i] / num))); (j)++) printf("%c", 'a' + i), c++; } for (int(i) = 0; (i) < (max(0, n - c)); (i)++) printf("%c", 'a'); printf("\n"); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; public class Main { public static class Pair implements Comparable<Pair> { public char c; public int f; public Pair(char c, int f) { this.c = c; this.f = f; } public int compareTo(Pair pair) { if (this.f < pair.f) return 1; if (this.f > pair.f) return -1; if (this.c < pair.c) return -1; if (this.c > pair.c) return 1; return 0; } } public static void main(String[] args) throws Throwable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char[] s = br.readLine().toCharArray(); int n = Integer.parseInt(br.readLine()); int[] f = new int[26]; List<Pair> list = new ArrayList<Pair>(); Arrays.fill(f, 0); for (int i = 0; i < s.length; i++) f[s[i]-'a']++; int cnt = 0; for (int i = 0; i < 26; i++) if (f[i] > 0) { list.add(new Pair((char) ('a' + i), f[i])); cnt++; } Collections.sort(list); if (cnt > n) System.out.println("-1"); else for (int k = 1;; k++) { int need = 0; for (Pair p : list) { char cc = p.c; int ff = p.f; int nn = (p.f + k - 1) / k; need += nn; } if (need <= n) { StringBuilder sb = new StringBuilder(); for (Pair p : list) { char cc = p.c; int ff = p.f; int nn = (p.f + k - 1) / k; for (int i = 0; i < nn; i++) sb.append(cc); } int rem = n - need; for (int i = 0; i < rem; i++) sb.append('z'); System.out.println(k); System.out.println(sb.toString()); break; } } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int n, cnt[255]; string s; bool check(int l) { int nn = n, t; for (char ch = 'a'; ch <= 'z'; ch++) { if (cnt[ch] % l == 0) t = cnt[ch] / l; else t = cnt[ch] / l + 1; nn -= t; if (nn < 0) return 0; } return 1; } string calc(int l) { string ret = ""; int t; for (char ch = 'a'; ch <= 'z'; ch++) { if (cnt[ch] % l == 0) t = cnt[ch] / l; else t = cnt[ch] / l + 1; n -= t; while (t--) ret += ch; } while (n--) ret += 'a'; return ret; } int main() { cin >> s; for (int i = 0; i < s.size(); i++) cnt[s[i]]++; cin >> n; int l = 1, r = 1000, ans = -1; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) { r = mid - 1; ans = mid; } else l = mid + 1; } printf("%d\n", ans); if (ans != -1) cout << calc(ans) << endl; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Banan { public static class Letter implements Comparable<Letter>{ public Letter(char c, int count){ this.letter = c; this.stickCount = 1; this.strοΏ½ount = count; } char letter; int strοΏ½ount; int stickCount; @Override public int compareTo(Letter that) { if(this.strοΏ½ount*that.stickCount > that.strοΏ½ount*this.stickCount) return -1; else return 1; } } public static void main(String[] args) throws IOException{ BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); TreeSet<Letter> set = new TreeSet<Letter>(); int n = Integer.parseInt(reader.readLine()); int l = s.length(); int[] count = new int[26]; for(int i=0; i<l; i++){ char c = s.charAt(i); count[c - 'a']++; } int total = 0; for(int i=0;i<26;i++){ if(count[i]>0){ total++; set.add(new Letter((char)(i+'a'), count[i])); } } if(total > n){ System.out.println(-1); }else{ while(total < n){ Letter lc = set.pollFirst(); lc.stickCount++; total++; set.add(lc); } int k = 0; for(Letter lc:set){ if(((double)lc.strοΏ½ount)/lc.stickCount > k){ k = (int)Math.ceil(((double)lc.strοΏ½ount)/lc.stickCount); } } System.out.println(k); for(Letter lc:set){ for(int i=0;i<lc.stickCount;i++){ System.out.print(lc.letter); } } System.out.println(); } } finally { if(reader != null) reader.close(); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int mas[26]; string s; int main() { int n; cin >> s >> n; for (int i = 0; i < s.length(); i++) mas[s[i] - 'a']++; int mini = 0; for (int i = 0; i < 26; i++) if (mas[i] != 0) mini++; if (mini > n) { cout << -1 << endl; return 0; } for (int col = 1; col <= 1000; col++) { int cnt = 0; for (int j = 0; j < 26; j++) cnt += (mas[j] + col - 1) / col; if (cnt <= n) { string s = ""; for (int j = 0; j < 26; j++) { char c = j + 'a'; int cur = (mas[j] + col - 1) / col; for (int t = 0; t < cur; t++) { s += c; } } while (s.length() < n) s += 'a'; if (s.length() > n) throw 1; cout << col << endl; cout << s << endl; return 0; } } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Locale; import java.util.StringTokenizer; import static java.util.Arrays.deepToString; public class A { static int doit(int[] cnt, int[] have, boolean finish) { for (int i = 0; i < cnt.length; i++) { if (cnt[i] > 0 && have[i] == 0) { if (!finish) have[i]++; return -1; } } int mi = -1, mneed = -1; for (int i = 0; i < cnt.length; i++) { if (cnt[i] > 0) { int need = (cnt[i] + have[i] - 1) / have[i]; if (mi == -1 || need > mneed) { mi = i; mneed = need; } } } if (!finish) have[mi]++; return mneed; } static void solve() { char[] s = next().toCharArray(); int[] cnt = new int[26]; for (char c : s) { cnt[c - 'a']++; } int n = nextInt(); int[] have = new int[26]; for (int i = 0; i < n; i++) { doit(cnt, have, false); } int ans = doit(cnt, have, true); writer.println(ans); if (ans == -1) return; for (int i = 0; i < cnt.length; i++) { for (int j = 0; j < have[i]; j++) { writer.print((char) (i + 'a')); } } } public static void main(String[] args) throws Exception { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); Locale.setDefault(Locale.US); setTime(); solve(); printTime(); printMemory(); writer.close(); } static BufferedReader reader; static PrintWriter writer; static StringTokenizer tok = new StringTokenizer(""); static long systemTime; static void debug(Object... o) { System.err.println(deepToString(o)); } static void setTime() { systemTime = System.currentTimeMillis(); } static void printTime() { System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime)); } static void printMemory() { System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb"); } static String next() { while (!tok.hasMoreTokens()) { String w = null; try { w = reader.readLine(); } catch (Exception e) { e.printStackTrace(); } if (w == null) return null; tok = new StringTokenizer(w); } return tok.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static BigInteger nextBigInteger() { return new BigInteger(next()); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; import java.io.*; import java.nio.*; import java.nio.channels.*; public class Main { static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter out = new PrintWriter(Channels.newWriter(Channels.newChannel(System.out), "US-ASCII") ); static int CHARS = 'z'-'a' +1; public static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public static String next() { try { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer(reader.readLine() ); } } catch (Exception e) { } return tokenizer.nextToken(); } public static Integer nextInt() { Integer ret = null; try { ret = Integer.parseInt(next()); } catch (Exception e) { } return ret; } public static int count(int[] counter, String s) { int count = 0; for (char c: s.toCharArray()) { counter[c-'a']++; } for (int c: counter) { if (c > 0) { count++; } } return count; } public static int calc(int times, int[] counter, int[] needs) { int need; int sum=0; for (int i=0; i<counter.length; i++) { need = counter[i]/times; need += (counter[i] % times > 0) ? 1 : 0; needs[i] = need; sum += need; } return sum; } public static void log(Object s) { //System.out.println(s.toString()); } public static int search(int[] counter, int n, String s) { int[] needs = new int [CHARS]; int calc; int hi = s.length(); int lo = 1; int mid; while (lo <= hi) { mid = lo + (hi-lo)/2; log("lo: " + lo + ", mid: " + mid + ", hi: " + hi); calc = calc(mid, counter, needs); log(calc); //if (calc == n || ((lo==hi) && (mid==lo))) { if ((lo==hi) && (mid==lo)) { return mid; } else if(calc <= n) { hi = mid; } else { lo = mid+1; } } return -1; } public static String stick(int[] counter, int n) { StringBuffer res = new StringBuffer(); Character first = null; int size=0; for (int i=0; i<counter.length; i++) { if (counter[i] > 0) { char c = (char)(i + 'a'); if (first == null) { first = c; } for (int j=0; j<counter[i]; j++) { res.append(c); size++; } } } log("size:" + size + " n:" + n); if (size < n) { for (int i=1; i<= n-size; i++) { res.append(first); } } return res.toString(); } public static void main (String args[]) { init(System.in); int[] counter = new int[CHARS]; String s = next(); int n = nextInt(); int different = count(counter, s); if (different > n) { out.println(-1); } else { int res = search(counter, n, s); int[] needs = new int[CHARS]; int total = calc(res, counter, needs); String word = stick(needs, n); out.println(res); out.println(word); } out.close(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char s[2000]; int n; map<char, int> counter; map<char, int> repeat; int main() { scanf("%s", s); scanf("%d", &n); int len = strlen(s); for (int i = 0; i < len; i++) { counter[s[i]] += 1; repeat[s[i]] = 1; } if (n < counter.size()) { puts("-1"); return 0; } for (int i = 0; i < n - counter.size(); i++) { int maxv = 0; char maxk; for (map<char, int>::iterator it = counter.begin(); it != counter.end(); it++) { int h = ceil((double)it->second / repeat[it->first]); if (h > maxv) { maxv = h; maxk = it->first; } } repeat[maxk]++; } int maxh = 0; char res[2000] = ""; for (map<char, int>::iterator it = counter.begin(); it != counter.end(); it++) { int h = ceil((double)it->second / repeat[it->first]); if (h > maxh) { maxh = h; } for (int i = 0; i < repeat[it->first]; i++) sprintf(res + strlen(res), "%c", it->first); } printf("%d\n", maxh); printf("%s\n", res); }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≀ |s| ≀ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≀ n ≀ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { static StringTokenizer st; static BufferedReader in; static PrintWriter pw; static int[]cnt; static int L; static int[]leng; static int n; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); String s = next(); L = s.length(); n = nextInt(); cnt = new int[26]; for (int i = 0; i < s.length(); i++) { cnt[s.charAt(i)-97]++; } leng = new int[26]; int number = 0; for (int i = 0; i < 26; i++) { if (cnt[i] > 0) number++; } if (number > n) { System.out.println(-1); return; } int left = 0, right = (int) 1e9; while (left <= right) { int middle = (left+right) >> 1; if (possible(middle) && !possible(middle-1)) { possible(middle); System.out.println(middle); int t = 0; for (int i = 0; i < 26; i++) { for (int j = 1; j <= leng[i]; j++) { System.out.print((char)(i+97)); t++; } } for (int i = t+1; i <= n; i++) { System.out.print('a'); } System.out.println(); return; } if (!possible(middle)) left = middle+1; else right = middle-1; } pw.close(); } private static boolean possible(int s) { if (s==0) return false; leng = new int[26]; int L = 0; for (int i = 0; i < 26; i++) { int x = cnt[i] / s; if (cnt[i] % s != 0) x++; leng[i] = x; L += x; } return L <= n; } private static int nextInt() throws IOException{ return Integer.parseInt(next()); } private static long nextLong() throws IOException{ return Long.parseLong(next()); } private static double nextDouble() throws IOException{ return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) lst = list(map(int,input().split())) a = list(zip(lst,lst[1:])) record = [] for x in a: s,e = min(x[0],x[1]),max(x[0],x[1]) for y in record: if y[0]<s<y[1]<e or s<y[0]<e<y[1]: exit(print("yes")) record.append((s,e)) print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) a = [int(x) for x in input().split()] x = [] for i in range(n - 1): x.append(sorted([a[i],a[i + 1]])) #print(x) for i in range(n - 1): for j in range(i + 2, n - 1): if (x[i][0] > x[j][0] and x[i][0] < x[j][1] and (x[i][1] > x[j][1] or x[i][1] < x[j][0])) or (x[i][1] > x[j][0] and x[i][1] < x[j][1] and (x[i][0] > x[j][1] or x[i][0] < x[j][0])): print('yes') exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[1005]; int i, j; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < n - 1; i++) { int l = a[i], r = a[i + 1]; if (l > r) swap(l, r); for (j = 0; j < n - 1; j++) { int l1 = a[j], r1 = a[j + 1]; if (l1 > r1) swap(l1, r1); if ((r1 < r && l > l1 && l < r1) || (r < r1 && l1 > l && l1 < r) || (l1 < l && r1 > l && r1 < r) || (l < l1 && r > l1 && r < r1)) { cout << "yes" << endl; return 0; } } } cout << "no" << endl; } return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n=int(input()) l=[int(x) for x in input().split()] a=[] for i in range(n-1): a.append((min(l[i], l[i+1]), max(l[i], l[i+1]))) for i in a: for j in a: if i[0]<j[0]<i[1]<j[1] or j[0]<i[0]<j[1]<i[1]: print("yes") exit() print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int l, r, ll, rr, n, arr[1009]; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i + 1 < n; i++) { l = min(arr[i], arr[i + 1]); r = max(arr[i], arr[i + 1]); for (int j = i + 2; j + 1 < n; j++) { ll = min(arr[j], arr[j + 1]); rr = max(arr[j], arr[j + 1]); if ((ll < r && ll > l && rr > r) || (l < rr && l > ll && r > rr)) { cout << "yes" << '\n'; return 0; } } } cout << "no" << '\n'; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import sys input = lambda: sys.stdin.readline().strip("\r\n") n = int(input()) ls = list(map(int, input().split())) for i in range(n-1): for j in range(n-1): w, x = sorted([ls[i], ls[i+1]]) y, z = sorted([ls[j], ls[j+1]]) if w < y < x < z or y < w < z < x: print('yes') exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1030; int x[MAXN]; int main() { int n; cin >> n; for (int i = 0; i < (n); ++i) cin >> x[i]; for (int i = 0; i < (n - 1); ++i) { for (int j = i + 2; j <= (n - 2); ++j) { int a(x[i]), b(x[i + 1]); int c(x[j]), d(x[j + 1]); if (a > b) swap(a, b); if (c > d) swap(c, d); if (a < c && c < b && b < d) { cout << "yes" << endl; return 0; } if (c < a && a < d && d < b) { cout << "yes" << endl; return 0; } } } cout << "no" << endl; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) seq = list(map(int, input().split())) if n == 1: print("no") exit(0) pairs = [] cur = [-1, seq[0]] fail = False for i in range(1, len(seq)): cur[0], cur[1] = cur[1], seq[i] pairs.append([min(cur), max(cur)]) for x1, x2 in pairs: if not fail: for x3, x4 in pairs: if (x1 != x3 or x2 != x4) and (x1 < x3 < x2 < x4 or x3 < x1 < x4 < x2): fail = True break else: break if fail: print("yes") else: print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; bool compare(const pair<int, int>& i, const pair<int, int>& j) { return i.first < j.first; } void solve() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n]; for (int i1 = 0; i1 < n; i1++) { cin >> a[i1]; } for (int i1 = 0; i1 < n - 1; i1++) { for (int i2 = 0; i2 < n - 1; i2++) { int x1 = max(a[i1], a[i1 + 1]), x2 = min(a[i1], a[i1 + 1]), y1 = max(a[i2], a[i2 + 1]), y2 = min(a[i2], a[i2 + 1]); swap(x1, x2); swap(y1, y2); if (y1 >= x1 && y1 <= x2 && y2 >= x1 && y2 <= x2) { } else if (y1 <= x1 && y2 >= x2) { } else if (y1 <= x1 && y2 <= x1) { } else if (y1 >= x2 && y2 >= x2) { } else { cout << "yes"; return; } } } cout << "no"; return; } int main() { int t; t = 1; while (t--) { solve(); cout << "\n"; } return 0; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int a[1111]; int main() { int n, i, j; int themax1, themin1; int themax2, themin2; int flag = 0; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; if (n <= 2) flag = 0; else { for (i = 2; i < n - 1; i++) { themax2 = max(a[i], a[i + 1]); themin2 = min(a[i], a[i + 1]); for (j = 0; j + 1 < i; j++) { themax1 = max(a[j], a[j + 1]); themin1 = min(a[j], a[j + 1]); if (themin1 < themin2 && themin2 < themax1 && themax1 < themax2) flag = 1; if (themin2 < themin1 && themin1 < themax2 && themax2 < themax1) flag = 1; } } } if (flag) cout << "yes" << endl; else cout << "no" << endl; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; /** * Created by tdph5945 on 2016-06-18. */ public class DimaAndContinuousLine { public static void main(String... args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(), first, second, lfirst, lsecond; int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } String res = "no"; outer: for (int i = 0; i < n - 1; i++) { first = Math.min(arr[i], arr[i + 1]); second = Math.max(arr[i], arr[i + 1]); for (int j = 0; j < i; j++) { lfirst = Math.min(arr[j], arr[j + 1]); lsecond = Math.max(arr[j], arr[j + 1]); if ((first < lfirst && lfirst < second && second < lsecond) || (lfirst < first && lsecond < second && first < lsecond)) { res = "yes"; break outer; } } } System.out.println(res); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) seq = list(map(int, input().split())) if n == 1: print("no") exit(0) pairs = [] cur = [-1, seq[0]] fail = False for i in range(1, len(seq)): cur[0], cur[1] = cur[1], seq[i] if cur[1] < cur[0]: pairs.append([cur[1], cur[0]]) else: pairs.append(cur[:]) #print(pairs) for x1, x2 in pairs: if not fail: for x3, x4 in pairs: if (x1 != x3 or x2 != x4) and (x1 < x3 < x2 < x4 or x3 < x1 < x4 < x2): fail = True break else: break if fail: print("yes") else: print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; public class DimaAndContinuousLine { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; i++){ arr[i] = sc.nextInt(); } boolean flag = false; for(int i=0; i<n-1; i++){ int x1 = arr[i]; int x2 = arr[i+1]; if(x1>x2){ int temp = x1; x1 = x2; x2 = temp; } for(int j=0; j<n-1; j++){ int x3 = arr[j]; int x4 = arr[j+1]; if(x3>x4){ int temp = x3; x3 = x4; x4 = temp; } if(x1<x3 && x3<x2 && x2<x4){ flag = true; break; } if(flag) break; } } if(flag) System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n,l=int(input()),list(map(int,input().split())) for i in range(n-1): for j in range(n-1): a,b,c,d=sorted([l[i],l[i+1]])+sorted([l[j],l[j+1]]) if a<c<b<d: exit(print('yes')) print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; /** * Created with IntelliJ IDEA. * User: samir * Date: 10/25/13 * Time: 11:46 PM * To change this template use File | Settings | File Templates. */ public class A { public static boolean intersects(int[] n) { for(int i = 1; i < n.length; ++i) { int seg0_start = n[i - 1]; int seg0_end = n[i]; if(seg0_end < seg0_start) { int t = seg0_end; seg0_end = seg0_start; seg0_start = t; } for(int j = i + 1; j < n.length; ++j) { int seg1_start = n[j - 1]; int seg1_end = n[j]; if(seg1_end < seg1_start) { int t = seg1_end; seg1_end = seg1_start; seg1_start = t; } if(seg1_start > seg0_start && seg1_start < seg0_end && seg1_end > seg0_end) return true; if(seg1_end > seg0_start && seg1_end < seg0_end && seg1_start < seg0_start) return true; } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] n = new int[N]; for(int i = 0; i < N; ++i) n[i] = sc.nextInt(); System.out.println(intersects(n) ? "yes" : "no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) lst = list(map(int,input().split())) for i in range(n-1): for j in range(i+1,n-1): # Check if the start of the first semi circle is smaller than the second one # and the end of the frist semi circle is also small the second one # It it's true then there is an intersection a = min(lst[i], lst[i+1]) b = max(lst[i], lst[i+1]) c = min(lst[j], lst[j+1]) d = max(lst[j], lst[j+1]) if a < c < b < d or c < a < d < b: print("yes") exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n=int(input()) arr=[int(i) for i in input().split()] if n==2 or n==1: print("no") exit() l=[[arr[0],arr[1]],[arr[1],arr[2]]] for i in range(2,n-1): for j in range(i): x= abs(abs(l[j][0]-l[j][1])/2 - abs(arr[i]-arr[i+1])/2) < abs((l[j][0]+l[j][1])/2 - (arr[i]+arr[i+1])/2 ) < abs(l[j][0]-l[j][1])/2 + abs(arr[i]-arr[i+1])/2 if x: print("yes") exit() l.append([arr[i],arr[i+1]]) print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
''' Created on Oct 31, 2013 @author: Ismael ''' def main(): input() l = list(map(int,input().split())) #l = [0,10,5,15] #l = [0,15,5,10] if(len(l)<=3): print("no") return xMin = min(l[0],l[1]) xMax = max(l[0],l[1]) for i in range(2,len(l)-1): if(l[i]<=xMin or l[i]>=xMax):#3eme ext, next point must be outside [xMin,xMax] if(l[i+1]>xMin and l[i+1]<xMax): print("yes") return elif(l[i]>=xMin and l[i]<=xMax):#3eme int, next point must be inside [xMin,xMax] if(l[i+1]<xMin or l[i+1]>xMax): print("yes") return min123 = min(xMin,l[i]) max123 = max(xMax,l[i]) if(l[i+1]<min123 or l[i+1]>max123): xMin = min123 xMax = max123 else: elems = [l[i+1],xMin,xMax,l[i]] elems.sort() ind4 = elems.index(l[i+1]) xMin = elems[ind4-1] xMax = elems[ind4+1] print("no") main()
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) x = [int(x) for x in input().split()] list11 = [] ans = 'no' for i in range(n-1): if x[i]<x[i+1]: list11.append([x[i],x[i+1]]) else: list11.append([x[i+1],x[i]]) list1 = sorted(list11,key=lambda x:x[1]) for i in range(len(list1)): for e in range(i,len(list1)): if i==e: continue if list1[i][0] < list1[e][0] and list1[i][1] > list1[e][0] and list1[i][0] < list1[e][1] and list1[i][1] < list1[e][1]: ans = 'yes' break print(ans)
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public String isIntersection(int n, int[] points) { if (n == 0 || n == 1) return "no"; int temp; for (int i = 0; i < points.length-1; i++) { int x1=points[i]; int x2=points[i+1]; for (int j = 0; j <points.length-1; j++) { if(j==i) continue; int a1=points[j]; int a2=points[j+1]; if(a1>a2){ temp=a2; a2=a1; a1=temp; } if(x1>x2){ temp=x2; x2=x1; x1=temp; } if(x1<a1 && a1<x2 && a2>x2 ) return "yes"; if(x1<a2 && a2<x2 && a2>x2 ) return "yes"; } } return "no"; } public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader flujoE = new BufferedReader(isr); String line = flujoE.readLine(); int n = Integer.parseInt(line); line = flujoE.readLine(); String[] pointsString = line.split(" "); int[] points = new int[n]; for (int i = 0; i < pointsString.length; i++) { points[i] = Integer.parseInt(pointsString[i]); } Main helpDima = new Main(); System.out.println(helpDima.isIntersection(n, points)); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; public class DimaContinousLine { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = sc.nextInt(); } boolean inter = false; for (int i = 0; i < ar.length; i++) { for (int j = i + 2; j < ar.length; j++) { if (j < ar.length - 1) { int min1, max1, min2, max2; if (ar[i] < ar[i + 1]) { min1 = ar[i]; max1 = ar[i + 1]; } else { min1 = ar[i + 1]; max1 = ar[i]; } if (ar[j] < ar[j + 1]) { min2 = ar[j]; max2 = ar[j + 1]; } else { min2 = ar[j + 1]; max2 = ar[j]; } if (min1 < min2) { if (max1 < max2 && max1 > min2) { inter = true; } } else { if (max2 < max1 && max2 > min1) { inter = true; } } } } } System.out.println(inter ? "yes" : "no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
def intersect(p1,p2): if (p2[0] < p1[1] and p2[0] > p1[0] and p2[1] > p1[1]) or (p2[1] < p1[1] and p2[1] > p1[0] and p2[0] < p1[0]): return True return False def check(points): for x in range(len(points)): for i in range(x + 1,len(points)): if intersect(points[x],points[i]): return "yes" return "no" n = int(input()) temp_in = [int(x) for x in input().split(' ')] points = [] for x in range(n - 1): points.append((min(temp_in[x],temp_in[x + 1]),max(temp_in[x],temp_in[x + 1]))) #print(points) print(check(points)) #chyba przypadek z tym ze wspolrzedne pozniej sa mniejsze i wtedy warunek nie wchodzi na przeciecie ''' 2 0 15 13 20 ans: yes semicircles will intersect with each other in one case: begininning of the second one will be earlier than ending of the first. and there will be no subcircle (circle which is included in the one above) brute force O(N^2+N / 2) '''
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int const Max = 1111; int n; int x[Max]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> x[i]; bool ans = true; for (int i = 1; i < n; i++) { bool f1 = false; bool f2 = false; for (int j = i + 2; j <= n; j++) { int Min, Max; Min = min(x[i], x[i + 1]); Max = max(x[i], x[i + 1]); if (Min < x[j] && x[j] < Max) f1 = true; else f2 = true; } if (f1 && f2) ans = false; } if (ans) cout << "no" << endl; else cout << "yes" << endl; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int l = INT_MAX; int h = INT_MIN; string ans = "no"; if (n > 3) { vector<pair<int, int>> arr; int a, b; cin >> a >> b; if (a < b) arr.push_back({a, b}); else arr.push_back({b, a}); for (int i = 3; i <= n; i++) { int c; cin >> c; if (b < c) arr.push_back({b, c}); else arr.push_back({c, b}); b = c; } for (int i = 0; i < (int)arr.size(); i++) { for (int j = i + 1; j < (int)arr.size(); j++) { pair<int, int> f = arr[i]; pair<int, int> s = arr[j]; if ((f.first < s.first && f.second > s.first && f.second < s.second) || (f.first > s.first && f.first < s.second && f.second > s.second)) { ans = "yes"; break; } } } } cout << ans << endl; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main extends PrintWriter { BufferedReader in; StringTokenizer stok; final Random rand = new Random(31); final int inf = (int) 1e9; final long linf = (long) 1e18; boolean intersect(int x1, int y1, int x2, int y2) { if (max(x1, x2) >= min(y1, y2)) { return false; } if (x2 >= x1 && y2 <= y1) { return false; } if (x1 >= x2 && y1 <= y2) { return false; } return true; } void solve() throws IOException { int n = nextInt(); int[] a = nextIntArray(n); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n - 1; j++) { if (intersect(min(a[i], a[i + 1]), max(a[i], a[i + 1]), min(a[j], a[j + 1]), max(a[j], a[j + 1]))) { println("yes"); return; } } } println("no"); } void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); System.exit(abs(-1)); } finally { close(); } } Main() throws IOException { super(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } Main(String filename) throws IOException { super("".equals(filename) ? "output.txt" : filename + ".out"); in = new BufferedReader(new FileReader("".equals(filename) ? "input.txt" : filename + ".in")); } public static void main(String[] args) throws IOException { new Main().run(); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } int[] nextIntArray(int len) throws IOException { int[] res = new int[len]; for (int i = 0; i < len; i++) { res[i] = nextInt(); } return res; } void shuffle(int[] a) { for (int i = 1; i < a.length; i++) { int x = rand.nextInt(i + 1); int _ = a[i]; a[i] = a[x]; a[x] = _; } } boolean nextPerm(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1; ; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } <T> List<T>[] createAdjacencyList(int countVertex) { List<T>[] res = new List[countVertex]; for (int i = 0; i < countVertex; i++) { res[i] = new ArrayList<T>(); } return res; } class Pair<A extends Comparable<A>, B extends Comparable<B>> implements Comparable<Pair<A, B>> { A a; B b; public Pair(A a, B b) { this.a = a; this.b = b; } @Override public int compareTo(Pair<A, B> o) { int aa = a.compareTo(o.a); return aa == 0 ? b.compareTo(o.b) : aa; } } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int n, arr[1000]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; bool ans = false; for (int i = 0; i < n - 1; i++) { int l1 = min(arr[i], arr[i + 1]); int r1 = max(arr[i], arr[i + 1]); for (int j = 0; j < n - 1; j++) { int l2 = min(arr[j], arr[j + 1]); int r2 = max(arr[j], arr[j + 1]); if (l1 < l2 && r1 > l2 && r1 < r2) ans = true; if (r1 > r2 && l1 < r2 && l1 > l2) ans = true; } } cout << (ans ? "yes" : "no") << endl; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n=int(input()) ar=map(int,raw_input().split()) l=[] for _ in xrange(n-1): l.append([]) for i in xrange(n-1): l[i].append((ar[i+1]+ar[i])/2.0) l[i].append(abs(ar[i+1]-ar[i])/2.0) flag=1 for i in xrange(n-1): for j in range(i+1,n-1): c1=l[i][0] c2=l[j][0] r1=l[i][1] r2=l[j][1] val1=[c1-r1,r1+c1] val2=[c2-r2,r2+c2] #print val1,val2 if val1[1]<=val2[0]: #print "here1" continue elif val2[1]<=val1[0]: #print "here2" continue elif val1[0]<=val2[0] and val2[1]<=val1[1]: #print "here3" continue elif val2[0]<=val1[0] and val1[1]<=val2[1]: #print "here4" continue else: flag=0 break if flag==1: print "no" else: print "yes"
PYTHON
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int p1; cin >> p1; int p2; cin >> p2; vector<pair<int, int> > v; int p = p2; v.push_back(make_pair(p1, p2)); for (int i = 1; i < n; i++) { int t; cin >> t; for (int i = 0; i < v.size(); i++) { int l1 = v[i].first; int l2 = v[i].second; if (l1 > l2 & t<l1 & t> l2 & (p > l1 | p < l2)) { cout << "yes"; return 0; } else if (l2 > l1 & t<l2 & t> l1 & (p<l1 | p> l2)) { cout << "yes"; return 0; } else if (l1 > l2 & (t<l2 | t> l1) & p<l1 & p> l2) { cout << "yes"; return 0; } else if (l2 > l1 & (t > l2 | t < l1) & p > l1 & p < l2) { cout << "yes"; return 0; } } v.push_back(make_pair(p, t)); p = t; } cout << "no"; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.*; import java.io.*; public class Dima_and_Continuous_Line { public static void main(String args[]) throws Exception { BufferedReader f=new BufferedReader(new InputStreamReader(System.in)); int runs=Integer.parseInt(f.readLine()); interval[] arr=new interval[runs-1]; StringTokenizer st=new StringTokenizer(f.readLine()); int[] thing=new int[runs]; for(int x=0;x<runs;x++) thing[x]=Integer.parseInt(st.nextToken()); boolean flag=true; for(int x=0;x<runs-1;x++) { arr[x]=new interval(Math.min(thing[x],thing[x+1]),Math.max(thing[x],thing[x+1])); for(int y=0;y<x;y++) if(arr[y].start<arr[x].start&&arr[y].end<arr[x].end&&arr[y].end>arr[x].start||arr[x].start<arr[y].start&&arr[x].end<arr[y].end&&arr[x].end>arr[y].start) flag=false; } System.out.println(flag?"no":"yes"); } } class interval { int start,end; public interval(int a,int b) { start=a; end=b; } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Solution { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } public static class Task { static class Range { int s, e; Range(int start, int end) { s = start; e = end; } } public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); Range r[] = new Range[n - 1]; int s = in.nextInt() + 1000000; int e; for (int i = 0; i < n - 1; i++) { e = in.nextInt() + 1000000; r[i] = new Range(Math.min(s, e), Math.max(s, e)); s = e; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (j == i) { continue; } if (r[j].s < r[i].s) { if (r[j].e > r[i].s && r[j].e < r[i].e) { out.println("yes"); return; } } else if (r[j].s > r[i].s && r[j].s < r[i].e) { if (r[i].e < r[j].e) { out.println("yes"); return; } } } } out.println("no"); } } // } ---------------------------------------------------------------------------- // MAIN - INPUT - OUTPUT static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } public char[][] nextMatrix(int row, int col) { char[][] m = new char[row][col]; for (int i = 0; i < row; i++) { m[i] = next().toCharArray(); } return m; } } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
""" let one arc is s1-s2 then it cuts another arc s3-s4 if 1. s1<s3<s2<s4 OR 2. s3<s1<s4<s2 for each arc, check if it cuts any of the other arcs """ n = int(input()) x = [] mp = map(int,input().split()) for k in mp: x.append(k) res = False #means that do not cut for i in range(n-1): s1 = min(x[i],x[i+1]) s2 = max(x[i],x[i+1]) #print("comparing "+str(s1)+"-"+str(s2)+" with") for j in range(i+1,n-1): s3 = min(x[j],x[j+1]) s4 = max(x[j],x[j+1]) #print(str(s3)+"-"+str(s4)) if s3<s1<s4<s2 or s1<s3<s2<s4: res = True if res: print("yes") else: print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import sys n = int(raw_input()) L = list(map(int, raw_input().split(' '))) Lft =[] Rt =[] if len(L) <=3: print 'no' else : for i in range(n): tt = i+1 if tt != n: if L[i] < L[tt]: Lft.append(L[i]) Rt.append(L[tt]) else : Lft.append(L[tt]) Rt.append(L[i]) flag = 0 for i in range(n-1): for j in range(n-1): if Lft[i] > Lft[j] and Lft[i] < Rt[j]and Rt[i] >Rt[j]: flag = 1 if flag ==0: print 'no' else : print 'yes'
PYTHON