Codeforces Round #694
A. Strange Partition
bi/x是向上取整的。
所以不进行不对b中的元素进行合并的操作,所得到的值是最大的,反之,全部加起来除x值最小。
向上取整:ceil ; 向下取整:floor ; 四舍五入: round ;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
int main(){
int t;
ll a[N], n, x;
cin >> t;
while(t--){
ll maxx = 0, minn = 0;
cin >> n >> x;
for(int i=1; i<=n; ++i){
cin >> a[i];
maxx += ceil(a[i]*1.0/x);
minn += a[i];
}
minn = ceil(minn*1.0/x);
cout << minn <<' ' << maxx << endl;
}
}
B. Strange List
如果q能被x整除,则加上x个q/x,q/xq=q,q/x/xx*x=q;递推得,如果q被x整除,q/x能被x整除,q/x/x等能被x整除,每次加上的值都是q。用队列来模拟。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
struct node{
ll now, next;
};
int main(){
int t;
ll n , x;
node a;
queue <node> q;
while(!q.empty()) q.pop();
scanf("%d",&t);
while(t--){
ll sum = 0;
scanf("%lld%lld", &n, &x);
for(int i=1; i<=n; ++i){
scanf("%lld", &a.now);
a.next = a.now;
q.push(a);
}
while(!q.empty()){
if (q.front().next%x == 0){
a.next = q.front().next/x;
a.now = q.front().now;
sum += q.front().now;
q.push(a);
q.pop();
}
else {
sum += q.front().now;
q.pop();
break;
}
}
while(!q.empty()){
sum += q.front().now;
q.pop();
}
printf("%lld\n", sum);
}
}
C. Strange Birthday Party
由题干知c是递增的,n个朋友每人分配一个整数k,朋友可以得到价值cj的礼物或cki的现金。如果k值越大,需要的现金也就越多,因此k大的朋友要给他们发价值小的礼物直到礼物发完。
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
typedef long long ll;
ll k[N], c[N], vis[N];
int t;
ll n, m, sum = 0;
bool cmp(ll a, ll b){
return a > b;
}
int main(){
cin >> t;
while(t--){
memset(vis, 0, sizeof(vis));
sum = 0;
cin >> n >> m;
for(int i=1; i<=n; ++i)
cin >> k[i];
for(int i=1; i<=m; ++i)
cin >> c[i];
sort(k+1, k+n+1, cmp);
for(int i=1; i<=n; ++i){
if(i<=m && c[i] < c[k[i]] && !vis[i]){
sum += c[i];
vis[i] = 1;
}
else sum += c[k[i]];
}
cout << sum << endl;
}
}