A. Donut Shops
There are two rival donut shops.
The first shop sells donuts at retail: each donut costs a dollars.
The second shop sells donuts only in bulk: box of b donuts costs c dollars. So if you want to buy x donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to x.
You want to determine two positive integer values:
how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop?
how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop?
If any of these values doesn't exist then that value should be equal to −1. If there are multiple possible answers, then print any of them.
The printed values should be less or equal to 109. It can be shown that under the given constraints such values always exist if any values exist at all.
Input
The first line contains a single integer t (1≤t≤1000) — the number of testcases.
Each of the next t lines contains three integers a, b and c (1≤a≤109, 2≤b≤109, 1≤c≤109).
Output
For each testcase print two positive integers. For both shops print such x that buying x donuts in this shop is strictly cheaper than buying x donuts in the other shop. x should be greater than 0 and less or equal to 109.
If there is no such x, then print −1. If there are multiple answers, then print any of them.
Example
input
4
5 10 4
4 5 20
2 2 3
1000000000 1000000000 1000000000
output
-1 20
8 -1
1 2
-1 1000000000
Note
In the first testcase buying any number of donuts will be cheaper in the second shop. For example, for 3 or 5 donuts you'll have to buy a box of 10 donuts for 4 dollars. 3 or 5 donuts in the first shop would cost you 15 or 25 dollars, respectively, however. For 20 donuts you'll have to buy two boxes for 8 dollars total. Note that 3 and 5 are also valid answers for the second shop, along with many other answers.
In the second testcase buying any number of donuts will be either cheaper in the first shop or the same price. 8 donuts cost 32 dollars in the first shop and 40 dollars in the second shop (because you have to buy two boxes). 10 donuts will cost 40 dollars in both shops, so 10 is not a valid answer for any of the shops.
In the third testcase 1 donut costs 2 and 3 dollars, respectively. 2 donuts cost 4 and 3 dollars. Thus, 1 is a valid answer for the first shop and 2 is a valid answer for the second shop.
In the fourth testcase 109 donuts cost 1018 dollars in the first shop and 109 dollars in the second shop.
题意: A 店每个甜甜圈 a 元 ,B 店一盒 b 个甜甜圈 c 元 ,输出 x 表示在该店买 x 个甜甜圈比另一家店便宜 .(B店只能一盒一盒购买)
每个案例对应 a b c ,若不存在 x 则输出 -1 .
分类讨论:
记 ans1 ,ans2 为答案 .
当 b == 1 时 ,比较 a 和 c 的大小即可 .
当 b != 1 时 ,A 店也买 b 个甜甜圈价钱为 a * b;
若 a * b <= c ,则另 ans1 = 1 ,ans2 = -1;
若 a * b > c ,则 ans2 = b; 若 a < c ,则 ans1 = 1; 否则 a >= c 时 ans1 = -1;
这...和直接贴代码有什么区别...
#include
#define LL long long
#define Pi acos(-1.0)
#define INF 2147483646
#define eps 1e-9
#define MS 100009
#define mss 17
using namespace std;
// Notice the data size
// Notice the input and output
LL n,m,k;
LL p[MS];
int main() {
cin >> k;
while(k--){
LL a,b,c;
cin >> a >> b >> c;
LL ans1,ans2;
if(b == 1){
if(a == c){
ans1 = ans2 = -1;
}
else if(a > c){
ans1 = 1;
ans2 = -1;
}
else if(a < c){
ans1 = -1;
ans2 = 1;
}
}
else{
if(a*b<=c){
ans1 = 1;
ans2 = -1;
}
else if(a*b>c){
ans2 = b;
if(a
B. 01 Game
Alica and Bob are playing a game.
Initially they have a binary string s consisting of only characters 0 and 1.
Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent characters of string s and delete them. For example, if s=1011001 then the following moves are possible:
delete s1 and s2: 1011001→11001;
delete s2 and s3: 1011001→11001;
delete s4 and s5: 1011001→10101;
delete s6 and s7: 1011001→10110.
If a player can't make any move, they lose. Both players play optimally. You have to determine if Alice can win.
Input
First line contains one integer t (1≤t≤1000) — the number of test cases.
Only line of each test case contains one string s (1≤|s|≤100), consisting of only characters 0 and 1.
Output
For each test case print answer in the single line.
If Alice can win print DA (YES in Russian) in any register. Otherwise print NET (NO in Russian) in any register.
Example
input
3
01
1111
0011
output
DA
NET
NET
Note
In the first test case after Alice's move string s become empty and Bob can not make any move.
In the second test case Alice can not make any move initially.
In the third test case after Alice's move string s turn into 01. Then, after Bob's move string s become empty and Alice can not make any move.
题意:两玩家 ,Alice先 ,Bob后 ,每次能做的操作为找到相邻的两个'0'、'1'字符并删除 ,最后不能操作的人输 .
若Alice赢 ,则输出 'DA' ,否则 'NET' . (俄语中分别表示 YES 和 NO ) .
解: 只要字符串中有 0 有 1 则游戏都可以进行下去 ,而且每次操作必定少一个 0 和 1 ,则得到 0 和 1 中少的个数记为 t ,t 为奇数则赢 .
#include
#define LL long long
#define Pi acos(-1.0)
#define INF 2147483646
#define eps 1e-9
#define MS 100009
#define mss 17
using namespace std;
// Notice the data size
LL n,m,k;
LL p[MS];
char s[MS];
int main() {
std::ios::sync_with_stdio(false);
cin >> k;
while(k--){
memset(s,0,sizeof s);
cin >> s;
LL t1,t0,h = strlen(s);
t0 = t1 = 0;
for(int i=0;i
C. Pluses and Minuses
You are given a string s consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode:
res = 0
for init = 0 to inf
cur = init
ok = true
for i = 1 to |s|
res = res + 1
if s[i] == '+'
cur = cur + 1
else
cur = cur - 1
if cur < 0
ok = false
break
if ok
break
Note that the inf denotes infinity, and the characters of the string are numbered from 1 to |s|.
You have to calculate the value of the res after the process ends.
Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.
The only lines of each test case contains string s (1≤|s|≤106) consisting only of characters + and -.
It's guaranteed that sum of |s| over all test cases doesn't exceed 106.
Output
For each test case print one integer — the value of the res after the process ends.
Example
input
3
--+-
---
++--+-
output
7
9
6
题意: 就是题中所给代码最后得到 res 的值 .
翻译一下就比如一个横板2D闯关游戏 ,一开始血量 cur = 0 ,每一格对应一个字符 ,每次碰到 '-' 则血量-- ,'+' 则血量++ ,若血量cur < 0 则从头再来且初始血量 +1 直至通关,求最后一共走过多少格 .
解: 求前缀和 ,记 '-' 为 -1 ,'+' 为 +1 ,因为前缀和是连续的 ,所以每次记录血量为 0 时找前缀和第一次为 -1 的下标 ,记录血量为 1 是第一次为 -2 的下标... 最后下标值相加即可 .
#include
#define LL long long
#define Pi acos(-1.0)
#define INF 2147483646
#define eps 1e-9
#define MS 1000009
#define mss 17
using namespace std;
// Notice the data size
LL n,m,k;
LL p[MS],a[MS];
char s[MS];
int main() {
std::ios::sync_with_stdio(false);
cin >> k;
while(k--){
cin >> s;
LL h = strlen(s);
LL t = 0 ,cc = -1 ,tot = 0;
for(int i=0;i
D. Maximum Sum on Even Positions
You are given an array a consisting of n integers. Indices of the array start from zero (i. e. the first element is a0, the second one is a1, and so on).
You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of a with borders l and r is a[l;r]=al,al+1,…,ar.
Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements a0,a2,…,a2k for integer k=⌊n−12⌋ should be maximum possible).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of a. The second line of the test case contains n integers a0,a1,…,an−1 (1≤ai≤109), where ai is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).
Output
For each test case, print the answer on the separate line — the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of a.
Example
input
4
8
1 7 3 4 7 6 2 9
5
1 2 1 2 1
10
7 8 4 5 7 6 8 9 7 3
4
3 1 2 1
output
26
5
37
5
题意: 给一段数组 ,可以翻转一次连续的子序列 ,使得 a0 ,a2 ,a4 ... ,a2k 之和最大 ,输出最大值 .
解: 显然翻转长度为奇数的序列没有任何用处 ,所以翻转偶数长度的片段 .
例如 7 8 4 5 7 6 本来是 7 + 4 + 7 翻转后为 8 + 5 + 6,也就是相当于 7 8 换位 ,4 5 换位 ,7 6 换位
则 记录两两换位后的差值存储与数组 a ,原数组 p 是 7 8 4 5 7 6 则 a 为 1 1 -1 ,显然此时交换 7 8 4 5 比交换整体更赚 ,所以求数组 a 的连续一段 和最大 的区间值 ta 即可 ,可记录 a 的前缀和求解 .
当然在 7 8 4 5 7 6 中 ,8 不仅可以和 7 交换 ,也可与 4 交换 ,则新建数组 b 记录 8 4 差值 ,5 7 差值 ... 也求数组 b 的连续一段 和最大 的区间值 tb ,取 ta ,tb 大者 .
#include
#define LL long long
#define Pi acos(-1.0)
#define INF 2147483646
#define eps 1e-9
#define MS 200009
#define mss 17
using namespace std;
// Notice the data size
LL n,m,k;
LL p[MS],a[MS],b[MS];
int main() {
std::ios::sync_with_stdio(false);
cin >> k;
while(k--){
a[0] = b[0] = 0;
LL h,ans = 0;
cin >> h;
for(int i=0;i> p[i];
if(!(i&1)) ans += p[i]; // 记录不进行任何翻转操作的答案
}
LL maxx = 0,tot = 0;
for(int i=1;i
E. Sum of Digits
Let f(x) be the sum of digits of a decimal number x.
Find the smallest non-negative integer x such that f(x)+f(x+1)+⋯+f(x+k)=n.
Input
The first line contains one integer t (1≤t≤150) — the number of test cases.
Each test case consists of one line containing two integers n and k (1≤n≤150, 0≤k≤9).
Output
For each test case, print one integer without leading zeroes. If there is no such x that f(x)+f(x+1)+⋯+f(x+k)=n, print −1; otherwise, print the minimum x meeting that constraint.
Example
input
7
1 0
1 1
42 7
13 7
99 1
99 0
99 2
output
1
0
4
-1
599998
99999999999
7997
题意: 设 f(x) 为十进制数 x 的位数之和 ,找出最小的非负整数 x,使 f(x) + f(x+1) + ⋯ + f(x+k) = n
解:
0. 当我发现 k 等于 9 时 ,
若 x = 0 时 ==> 0 1 2 3 4 5 6 7 8 9 位数和为 45
若 x = 1 时 ==> 1 2 3 4 5 6 7 8 9 10 位数和为 46
若 x = 2 时 ==> 2 3 4 5 6 7 8 9 10 11 位数和为 47
嗯 ... 想着如果说 n = 55 ,k == 9 的话 ,我好像可以把个位数定下来 ,这样的话 10 个数 每个个位数就可以定为 0 1 2 3 4 5 6 7 8 9 和为 45.
为什么这么定呢? 因为 n - 45 = 10 ,欸! 这不是每个十位数加上一个 1 就好了 ,那么答案就是 10 !
当然这是非常理想的情况下 ,但是定下了后面的想法框架 .
1. 也就是现在想法先是定下个位数数字 ,再减去个位数之和 ,最后确定十位百位千位万位... (比赛时时间有限只能想到这儿了 ,果然是我tcl) .
个位数怎么确定呢?
因为有 k+1 个数 ,0 ~ k 分别表示每一个数 ,既然要确定个位数 ,那就先打表吧 .
比如说 个位数为 0 时 ,k = 0,1,2,3... 令 p[i][j] 表示个位数为 i 长度为 j+1 的各个个位数之和 (毕竟 k 为 1 时 ,有两个数)
那么 p[7][5] 会出现 7 8 9 10 11 这样子 ,10 为 1 ,11 为 2 .
最大的就是 p[9][9] ,9 10 11 12 13 14 15 16 17 18 最大就是 18 也就是位数和为 9 .
这样表就打完了 ,接下来怎么选个位数 .
现在假设选完了个位数 p[i][j] ,毕竟数字都是连续的所以 n - p[i][j] 这个值要平均分配到每个数的十位以上 .
所以 n - p[i][j] 要能整除 k + 1 .
记 cc = (n - p[i][j])/(k+1) ,既然个位数确定下来了 ,cc 又是十位数以上的位数之和 ,那么贪心即可 .
2. 十位数以上的需要注意当 p[i][j] 中 i + j > 9 时 ,这样每次数字 +1 时会进位 这个时候十位数最多到 8 即可 .
感觉写的挺认真 ,但是第三视角看我写的内容好像也不是特别清楚 ...
#include
#define LL long long
#define Pi acos(-1.0)
#define INF 2147483646
#define eps 1e-9
#define MS 100009
#define mss 17
using namespace std;
// Notice the data size
// Notice the input and output
LL n,m,k;
LL p[11][11],a[20];
void init(){
for(int i=0;i<10;i++) a[i] = i;
for(int i=10;i<19;i++) a[i] = i-9;
for(int i=0;i<10;i++){ // 打表
for(int j=0;j<10;j++){
for(int u=i;u<=i+j;u++){
p[i][j] += a[u];
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
init();
cin >> m;
while(m--){
cin >> n >> k;
LL t = -1 ,cc = INF,flag = 0;
for(int i=9;i>=0;i--){ // 寻找一个个位数
if(p[i][k]>n) continue;
if((n-p[i][k])%(k+1) == 0){
if((n-p[i][k])/(k+1)<=cc){
t = i; // 记录个位数
cc = (n-p[i][k])/(k+1); // 记录十位以上位数之和
}
}
}
if(t == -1){ // 未能整除
cout << t << endl;
continue;
}
LL t9;
if(t+k <= 9){ // 不用担心进位
t9 = cc / 9;
cc -= t9*9;
if(cc != 0) cout << cc;
for(int i=0;i