Codeforces Round #694 (Div. 2)
A. Strange Partition
题意: 给定含 n n n个元素的数组 a a a和一个整数 x x x,你可以将数组中任意相邻数组合并,试让你求出所有【 a i / x a_i/x ai/x(向上取整)的和的】最大值与最小值。
题解: 因为除数是向上取整的,因此不合并求所有除数的和即为最大值,全部合并求除数即为最小值。
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define _for(i,a,n) for (int i = (a); i < (n); ++i)
#define _rep(i,a,n) for (int i = (a); i <= (n); ++i)
#define _per(i,n,a) for (int i = (n); i >= (a); --i)
#define dbg(x...) do { cout << #x << " -> "; err(x); } while (0)
void err () { cout << endl;}
template <class T, class... Ts>
void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...);}
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
typedef long long ll;
const int inf = 0x3f3f3f3f;
void run () {
int n = read(), x = read();
ll minAns = 0, maxAns = 0;
_rep (i, 1, n) {
ll a; cin >> a;
maxAns += ceil((double)a / x);
minAns += a;
}
minAns = ceil((double)minAns / x);
cout << minAns << " " << maxAns << endl;
}
int main () {
int _T = read();
while (_T--) run();
return 0;
}
B. Strange List
题意: 对于长度为 n n n的序列,给定一个除数 x x x。要求对序列进行如下操作:
- 如果某数 a i a_i ai能够被 x x x整除,则在序列后方增加 x x xg个 a i / x a_i / x ai/x。否则结束操作
- 对整个序列进行求和
题解: 年少无知的时候拿queue模拟过 MLE了,需要从中寻得规律。比如某数16被除数2整除后得到2个8,继而能被2整除得到4个4,再被2整除得到8个2,最后得到16个1结束。也就是说某个数
a
i
a_i
ai能被整除的次数有限,而当得到某个不能再被整除的数之后,将会结束后续的所有操作。
因此我们需要在原序列中找到能被整除次数
c
n
t
cnt
cnt最少的数,作为一个临界点。临界点之前的数能获得的权值为
a
i
∗
(
c
n
t
+
2
)
a_i * (cnt + 2)
ai∗(cnt+2),临界点及以后的数能获得的权值为
a
i
∗
(
c
n
t
+
1
)
a_i * (cnt + 1)
ai∗(cnt+1)
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define _for(i,a,n) for (int i = (a); i < (n); ++i)
#define _rep(i,a,n) for (int i = (a); i <= (n); ++i)
#define _per(i,n,a) for (int i = (n); i >= (a); --i)
#define dbg(x...) do { cout << #x << " -> "; err(x); } while (0)
void err () { cout << endl;}
template <class T, class... Ts>
void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...);}
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
int a[maxn];
void run () {
ll sum = 0;
int pos = 0;
int cnt = inf;
int n = read(), x = read();
_rep (i, 1, n) {
a[i] = read();
int s = 0;
int tem = a[i];
while (tem % x == 0) {
s++;
tem /= x;
}
if (s < cnt) {
pos = i;
cnt = s;
}
}
_for (i, 1, pos) sum += 1ll * (cnt + 2) * a[i];
_rep (i, pos, n) sum += 1ll * (cnt + 1) * a[i];
cout << sum << endl;
}
int main () {
int _T = read();
while (_T--) run();
return 0;
}
C. Strange Birthday Party
题意: 有 n n n个朋友,商店有 m m m种商品,这 m m m种商品按序号价格从小到大排列,对于每一个朋友给出一个序号 k k k,可以直接给朋友序号 k k k的商品价格的金钱或给朋友买一个序号小于等于 k k k的商品,且每种商品最多只能买一次,问需要花费的最少金钱
题解: 贪心,先满足 k k k值较大的朋友, k k k值较小的朋友满足不了的时候直接给钱就好了。
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define _for(i,a,n) for (int i = (a); i < (n); ++i)
#define _rep(i,a,n) for (int i = (a); i <= (n); ++i)
#define _per(i,n,a) for (int i = (n); i >= (a); --i)
#define dbg(x...) do { cout << #x << " -> "; err(x); } while (0)
void err () { cout << endl;}
template <class T, class... Ts>
void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...);}
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
int a[maxn];
int c[maxn];
void run () {
int n = read(), m = read();
_rep (i, 1, n) a[i] = read();
_rep (i, 1, m) c[i] = read();
sort (a + 1, a + n + 1, greater<int>());
int k = 1;
ll sum = 0;
_rep (i, 1, n) {
if (a[i] > k) sum += 1ll * c[k++];
else sum += 1ll * c[a[i]];
}
cout << sum << endl;
}
int main () {
int _T = read();
while (_T--) run();
return 0;
}