codeforces#510 Div2

pre过了三题 后来A题被hack了 B题终测挂了 两题其实都是有一个小细节没有处理好 中间C还因为cinT了一次

唉本来打的还不错的 还是太菜了 继续加油吧

A-Benches

有n张椅子 原来第i张上有ai个人 现在来了m个人

求最大值的最大和最小的可能

 #include <iostream>
#include<algorithm>
#include<stdio.h>
#include<set>
#include<cmath>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<stack> using namespace std; int n, m;
const int maxn = ;
int a[maxn]; int main()
{
while (cin >> n >> m) {
int maxpeo = -;
for (int i = ; i < n; i++) {
cin >> a[i];
maxpeo = max(maxpeo, a[i]);
} int maxk = maxpeo + m;
int sub = ;
for (int i = ; i < n; i++) {
sub += maxpeo - a[i];
}
int mink;
if(sub >= m){
mink = maxpeo;
}
else if ((m - sub) % n == ) {
mink = (m - sub) / n + maxpeo;
}
else {
mink = (m - sub) / n + + maxpeo;
} cout<<mink<<" "<<maxk<<endl;
}
}

B-Vitamins

有n瓶果汁,每瓶有一个价格 和 可能含有的维生素【最多只有ABC三种维生素】

现在想要用最少的钱集齐ABC

本来感觉是dp 写不出 所以先去写了C 后来发现其实贪心就能过

分类讨论一下 一种是 直接选三个维生素都有的里面价格最小的

一种是 选两个维生素加缺的里面价格最小的

一种是 选单个维生素价格最小的之和

存好含三种的果汁 两种的果汁 含A的果汁【不包括三种都有】含B的含C的

第一种可能只有一种情况

第二种可能 枚举 跑一遍

第三种可能也只有一种情况

比一下最小值

需要注意有可能有的数量是0

 #include <iostream>
#include<algorithm>
#include<stdio.h>
#include<set>
#include<cmath>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<stack> #define inf 0x3f3f3f3f using namespace std; const int maxn = ;
int n;
struct node {
int c;
char vitamin[];
}juice[maxn];
node a[maxn], b[maxn], c[maxn], three[maxn], two[maxn]; bool cmp(node a, node b)
{
return a.c < b.c;
} int main()
{
while (scanf("%d", &n) != EOF) {
int cnta = , cntb = , cntc = , cntth = , cnttwo = ;
for (int i = ; i < n; i++) {
scanf("%d %s", &juice[i].c, juice[i].vitamin);
if (strlen(juice[i].vitamin) == ) {
three[cntth++] = juice[i];
}
else if (strlen(juice[i].vitamin) == ) {
two[cnttwo++] = juice[i];
for (int j = ; j < ; j++) {
if (juice[i].vitamin[j] == 'A') {
a[cnta++] = juice[i];
}
else if (juice[i].vitamin[j] == 'B') {
b[cntb++] = juice[i];
}
else {
c[cntc++] = juice[i];
}
}
}
else {
if (juice[i].vitamin[] == 'A') {
a[cnta++] = juice[i];
}
else if (juice[i].vitamin[] == 'B') {
b[cntb++] = juice[i];
}
else {
c[cntc++] = juice[i];
}
}
} if (cntth == && (cnta == || cntb == || cntc == )) {
printf("-1\n");
continue;
} sort(three, three + cntth, cmp);
sort(two, two + cnttwo, cmp);
sort(a, a + cnta, cmp);
sort(b, b + cntb, cmp);
sort(c, c + cntc, cmp); int ans;
if(cntth == ){
ans = inf;
}
else{
ans = three[].c;
}
for (int i = ; i < cnttwo; i++) {
if (strcmp(two[i].vitamin, "AB") == || strcmp(two[i].vitamin, "BA") == ) {
ans = min(ans, two[i].c + c[].c);
}
else if (strcmp(two[i].vitamin, "AC") == || strcmp(two[i].vitamin, "CA") == ) {
ans = min(ans, two[i].c + b[].c);
}
else {
ans = min(ans, two[i].c + a[].c);
}
}
if(cnta != && cntb != && cntc != ){
ans = min(ans, a[].c + b[].c + c[].c);
} printf("%d\n", ans);
}
}

C-Array Product

给一组数列 做n-1次操作

有两种操作 :第一种将ai和aj相乘结果赋给aj,ai删除

第二种 将ai位置删除【这种操作最多只能一次】

其实也是贪心

首先 所有的0都要合并 然后删去

负数 如果是奇数个的话 删去绝对值最小的那个

如果既有0又有奇数个负数 就把这个负数和0合并再删去

剩下的这些全部照常做第一种操作就好了

 #include <iostream>
#include<algorithm>
#include<stdio.h>
#include<set>
#include<cmath>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<stack> #define inf 0x3f3f3f3f using namespace std; int n;
const int maxn = 2e5 + ;
int a[maxn];
bool vis[maxn]; int main()
{
//freopen("C:\\Users\\wyb\\Desktop\\tmpcode\\codeforces\\in.txt", "r", stdin);
//freopen("C:\\Users\\wyb\\Desktop\\tmpcode\\codeforces\\out.txt", "w", stdout);
while (scanf("%d", &n) != EOF) {
memset(vis, , sizeof(vis));
int cntmin = , lastzero = -, maxminpos, maxmin = -inf, cnt = ;
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == ) {
if (lastzero == -) {
lastzero = i;
}
else {
printf("1 %d %d\n", lastzero, i);
//cout << "1 " << lastzero << " " << i << endl;
vis[lastzero] = false;
lastzero = i;
cnt++;
}
}
if (a[i] < ) {
cntmin++;
if (maxmin < a[i]) {
maxmin = a[i];
maxminpos = i;
}
}
}
if (cntmin % && lastzero != -) {
if(cnt == n - ){
continue;
}
cnt++;
printf("1 %d %d\n", maxminpos, lastzero);
//cout << "1 " << maxminpos << " " << lastzero << endl;
if(cnt == n - ){
continue;
}
cnt++;
printf("2 %d\n", lastzero);
//cout << "2 " << lastzero << endl;
vis[maxminpos] = vis[lastzero] = false;
}
else if (lastzero != -) {
if(cnt == n - ){
continue;
}
cnt++;
printf("2 %d\n", lastzero);
//cout << "2 " << lastzero << endl;
vis[lastzero] = false;
}
else if(cntmin % ){
if(cnt == n - ){
continue;
}
cnt++;
printf("2 %d\n", maxminpos);
//cout<<"2 "<<maxminpos<<endl;
vis[maxminpos] = false;
} int last = -;
for (int i = ; i <= n; i++) {
if (vis[i]) {
if (last == -) {
last = i;
}
else {
if(cnt == n - ){
break;
}
cnt++;
printf("1 %d %d\n", last, i);
//cout << "1 " << last << " " << i << endl;
last = i;
}
}
}
//cout<<endl;
}
}
上一篇:Java用Scanner类获取用户输入


下一篇:libstdc++.so.5: undefined reference to `memcpy@GLIBC_2.14'