算法马拉松24

本文解析了算法马拉松24的四道题目:小C的多边形、逆序对统计、俄罗斯方块,提供了详细题解及代码实现,特别关注了数据结构与算法优化。

算法马拉松24

A 小C的多边形

  • 题意:

n+1个点的多边形。给外圈的边标记上1~n,里圈的边也标记上1~n,使得对于一个外圈相邻点与中间点构成的三角形的边权之和都相等。\(n \le 10^6\)

  • 题解:

显然每个三角形权值和为\(\frac{3(n+1)}{2}\)

一开始简化成n个数排一个环,相邻两个数的和不相等并且有上下界,然后并不好做

构造了一下n=5发现外圈正好1..5,内圈1,2之间填n

然后这样写一下交上就T了...不加输出优化tle 2333

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

char c[20];
inline void put(int x) {
    int p = 0;
    while(x) c[++p] = x%10 + '0', x /= 10;
    while(p) putchar(c[p--]);
}
int n;
void solve() {
    for(int i=1; i<=n; i++) put(i), putchar(' ');
    puts("");
    int sum = (n+1)/2*3-1, now = (n+3)/2-1;
    for(int i=1; i<=n; i++) {
        put(now); putchar(' ');
        now = sum - now;
        sum--;
    }
}
int main() {
//  freopen("in", "r", stdin);
    scanf("%d", &n); n--;
    if(~n&1) puts("0");
    else solve();
}


B 逆序对统计

  • 题意:

n个位置,\(1..m\)中每个数可以放在某一个位置,求逆序对最多个数。\(n \le 20, m \le 100\)

  • 题解:

比赛时几乎想到正解了qwq

从小到大枚举数,然后放一个数只会与他位置后面的数构成逆序对,把n状压一下就行了

但当时认为如果位置i已经有数了,还要减去位置i已经构成的逆序对个数,没法维护

其实完全不用考虑有数的情况,加入再删除和没加入是一样的,从没数的状态可以转移呀

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 105, M = (1<<20) + 5, INF = 1e9;
inline int read(){
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, m, a[N], all, one[M], f[2][M], cur;
void print(int x) {
    for(int i=n-1; i>=0; i--) printf("%c", (x & (1<<i)) ? '1' : '0'); puts("");
}
int main() {
    freopen("in", "r", stdin);
    n=read(); m=read();
    for(int i=1; i<=m; i++) a[i] = read() - 1;
    all = 1<<n;
    for(int i=0; i<=n; i++) one[1<<i] = 1;
    for(int i=1; i<all; i++) one[i] = one[i&-i] + one[i^(i&-i)];

    memset(f, -1, sizeof(f));
    f[cur][0] = 0;
    for(int i=0; i<m; i++, cur ^= 1) { 
        int *g = f[cur], *d = f[cur^1];
        for(int s=0; s<all; s++) if(g[s] != -1) { //printf("f %d %d  %d\n", i, s, g[s]); print(s);
            d[s] = max(d[s], g[s]);
            if(~ s & (1<<a[i+1])) {
                int ns = s | (1<<a[i+1]); 
                d[ns] = max(d[ns], g[s] + one[ns >> (a[i+1] + 1)]);
            }
            g[s] = -1;
        }
    }
    printf("%d\n", f[cur][all-1]);
}


C 俄罗斯方块

  • 题意:

\(n * m \le 10^7\)的01网格,每次将一个俄罗斯方块区域异或,问是否能全0.

  • 题解:

稍微玩一下发现可以做到:

  1. 异或两个相邻格
  2. 将一个1格任意移动

这样的话1的个数为奇数一定可行啊!

然而我忽略了网格大小,至少要是2*3才行!

这样的话特判一下2*2 和1*x

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e7+5;
inline int read(){
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, m, a[N];
char s[N];
int main() {
    freopen("in", "r", stdin);
    int T = read();
    while(T--) {
        n = read(); m = read();
        if(n == 1 || m == 1) {
            if(n == 1) {scanf("%s", s+1); n = m; for(int i=1; i<=n; i++) a[i] = s[i] - '0';}
            else for(int i=1; i<=n; i++) a[i] = read();
            int flag = 1;
            for(int i=1; i<=n; i++) if(a[i]) {
                if(i+3 > n) {flag = 0; break;}
                for(int j=i; j<=i+3; j++) a[j] ^= 1;
            }
            puts(flag ? "Yes" : "No");
            continue;
        }
        int cnt = 0;
        for(int i=1; i<=n; i++) {
            scanf("%s", s+1);
            for(int j=1; j<=m; j++) cnt += (s[j] - '0') & 1;
        }
        if(n > m) swap(n, m);
        if(n >= 2 && m >= 3) puts((cnt & 1) ? "No" : "Yes");
        else if(n == 2 && m == 2) puts(cnt == 4 || cnt == 0 ? "Yes" : "No");
    }
}


D 单独写了 E F弃疗

转载于:https://www.cnblogs.com/candy99/p/6793047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值