A. Vasya and Chocolate
Simulation questions. Data will burst \ (int \) , to open \ (Long \) \ (Long \)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
int main(){
int T; scanf("%d", &T);
while(T--){
LL s, a, b, c;
scanf("%lld%lld%lld%lld", &s, &a, &b, &c);
LL buy = s / c, free = buy / a * b;
printf("%lld\n", buy + free);
}
return 0;
}
B. Vasya and Isolated Vertices
Considering the minimum, between two even sides, the answer is \ (max (n - 2 * m, 0) \)
Consider the maximum, except \ (m \) to \ (1 or 0 \) outside the special judge, each side trying to expand by up to a point not isolated, you can put \ (now - 1 \) edges even All sides to before.
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
LL n, m;
int main(){
cin >> n >> m;
cout << max(n - 2 * m, 0ll) << " ";
if(m == 0) printf("%lld\n", n);
else if(m == 1) printf("%lld\n", n - 2);
else{
int now = 2;
while(m - now + 1 > 0 && now < n) m -= (now - 1), now++;
printf("%lld\n", n - now);
}
return 0;
}
C. Make It Equal
I was too weak, just think of the \ (O (nlogn) \) approach ... is to use an array of tree maintenance and prefix, you can use \ (O (logn) \) time to calculate the costs, then get a pointer from the large to the small search on it ...
#include <cstdio>
#include <iostream>
#include <cmath>
#include <limits.h>
typedef long long LL;
using namespace std;
const int N = 200010;
int n, k, a[N], cnt[N], ans = 0, maxn = -1, minn = INT_MAX;
LL c[N];
void add(int x, LL k){
for(; x <= maxn; x += x & -x) c[x] += k;
}
LL ask(int x){
LL res = 0;
for(; x; x -= x & -x) res += c[x];
return res;
}
LL inline get(int x){
return ask(maxn) - ask(x - 1);
}
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d", a + i), cnt[a[i]]++, maxn = max(maxn, a[i]), minn = min(minn, a[i]);
for(int i = maxn; i >= 1; i--)
add(i, (LL)cnt[i] * i);
for(int i = maxn; i >= 1; i--)
cnt[i] += cnt[i + 1];
int i = maxn - 1;
while(i >= minn){
ans++;
while(i - 1 >= minn && get(i) - (LL)(i - 1) * cnt[i] <= k) i--;
add(i, (LL)i * (cnt[i + 1]) - get(i + 1));
i--;
}
printf("%d\n", ans);
return 0;
}
And in fact can handle suffix, it can be used in the process of the pointer jump down from the \ (O (n) \) time resolved spicy:
#include <cstdio>
#include <iostream>
#include <cmath>
#include <limits.h>
typedef long long LL;
using namespace std;
const int N = 200010;
int n, k, a[N], cnt[N], po[N], ans = 0, maxn = -1, minn = INT_MAX;
LL c[N];
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d", a + i), po[a[i]]++, cnt[a[i]]++, maxn = max(maxn, a[i]), minn = min(minn, a[i]);
for(int i = maxn; i >= 1; i--)
cnt[i] += cnt[i + 1];
int i = maxn - 1;
c[maxn] = (LL)maxn * po[maxn];
while(i >= minn){
ans++;
c[i] = c[i + 1] + (LL)i * po[i];
while(i - 1 >= minn && c[i] - (LL)(i - 1) * cnt[i] <= k)
i--, c[i] = c[i + 1] + (LL)i * po[i];;
c[i] += (LL)i * (cnt[i + 1]) - c[i + 1];
i--;
}
printf("%d\n", ans);
return 0;
}
D. Three Pieces
\ (pair <int, int> \) comes with a comparison function, so save a lot of effort. Write a priority queue \ (bfs \) would provoke too ...
There will be a total of $ 3 * N ^ 4 $ states, each state should extend up to expand \ (4 * N \) level, in fact, will be less.
A total complexity \ (O (N * ^ 12 is. 5) \) , do not \ (TLE \) .
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 15;
int n, a[N][N];
PII num[N * N];
//0: 车、1: 马: 2、象
int dx[3][8] = {
{1, -1, 0, 0},
{1, 1, -1, -1, -2, -2, 2, 2},
{1, 1, -1, -1},
};
int dy[3][8] = {
{0, 0, 1, -1},
{2, -2, 2, -2, 1, -1, 1, -1},
{1, -1, 1, -1},
};
int size[3] = {4, 8, 4};
int ne[3] = {N, 1, N};
PII step[N][N][N * N][3];
struct Node{
int x, y, k, m, t, c;
};
bool operator < (const Node &x, const Node &y){
return x.t == y.t ? x.c > y.c : x.t > y.t;
}
bool inline check(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= n;
}
PII bfs(){
priority_queue<Node> q;
for(int i = 0; i < 3; i++){
q.push((Node){ num[1].first, num[1].second, 1, i, 0, 0});
step[num[1].first][num[1].second][0][i] = make_pair(0, 0);
}
while(!q.empty()){
Node u = q.top(); q.pop();
if(u.k >= n * n){
return make_pair(u.t, u.c);
}
for(int i = 0; i < size[u.m]; i++){
for(int j = 1; j <= ne[u.m]; j++){
int nx = u.x + dx[u.m][i] * j, ny = u.y + dy[u.m][i] * j;
int nm = u.m, nt = u.t + 1, nc = u.c;
int nk = (num[u.k + 1] == make_pair(nx, ny)) ? u.k + 1 : u.k;
PII now = make_pair(nt, nc);
if(!check(nx, ny)) break;
if(now < step[nx][ny][nk][nm]){
step[nx][ny][nk][nm] = now;
q.push((Node){nx, ny, nk, nm, nt, nc});
}
}
}
for(int i = 0; i < 3; i++){
if(i == u.m) continue;
int nx = u.x, ny = u.y;
int nm = i, nt = u.t + 1, nc = u.c + 1;
int nk = u.k;
PII now = make_pair(nt, nc);
if(!check(nx, ny)) break;
if(now < step[nx][ny][nk][nm]){
step[nx][ny][nk][nm] = now;
q.push((Node){nx, ny, nk, nm, nt, nc});
}
}
}
return make_pair(-1, -1);
}
int main(){
memset(step, 0x3f, sizeof step);
scanf("%d", &n);
ne[0] = ne[2] = n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &a[i][j]), num[a[i][j]] = make_pair(i, j);
PII res = bfs();
printf("%d %d\n", res.first, res.second);
return 0;
}
E. Side Transmutations
The number of combinations, I certainly will not ...
Set \ (A \) is the length of the set of characters.
For each segment $ [b_ {i - 1} + 1, b_i] $ $ (1 <= i <= m) $
This section provided the length of the \ (len = b [i] - (b [i - 1] + 1) + 1 = b [i] - b [i - 1] \)
It has $ A ^ {len} $ different options:
- Turn in the past, then there is a corresponding past \ (A ^ {len} - 1 \) kinds of programs (including not turn over same kind), since the equivalent operation may count, a count They, so its contribution to \ (\ frac {A ^ { len} * (A ^ {len} - 1)} {2} \)
- Turn over same, each corresponding to past programs only, so as \ (len {A} ^ \) .
Then adding the two solutions \ (* \) into \ (ANS \) can be.
For \ ([b_m + 1, n - b_m] \) of this election do not choose will not result in duplication, the contribution of the answer is:
\(A ^ {n - b_m - (b _m + 1) + 1} = A ^ {n - 2 * b_m }\)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MOD = 998244353;
const int N = 200010;
int n, m, A, b[N];
int power(int a, int b){
int res = 1;
while(b){
if(b & 1) res = (LL)res * a % MOD;
a = (LL)a * a % MOD;
b >>= 1;
}
return res;
}
int main(){
cin >> n >> m >> A;
for(int i = 1; i <= m; i++) scanf("%d", b + i);
LL ans = 1;
for(int i = 1; i <= m; i++){
LL now = power(A, b[i] - b[i - 1]);
ans = (ans * ((now + now * (now - 1) / 2) % MOD)) % MOD;
}
ans = (ans * power(A, (n - 2 * b[m]))) % MOD;
cout << ans;
return 0;
}