AtCoder Inc. sells merchandise through its online shop.
Takahashi has decided to purchase N N N types of products from there.
For each integer i i i from 1 1 1 to N N N, the i i i-th type of product has a price of P i P_i Pi yen each, and he will buy Q i Q_i Qi of this.
Additionally, he must pay a shipping fee.
The shipping fee is 0 0 0 yen if the total price of the products purchased is S S S yen or above, and K K K yen otherwise.
He will pay the total price of the products purchased plus the shipping fee.
Calculate the amount he will pay.
The input is given from Standard Input in the following format:
N N N S S S K K K
P 1 P_1 P1 Q 1 Q_1 Q1
P 2 P_2 P2 Q 2 Q_2 Q2
⋮ \vdots ⋮
P N P_N PN Q N Q_N QN
Print the amount Takahashi will pay for online shopping.
2 2000 500
1000 1
100 6
2100
Takahashi buys one product for 1000 1000 1000 yen and six products for 100 100 100 yen each.
Thus, the total price of the products is 1000 × 1 + 100 × 6 = 1600 1000\times 1+100\times 6=1600 1000×1+100×6=1600 yen.
Since the total amount for the products is less than 2000 2000 2000 yen, the shipping fee will be 500 500 500 yen.
Therefore, the amount Takahashi will pay is 1600 + 500 = 2100 1600+500=2100 1600+500=2100 yen.
3 2000 500
1000 1
100 6
5000 1
6600
The total price of the products is 1000 × 1 + 100 × 6 + 5000 × 1 = 6600 1000\times 1+100\times 6+5000\times 1=6600 1000×1+100×6+5000×1=6600 yen.
Since the total amount for the products is not less than 2000 2000 2000 yen, the shipping fee will be 0 0 0 yen.
Therefore, the amount Takahashi will pay is 6600 + 0 = 6600 6600+0=6600 6600+0=6600 yen.
2 2000 500
1000 1
1000 1
2000
There may be multiple products with the same price per item.
具体见文后视频。
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int N, S, K;
cin >> N >> S >> K;
int Sum = 0, P, Q;
for (int i = 1; i <= N; i ++)
cin >> P >> Q, Sum += P * Q;
if (Sum < S) cout << Sum + K << endl;
else cout << Sum << endl;
return 0;
}
AtCoder Inc. sells glasses and mugs.
Takahashi has a glass with a capacity of G G G milliliters and a mug with a capacity of M M M milliliters.
Here, G < M G
Initially, both the glass and the mug are empty.
After performing the following operation K K K times, determine how many milliliters of water are in the glass and the mug, respectively.
The input is given from Standard Input in the following format:
K K K G G G M M M
Print the amounts, in milliliters, of water in the glass and the mug, in this order, separated by a space, after performing the operation K K K times.
5 300 500
200 500
The operation will be performed as follows. Initially, both the glass and the mug are empty.
Thus, after five operations, the glass has 200 200 200 milliliters, and the mug has 500 500 500 milliliters of water. Hence, print 200 200 200 and 500 500 500 in this order, separated by a space.
5 100 200
0 0
具体见文后视频。
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int K, M, G;
cin >> K >> G >> M;
int A = 0, B = 0;
while (K --)
{
if (A == G) A = 0;
else if (B == 0) B = M;
else
{
int Push = min(G - A, B);
A += Push, B -= Push;
}
}
cout << A << " " << B << endl;
return 0;
}
AtCoder Inc. sells T-shirts with its logo.
You are given Takahashi’s schedule for N N N days as a string S S S of length N N N consisting of 0
, 1
, and 2
.
Specifically, for an integer i i i satisfying 1 ≤ i ≤ N 1\leq i\leq N 1≤i≤N,
0
, he has no plan scheduled for the i i i-th day;1
, he plans to go out for a meal on the i i i-th day;2
, he plans to attend a competitive programming event on the i i i-th day.Takahashi has M M M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.
Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N N N days. If he does not need to buy new T-shirts, print 0 0 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.
0
, 1
, and 2
.Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print $ 0$.
6 1
112022
2
If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:
If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2 2 2.
3 1
222
3
2 1
01
0
He does not need to buy new T-shirts.
具体见文后视频。
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int N, M;
string S;
cin >> N >> M >> S;
S = ' ' + S;
int Plain = M, Logo = 0, TL = 0;
for (int i = 1; i <= N; i ++)
if (S[i] == '1')
{
if (Plain)
Plain --;
else
if (Logo) Logo --;
else TL ++;
}
else if (S[i] == '2')
{
if (Logo) Logo --;
else TL ++;
}
else
Plain = M, Logo = TL;
cout << TL << endl;
return 0;
}
You are given two grids, A and B, each with H H H rows and W W W columns.
For each pair of integers ( i , j ) (i, j) (i,j) satisfying 1 ≤ i ≤ H 1 \leq i \leq H 1≤i≤H and 1 ≤ j ≤ W 1 \leq j \leq W 1≤j≤W, let ( i , j ) (i, j) (i,j) denote the cell in the i i i-th row and j j j-th column. In grid A, cell ( i , j ) (i, j) (i,j) contains the integer A i , j A_{i, j} Ai,j. In grid B, cell ( i , j ) (i, j) (i,j) contains the integer B i , j B_{i, j} Bi,j.
You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:
Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.
Here, grid A is identical to grid B if and only if, for all pairs of integers ( i , j ) (i, j) (i,j) satisfying 1 ≤ i ≤ H 1 \leq i \leq H 1≤i≤H and 1 ≤ j ≤ W 1 \leq j \leq W 1≤j≤W, the integer written in cell ( i , j ) (i, j) (i,j) of grid A is equal to the integer written in cell ( i , j ) (i, j) (i,j) of grid B.
The input is given from Standard Input in the following format:
H H H W W W
A 1 , 1 A_{1, 1} A1,1 A 1 , 2 A_{1, 2} A1,2 ⋯ \cdots ⋯ A 1 , W A_{1, W} A1,W
A 2 , 1 A_{2, 1} A2,1 A 2 , 2 A_{2, 2} A2,2 ⋯ \cdots ⋯ A 2 , W A_{2, W} A2,W
⋮ \vdots ⋮
A H , 1 A_{H, 1} AH,1 A H , 2 A_{H, 2} AH,2 ⋯ \cdots ⋯ A H , W A_{H, W} AH,W
B 1 , 1 B_{1, 1} B1,1 B 1 , 2 B_{1, 2} B1,2 ⋯ \cdots ⋯ B 1 , W B_{1, W} B1,W
B 2 , 1 B_{2, 1} B2,1 B 2 , 2 B_{2, 2} B2,2 ⋯ \cdots ⋯ B 2 , W B_{2, W} B2,W
⋮ \vdots ⋮
B H , 1 B_{H, 1} BH,1 B H , 2 B_{H, 2} BH,2 ⋯ \cdots ⋯ B H , W B_{H, W} BH,W
If it is impossible to make grid A identical to grid B, output -1
. Otherwise, print the minimum number of operations required to make grid A identical to grid B.
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19
3
Swapping the fourth and fifth columns of the initial grid A yields the following grid:
1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19
Then, swapping the second and third rows yields the following grid:
1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19
Finally, swapping the second and third columns yields the following grid, which is identical to grid B:
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19
You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3 3 3.
2 2
1 1
1 1
1 1
1 1000000000
\-1
There is no way to perform the operation to make grid A match grid B, so print -1
.
3 3
8 1 6
3 5 7
4 9 2
8 1 6
3 5 7
4 9 2
0
Grid A is already identical to grid B at the beginning.
5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029
20
具体见文后视频。
#include
#include
#include
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef vector<vector<int>> VVI;
int N, M;
VVI A, B;
map<VVI, int> Vis, Dist;
int BFS()
{
queue<VVI> Q;
Q.push(A), Vis[A] = 1;
while (Q.size())
{
auto T = Q.front();
Q.pop();
if (T == B) return Dist[T];
VVI Source = T;
for (int i = 1; i < N; i ++)
{
for (int j = 1; j <= M; j ++)
swap(T[i][j], T[i + 1][j]);
if (!Vis.count(T)) Q.push(T), Vis[T] = 1, Dist[T] = Dist[Source] + 1;
T = Source;
}
for (int j = 1; j < M; j ++)
{
for (int i = 1; i <= N; i ++)
swap(T[i][j], T[i][j + 1]);
if (!Vis.count(T)) Q.push(T), Vis[T] = 1, Dist[T] = Dist[Source] + 1;
T = Source;
}
}
return -1;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> N >> M;
A.resize(N + 1), B.resize(N + 1);
for (int i = 1; i <= N; i ++)
A[i].resize(M + 1), B[i].resize(M + 1);
for (int i = 1; i <= N; i ++)
for (int j = 1; j <= M; j ++)
cin >> A[i][j];
for (int i = 1; i <= N; i ++)
for (int j = 1; j <= M; j ++)
cin >> B[i][j];
cout << BFS() << endl;
return 0;
}
AtCoder Inc. sells merchandise on its online shop.
There are N N N items remaining in the company. The weight of the i i i-th item ( 1 ≤ i ≤ N ) (1\leq i\leq N) (1≤i≤N) is W i W_i Wi.
Takahashi will sell these items as D D D lucky bags.
He wants to minimize the variance of the total weights of the items in the lucky bags.
Here, the variance is defined as V = 1 D ∑ i = 1 D ( x i − x ˉ ) 2 V=\frac{1}{D}\displaystyle\sum_{i=1}^D (x_i-\bar{x})^2 V=D1i=1∑D(xi−xˉ)2, where x 1 , x 2 , … , x D x_1,x_2,\ldots,x_D x1,x2,…,xD are the total weights of the items in the lucky bags, and x ˉ = 1 D ( x 1 + x 2 + ⋯ + x D ) \bar{x}=\frac{1}{D}(x_1+x_2+\cdots+x_D) xˉ=D1(x1+x2+⋯+xD) is the average of x 1 , x 2 , … , x D x_1,x_2,\ldots,x_D x1,x2,…,xD.
Find the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
It is acceptable to have empty lucky bags (in which case the total weight of the items in that bag is defined as 0 0 0),
but each item must be in exactly one of the D D D lucky bags.
The input is given from Standard Input in the following format:
N N N D D D
W 1 W_1 W1 W 2 W_2 W2 … \ldots … W N W_N WN
Print the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
Your output will be considered correct if the absolute or relative error from the true value is at most 1 0 − 6 10^{-6} 10−6.
5 3
3 5 3 6 3
0.888888888888889
If you put the first and third items in the first lucky bag, the second and fifth items in the second lucky bag, and the fourth item in the third lucky bag, the total weight of the items in the bags are 6 6 6, 8 8 8, and 6 6 6, respectively.
Then, the average weight is 1 3 ( 6 + 8 + 6 ) = 20 3 \frac{1}{3}(6+8+6)=\frac{20}{3} 31(6+8+6)=320,
and the variance is 1 3 { ( 6 − 20 3 ) 2 + ( 8 − 20 3 ) 2 + ( 6 − 20 3 ) 2 } = 8 9 = 0.888888 … \frac{1}{3}\left\{\left(6-\frac{20}{3}\right)^2+\left(8-\frac{20}{3}\right)^2+\left(6-\frac{20}{3}\right)^2 \right\}=\frac{8}{9}=0.888888\ldots 31{(6−320)2+(8−320)2+(6−320)2}=98=0.888888…, which is the minimum.
Note that multiple items may have the same weight, and that each item must be in one of the lucky bags.
具体见文后视频。
#include
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int SIZE = 17;
int N, D;
int W[SIZE], Sum[1ll << SIZE];
double F[SIZE][1ll << SIZE];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> N >> D;
for (int i = 1; i <= N; i ++)
cin >> W[i];
for (int i = 0; i < 1 << N; i ++)
for (int j = 0; j < N; j ++)
if (i >> j & 1)
Sum[i] += W[j + 1];
double Ave = Sum[(1 << N) - 1] * 1.0 / D;
for (int i = 0; i <= D; i ++)
for (int j = 0; j < 1 << N; j ++)
F[i][j] = 1e18;
F[0][0] = 0;
for (int i = 1; i <= D; i ++)
for (int j = 0; j < 1 << N; j ++)
for (int k = j; k; k = k - 1 & j)
{
int Left = j ^ k;
if (F[i - 1][Left] == 1e18) continue;
F[i][j] = min(F[i][j], F[i - 1][Left] + 1.0 * (Sum[k] * 1.0 - Ave) * (Sum[k] * 1.0 - Ave));
}
printf("%.8lf\n", F[D][(1 << N) - 1] * 1.0 / D);
return 0;
}
}
You are given an integer sequence A = ( A 1 , A 2 , … , A N ) A = (A_1, A_2, \ldots, A_N) A=(A1,A2,…,AN) of length N N N.
We will perform the following operation on A A A for i = 1 , 2 , … , M i = 1, 2, \ldots, M i=1,2,…,M in this order.
For the final sequence A A A after the above procedure, print the expected value, modulo 998244353 998244353 998244353, of A i A_i Ai for each i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,…,N.
How to print expected values modulo 998244353 998244353 998244353
It can be proved that the expected values sought in this problem are always rational. Furthermore, the constraints of this problem guarantee that if each of those expected values is expressed as an irreducible fraction y x \frac{y}{x} xy, then x x x is not divisible by 998244353 998244353 998244353.
Now, there is a unique integer z z z between 0 0 0 and 998244352 998244352 998244352, inclusive, such that x z ≡ y ( m o d 998244353 ) xz \equiv y \pmod{998244353} xz≡y(mod998244353). Report this z z z.
The input is given from Standard Input in the following format:
N N N M M M
A 1 A_1 A1 A 2 A_2 A2 … \ldots … A N A_N AN
L 1 L_1 L1 R 1 R_1 R1 X 1 X_1 X1
L 2 L_2 L2 R 2 R_2 R2 X 2 X_2 X2
⋮ \vdots ⋮
L M L_M LM R M R_M RM X M X_M XM
Print the expected values E i E_i Ei of the final A i A_i Ai for i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,…,N in the format below, separated by spaces.
E 1 E_1 E1 E 2 E_2 E2 … \ldots … E N E_N EN
5 2
3 1 4 1 5
1 2 2
2 4 0
499122179 1 665496238 665496236 5
We start from the initial state A = ( 3 , 1 , 4 , 1 , 5 ) A = (3, 1, 4, 1, 5) A=(3,1,4,1,5) and perform the following two operations.
As a result, the expected values of the elements in the final A A A are ( E 1 , E 2 , E 3 , E 4 , E 5 ) = ( 5 2 , 1 , 8 3 , 2 3 , 5 ) (E_1, E_2, E_3, E_4, E_5) = (\frac{5}{2}, 1, \frac{8}{3}, \frac{2}{3}, 5) (E1,E2,E3,E4,E5)=(25,1,38,32,5).
2 4
1 2
1 1 3
2 2 4
1 1 5
2 2 6
5 6
20 20
998769066 273215338 827984962 78974225 994243956 791478211 891861897 680427073 993663022 219733184 570206440 43712322 66791680 164318676 209536492 137458233 289158777 461179891 612373851 330908158
12 18 769877494
9 13 689822685
6 13 180913148
2 16 525285434
2 14 98115570
14 17 622616620
8 12 476462455
13 17 872412050
14 15 564176146
7 13 143650548
2 5 180435257
4 10 82903366
1 2 643996562
8 10 262860196
10 14 624081934
11 13 581257775
9 19 381806138
3 12 427930466
6 19 18249485
14 19 682428942
821382814 987210378 819486592 142238362 447960587 678128197 687469071 405316549 318941070 457450677 426617745 712263899 939619994 228431878 307695685 196179692 241456697 12668393 685902422 330908158
具体见文后视频。
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int SIZE = 2e5 + 10, MOD = 998244353;
int N, M;
int A[SIZE];
struct Node
{
int l, r;
int sum, add, mul;
}Tree[SIZE * 4];
void Pushup(int u)
{
Tree[u].sum = (Tree[u << 1].sum + Tree[u << 1 | 1].sum) % MOD;
}
void Eval(Node &tree, int add, int mul)
{
tree.sum = (tree.sum * mul + (tree.r - tree.l + 1) * add) % MOD;
tree.mul = (tree.mul * mul) % MOD;
tree.add = (tree.add * mul + add) % MOD;
}
void Pushdown(int u)
{
Eval(Tree[u << 1], Tree[u].add, Tree[u].mul);
Eval(Tree[u << 1 | 1], Tree[u].add, Tree[u].mul);
Tree[u].add = 0, Tree[u].mul = 1;
}
void Build(int u, int l, int r)
{
if (l == r) Tree[u] = {r, r, A[r], 0, 1};
else
{
Tree[u] = {l, r, 0, 0, 1};
int mid = l + r >> 1;
Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);
Pushup(u);
}
}
void Modify(int u, int l, int r, int add, int mul)
{
if (Tree[u].l >= l && Tree[u].r <= r) Eval(Tree[u], add, mul);
else
{
Pushdown(u);
int mid = Tree[u].l + Tree[u].r >> 1;
if (l <= mid) Modify(u << 1, l, r, add, mul);
if (r > mid) Modify(u << 1 | 1, l, r, add, mul);
Pushup(u);
}
}
int Query(int u, int l, int r)
{
if (Tree[u].l >= l && Tree[u].r <= r) return Tree[u].sum;
Pushdown(u);
int mid = Tree[u].l + Tree[u].r >> 1, sum = 0;
if (l <= mid) sum = Query(u << 1, l, r);
if (r > mid) sum = (sum + Query(u << 1 | 1, l, r)) % MOD;
return sum;
}
int qmi(int a, int b, int p)
{
int Result = 1;
while (b)
{
if (b & 1) Result = Result * a % p;
a = a * a % p;
b >>= 1;
}
return Result;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> N >> M;
for (int i = 1; i <= N; i ++)
cin >> A[i];
Build(1, 1, N);
while (M --)
{
int l, r, x;
cin >> l >> r >> x;
Modify(1, l, r, 0, (r - l) * qmi(r - l + 1, MOD - 2, MOD) % MOD);
Modify(1, l, r, x * qmi(r - l + 1, MOD - 2, MOD) % MOD, 1);
}
for (int i = 1; i <= N; i ++)
cout << Query(1, i, i) % MOD << " ";
return 0;
}
AtCoder Beginner Contest 332 讲解
最后祝大家早日