Codeforces Round356 div2
通过数: 3
线上的时候卡在第四题,由于答案的大小没有估计好(猜测最坏情况10^5个),所以没有猜测出相应算法。
返回去看第三题,发现十分简单WA了一发,格式错误一发过了。
看到第五题的时候大概还剩20分钟,代码能力欠缺没调出来。
a:
#include <bits/stdc++.h>
using namespace std;
int a[10];
int main()
{
while(scanf("%d", &a[0]) != EOF) {
for(int i = 1 ; i < 5 ; i++) scanf("%d", a + i);
sort(a, a + 5);
int sum = 0;
for(int i = 0 ; i < 5 ; i++) sum += a[i];
int res = sum;
for(int i = 0 ; i < 5 ; i++) {
if(i < 4 && a[i] == a[i + 1]) res = min(res, sum - a[i] * 2);
if(i < 3 && a[i] == a[i + 1] && a[i + 1] == a[i + 2]) res = min(res, sum - a[i] * 3);
}
printf("%d\n", res);
}
return 0;
}
B:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
int v[MAXN];
int main()
{
int n, a;
while(scanf("%d%d", &n, &a) != EOF) {
for(int i = 1; i <= n ; i++) scanf("%d", v + i);
int head = a, rear = a;
int res = 0;
while(head >= 1 && rear <= n) {
if(v[head] && v[rear]) {
res += 2;
if(head == rear) res--;
}
head--, rear++;
}
while(head >= 1) {
if(v[head]) res++;
head--;
}
while(rear <= n) {
if(v[rear]) res++;
rear++;
}
printf("%d\n", res);
}
return 0;
}
C:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 2;
int vis[MAXN], prime[MAXN], primecnt;
void init()
{
primecnt = 0;
memset(vis, 0, sizeof vis);
for(int i = 2 ; i <= 50 ; i++) {
if(vis[i] == 1) continue;
prime[primecnt++] = i;
for(int j = i ; j < MAXN ; j += i)
vis[j] = 1;
}
for(int i = 0 ; i < primecnt ; i++) {
if(prime[i] * prime[i] <= 100) prime[primecnt++] = prime[i] * prime[i];
else break;
}
}
char str[50];
int main()
{
init();
// for(int i = 0 ; i < primecnt ; i++) printf("%d ", prime[i]);
// puts("");
// printf("%d\n", primecnt);
int cnt = 0;
for(int i = 0 ; i < primecnt ; i++) {
printf("%d\n", prime[i]);
fflush(stdout);
scanf("%s", str);
if(str[0] == 'y') cnt++;
}
if(cnt >= 2) printf("composite\n");
else printf("prime\n");
fflush(stdout);
return 0;
}
D:
/*
对于一个数m,设a^3 <= m 且 (a+1)^3 > m
存在两种可能是最优的操作,一是m-a^3之后进行搜索,二是m取(a^3 - 1)然后然后-(a-1)^3进行搜索
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int MAXN = 1e5 + 5;
LL thr[MAXN];
int ans, tout[MAXN], out[MAXN];
LL ans2;
void init()
{
for(int i = 0 ; i < MAXN ; i++)
thr[i] = (LL)i * i * i;
}
LL dfs(LL u, int dep)
{
// printf("u = %I64d, dep = %d\n", u, dep);
// system("pause");
if(u == 0) {
if(dep > ans) {
ans = dep;
LL tans = 0;
for(int i = 0 ; i < dep ; i++) out[i] = tout[i], tans = tans + thr[tout[i]];
// if(dep == 9) {
// printf("out = "); for(int i = 0 ; i < dep ; i++) printf("%d ", out[i]);
// puts("");
// system("pause");
// }
ans2 = tans;
return 0;
}
else if(dep == ans) {
LL tans = 0;
int ok = 0;
for(int i = 0 ; i < dep ; i++) tans = tans + thr[tout[i]];
ans2 = max(ans2, tans);
return 0;
}
else return -1;
}
else {
LL m = u;
int mark = upper_bound(thr, thr + MAXN, m) - thr;
mark--;
tout[dep] = mark;
LL t1 = dfs(m - thr[mark], dep + 1);
tout[dep] = mark - 1;
LL t2;
if(mark != 1)
t2 = dfs(thr[mark] - 1 - thr[mark - 1], dep + 1);
else t2 = -1;
if(t1 == -1 && t2 == -1) return -1;
else return 0;
}
}
int main()
{
init();
LL m;
while(scanf("%I64d", &m) != EOF) {
ans = 0;
dfs(m, 0);
printf("%d %I64d\n", ans, ans2);
}
return 0;
}
E:
/*
枚举消灭矩形的左上角端点,在矩形内部的非障碍点连通块需要做相应处理
每次右移,然后最左边一列的点重新加入原连通块,右边一列的点从原连通块中取出
说白了就是模拟
线上的时候,由于复杂度没有算清楚采取一种十分复杂的计算方法,就是把矩形先左移至右端点,然后下移一格,在左移至左端点的S型走法
实际上,每次都新的从一行最左边开始,复杂度是可以承受的
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 + 2;
char g[MAXN][MAXN];
int bccno[MAXN][MAXN], bcccnt, num[MAXN * MAXN];
int vis[MAXN * MAXN];
//int sum[MAXN][MAXN];
int n, m;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
bool validPos(int x, int y){if(x >= 1 && x <= n && y >= 1 && y <= n) return true; else return false;}
void dfs(int x, int y)
{
num[bcccnt]++;
bccno[x][y] = bcccnt;
for(int i = 0 ; i < 4 ; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if(validPos(tx, ty) && bccno[tx][ty] == -1 && g[tx][ty] != 'X') dfs(tx, ty);
}
}
int caledge(int le, int ri, int up, int down)
{
vector<int>vc;
vc.clear();
if(le > 1) {
for(int i = up ; i <= down ; i++)
if(bccno[i][le - 1] != -1 && vis[bccno[i][le - 1]] == 0) {
vis[bccno[i][le - 1]] = 1;
vc.push_back(bccno[i][le - 1]);
}
}
if(ri < n) {
for(int i = up ; i <= down ; i++)
if(bccno[i][ri + 1] != -1 && vis[bccno[i][ri + 1]] == 0) {
vis[bccno[i][ri + 1]] = 1;
vc.push_back(bccno[i][ri + 1]);
}
}
if(up > 1) {
for(int i = le ; i <= ri ; i++)
if(bccno[up - 1][i] != -1 && vis[bccno[up - 1][i]] == 0) {
vis[bccno[up - 1][i]] = 1;
vc.push_back(bccno[up - 1][i]);
}
}
if(down < n) {
for(int i = le ; i <= ri ; i++)
if(bccno[down + 1][i] != -1 && vis[bccno[down + 1][i]] == 0) {
vis[bccno[down + 1][i]] = 1;
vc.push_back(bccno[down + 1][i]);
}
}
int res = 0;
for(int i = 0 ; i < (int)vc.size() ; i++) res += num[vc[i]], vis[vc[i]] = 0;
// if(up == 2 && down == 4 && le == 2) {
// for(int i = 0 ; i < (int)vc.size() ; i++) {
// printf("%d, num = %d\n", vc[i], num[vc[i]]);
// }
// }
vc.clear();
return res;
}
vector<int>bc;
int main()
{
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 1 ; i <= n ; i++) scanf("%s", g[i] + 1);
memset(bccno, -1, sizeof bccno);
memset(vis, 0, sizeof vis);
// memset(sum, 0, sizeof sum);
bcccnt = 0;
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= n ; j++) {
// sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
if(g[i][j] == '.' && bccno[i][j] == -1) {
bcccnt++;
num[bcccnt] = 0;
dfs(i, j);
}
}
}
// for(int i = 1 ; i <= n ; i++) {
// for(int j = 1 ; j<= n ; j++) printf("%d ", bccno[i][j]);
// printf("\n");
// }
// printf("num[1] =%d\n", num[1]);
int res = 0;
for(int i = 1 ; i <= bcccnt ; i++) res = max(res, num[i]);
int le = 1, ri = m, up = 1, down = m;
for(int i = 1 ; i <= m ; i++) {
for(int j = 1 ; j <= m ; j++) {
if(bccno[i][j] != -1) num[bccno[i][j]]--;
}
}
res = max(res, caledge(le, ri, up, down) + m * m);
int flag = 1;
while(down <= n) {
int tres;
if(flag == 1) {
if(ri == n) {
for(int i = le ; i <= ri ; i++) if(bccno[up][i] != -1) num[bccno[up][i]]++;
for(int i = le ; i <= ri ; i++) if(bccno[down + 1][i] != -1) num[bccno[down + 1][i]]--;
up++; down++;
if(down > n) break;
tres = caledge(le, ri, up, down) + m * m;
res = max(res, tres);
flag = -1;
}
else {
for(int i = up ; i <= down ; i++) if(bccno[i][le] != -1) num[bccno[i][le]]++;
for(int i = up ; i <= down ; i++) if(bccno[i][ri + 1] != -1) num[bccno[i][ri + 1]]--;
le++, ri++;
tres = caledge(le, ri, up, down) + m * m;
res = max(res, tres);
}
}
else {
if(le == 1) {
for(int i = le ; i <= ri ; i++) if(bccno[up][i] != -1) num[bccno[up][i]]++;
for(int i = le ; i <= ri ; i++) if(bccno[down + 1][i] != -1) num[bccno[down + 1][i]]--;
up++; down++;
if(down > n) break;
tres = caledge(le, ri, up, down) + m * m;
res = max(res, tres);
flag = 1;
}
else {
for(int i = up ; i <= down ; i++) if(bccno[i][le - 1] != -1) num[bccno[i][le - 1]]--;
for(int i = up ; i <= down ; i++) if(bccno[i][ri] != -1) num[bccno[i][ri]]++;
le--, ri--;
tres = caledge(le, ri, up, down) + m * m;
res = max(res, tres);
}
}
// printf("up = %d, down = %d, le = %d, ri = %d, tres = %d, num[1] = %d\n", up, down, le, ri, tres, num[1]);
}
printf("%d\n", res);
}
return 0;
}