A
打了个表,然后找第一个大于它的
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i < n; ++i)
bool Check(LL x){
int cnt = 0;
while(x > 0){
if(x % 10 != 0) cnt++;
x /= 10;
}
if(cnt <= 1) return true;
return false;
}
LL num[1005];
int main(){
LL n; scanf("%lld", &n);
int cnt = 0;
for(int i = 1; i < 10; ++i){
num[cnt++] = i;
}
LL t = 10;
for(int i = 1; i <= 9; ++i){
for(int j = 1; j < 10; ++j)
num[cnt++] = j * t;
t = t * 10;
}
for(int i = 0; i < cnt; ++i){
if(num[i] > n){
printf("%lld\n", num[i] - n);
break;
}
}
return 0;
}
B
连续K个取和,最后对总和除一个n - k + 1
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i < n; ++i)
LL num[200005];
int main(){
int n, k; scanf("%d%d", &n, &k);
REP(i, 0, n){
scanf("%lld", num + i);
}
double sum = 0;
LL tmp = 0;
for(int i = 0; i < k - 1; ++i)
tmp += num[i];
for(int i = k - 1; i < n; ++i){
tmp += num[i];
sum += tmp;
tmp -= num[i - k + 1];
}
sum = sum / (n - k + 1);
printf("%.8f\n", sum);
return 0;
}
C
先按条件把每杯水放一半,此时所有人都是满足的,然后对于多余的水从ai最大的开始放即可
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i < n; ++i)
const int qq = 105;
struct Friend{
int a, cup;
int id;
bool operator < (const Friend &t)const{
return a < t.a;
}
}fri[qq];
int cmp(const Friend &a, const Friend &b){
return a.id < b.id;
}
int main(){
int n, w; scanf("%d%d", &n, &w);
for(int i = 0; i < n; ++i){
scanf("%d", &fri[i].a);
fri[i].id = i;
}
for(int i = n - 1; i >= 0; --i){
int p = (fri[i].a + 1) / 2;
fri[i].cup = p;
w -= p;
}
sort(fri, fri + n);
for(int i = n - 1; i >= 0; --i){
int p = fri[i].a - fri[i].cup;
if(w > p){
fri[i].cup = fri[i].a;
w -= p;
}else{
fri[i].cup = fri[i].cup + w;
break;
}
}
sort(fri, fri + n, cmp);
if(w < 0){
puts("-1");
return 0;
}
for(int i = 0; i < n; ++i){
printf("%d ", fri[i].cup);
}
puts("");
return 0;
}
D
这题题意感觉有点迷,题干里面说移动一些元素,output里面又说移动一个元素
我们可以枚举被移动的哪一个,那么这个只有一种可能要么移到前面那一组,要么移到后面那组,此时我们二分断点即可
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i < n; ++i)
const int qq = 1e5 + 10;
LL sum[qq];
LL num[qq];
bool Find(int l, int r, LL x){
while(l <= r){
int mid = (l + r) / 2;
if(sum[mid] == x) return true;
else if(sum[mid] > x) r = mid - 1;
else l = mid + 1;
}
return false;
}
int main(){
int n;
scanf("%d", &n);
LL p = 0;
for(int i = 1; i <= n; ++i) scanf("%lld", num + i), sum[i] = sum[i - 1] + num[i], p += num[i];
if(p % 2 == 1){
puts("NO");
return 0;
}
p /= 2;
bool f = false;
for(int i = 1; i <= n; ++i){
if(Find(i + 1, n, p + num[i])){
f = true;
}
}
for(int i = n; i >= 1; --i){
if(Find(1, i - 1, p - num[i])){
f = true;
}
}
if(f) puts("YES");
else puts("NO");
return 0;
}
E
这题有意思,其实实质上还是个01背包、有贪心的思想在里面
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i < n; ++i)
const int qq = 3e5 + 10;
struct Backpack{
LL val;
int m1, m2;
}dp[qq];
int num[4];
LL Val[4][qq];
LL pre[4][qq];
int n, m;
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i){
int w; LL val; scanf("%d%lld", &w, &val);
Val[w][++num[w]] = val;
}
for(int i = 1; i < 4; ++i){
sort(Val[i] + 1, Val[i] + 1 + num[i], greater<LL>() );
for(int j = 1; j <= num[i]; ++j)
pre[i][j] = pre[i][j - 1] + Val[i][j];
}
dp[0].val = dp[0].m1 = dp[0].m2 = 0;
for(int i = 1; i <= m; ++i){
dp[i] = dp[i - 1];
if(dp[i].val < dp[i - 1].val + Val[1][dp[i - 1].m1 + 1]){
dp[i].val = dp[i - 1].val + Val[1][dp[i - 1].m1 + 1];
dp[i].m1 = dp[i - 1].m1 + 1;
dp[i].m2 = dp[i - 1].m2;
}
if(i >= 2 && dp[i].val < dp[i - 2].val + Val[2][dp[i - 2].m2 + 1]){
dp[i].val = dp[i - 2].val + Val[2][dp[i - 2].m2 + 1];
dp[i].m1 = dp[i - 2].m1;
dp[i].m2 = dp[i - 2].m2 + 1;
}
}
LL ans = 0;
for(int i = 0; i <= num[3]; ++i){
if(m >= i * 3){
ans = max(ans, pre[3][i] + dp[m - 3 * i].val);
}
}
printf("%lld\n", ans);
}