[比赛传送门]
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:
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.
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).
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.
4
5 10 4
4 5 20
2 2 3
1000000000 1000000000 1000000000
-1 20
8 -1
1 2
-1 1000000000
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元,第二家是b个甜甜圈c元。
请你输出两个数字x,y,且满足:
当从两家各买到x个甜甜圈时,第一家比第二家便宜(如果第二家无法恰好买到x个,就只要买够x个,即大于x就可以啦)。
当从两家各买到y个甜甜圈时,第二家比第一家便宜。
若x,y不存在,则以-1代替
如果第二家的套装价c
元大于等于第一家的单价a
元,则不可能出现第一家比第二家便宜,x=-1;否则x=1即可。
如果第二家的单个均价c/b
小于第一家的单价a
,则在第二家买一盒b个比在第一家买b个便宜,y=b;否则y=-1。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define rep(l, a, b) for (ll l = a; l < b; ++l)
#define per(l, a, b) for (ll l = a; l >= b; --l)
#define debug(a) cout << '#' << a << '#' << endl
using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
db a, b, c;
cin >> a >> b >> c;
int x, y;
if (c <= a) {
x = -1;
} else {
x = 1;
}
if ((c * 1.0) / (b * 1.0) < a) {
y = b;
} else {
y = -1;
}
cout << x << ' ' << y << endl;
}
}
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:
If a player can’t make any move, they lose. Both players play optimally. You have to determine if Alice can win.
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.
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.
3
01
1111
0011
DA
NET
NET
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和Bob轮流获得一回合机会,直到其中一个玩家无法删除字符串的元素,另一玩家赢下比赛,游戏结束。游戏的第一回合由Alice起手。
当Alice胜利输出DA
,当Bob胜利输出NET
。
只要字符串中0
和1
都存在,就必然存在可以消去的两个字符。
设字符串中0
的个数为a,1
的个数为b。则可以进行的回合数为min(a,b)。
所以当min(a,b)为偶数时,代表在Alice的回合字符串会只剩下一种字符,此时输出DA
,否则输出NET
。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define rep(l, a, b) for (ll l = a; l < b; ++l)
#define per(l, a, b) for (ll l = a; l >= b; --l)
#define debug(a) cout << '#' << a << '#' << endl
using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int z = 0, o = 0;
int sz = s.size();
rep(i, 0, sz) {
if (s[i] == '0') z++;
if (s[i] == '1') o++;
}
int minx = z > o ? o : z;
if ((minx & 1)) {
cout << "DA" << endl;
} else {
cout << "NET" << endl;
}
}
}
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.
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.
For each test case print one integer — the value of the res after the process ends.
3
--+-
---
++--+-
7
9
6
给你一个由+
和-
组成的字符串,按照上面的代码得出res并输出。其中inf指无穷大数。
这题肯定不能直接交上面的代码我已经帮大家试过了
剩下的就是找规律
当执行到cur小于0且cur为新低时,等于程序在字符s上从第一个到当前字符走过一遍,因为存在一个init=-cur,使得程序走到这个新低时退出次级循环。
而在不是新低的时候,无法找到一个init使得程序走到当前位置恰好小于0而退出当前循环。
该部分代码为:
for(int i=0,i<sz;++i){
if(s[i]=='+') cur++;
if(s[i]=='-') cur--;
if(cur<0){
if(minc>cur) res+=(i+1);
if(cur<minc) minc=cur;
}
}
当在init较小时cur存在小于0时,必然存在一个init使得cur全程大于等于0,使得ok保持为true,最后退出程序。
当字符串总为+
时,即当init较小时cur依然保持大于等于0,此时程序将只走过一遍字符串,然后退出循环,判定ok最后退出程序。
所以,程序除了cur遇到新低时会增加res,还会增加字符串长度的res,即:
res+=s.size()
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define rep(l, a, b) for (ll l = a; l < b; ++l)
#define per(l, a, b) for (ll l = a; l >= b; --l)
#define debug(a) cout << '#' << a << '#' << endl
using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
// int res = 0;
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
ll res = 0;
ll sz=s.size();
ll cur=0,minc=0;
rep(i,0,sz){
if(s[i]=='+') cur++;
if(s[i]=='-') cur--;
if(cur<0){
if(minc>cur) res+=(i+1);
if(cur<minc) minc=cur;
}
}
res+=sz;
cout << res << endl;
}
}