前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforces1025

codeforces1025

作者头像
attack
发布2018-09-17 15:29:44
3390
发布2018-09-17 15:29:44
举报

hackforces + fstforces

A

很明显当有一个字母出现次数>1时即合法

$n = 1$的情况需要特判

代码语言:javascript
复制
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#define Pair pair<int, int>
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
const int MAXN = 1e6 + 10;
using namespace std;
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;
char s[MAXN];
int num[MAXN];
int main() {
    N = read();
    if(N == 1) {
        puts("Yes"); return 0;
    }
    scanf("%s", s + 1);
    bool flag = 0;
    for(int i = 1; i <= N; i++) {
        num[s[i]]++;
        if(num[s[i]] >= 2) flag = 1;
    }
    puts(flag == 1 ? "Yes" : "No");
    return 0;
}
/*

*/

B

我的做法比较naive,直接对第一对数分解质因数,然后依次判断是否可行即可

TM一开始读错题了以为要输出最大的白浪费半个小时

代码语言:javascript
复制
#include<cstdio>
#include<cstdlib>
const int MAXN = 1e6 + 10;
int N, a[MAXN], b[MAXN], prime[MAXN], vis[MAXN * 10], tot = 0;
void check(int val) {
    for(int i = 1; i <= N; i++) if((a[i] % val) && (b[i] % val)) return ;
    printf("%d", val); exit(0);
}
void work(int x) {
    for(int i = 2; i * i <= x; i++) 
        if(x % i == 0) {check(i); while(x % i == 0) x /= i;}
    if(x != 1)check(x);
}
main() {
    scanf("%d", &N);
    for(int i = 1; i <= N; i++) scanf("%d %d", &a[i], &b[i]);
    work(a[1]); work(b[1]);
    puts("-1");
    return 0;
}

C

自己手玩一下就可以发现:把字符串从某一位置劈开再旋转无非就是换一种读字符串的方式

那么只要在字符串的所有循环阅读方式中找一个交错序列最长的就行

可以通过拼接一次实现

代码语言:javascript
复制
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#define Pair pair<int, int>
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
#define int long long 
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
const int MAXN = 1e6 + 10;
using namespace std;
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;
char s[MAXN];
main() {
    scanf("%s", s + 1);
    N = strlen(s + 1);
    for(int i = 1; i <= N; i++) s[i + N] = s[i];
    int mx = 0, cur = 0;
    N <<= 1;
    for(int i = 2; i <= N; i++)
        if(s[i] == s[i - 1]) mx = max(mx, cur), cur = 0;
        else cur++;
    mx = max(cur, mx);
    printf("%d", min(N / 2, mx + 1));
    return 0;
}
/*
bwbwbw
*/

D

比赛的时候一直以为是xjb贪心,居然忘了大力dp qwq

首先考虑一个很显然的区间dp, $f[l][r][root]$表示$(l, r)$区间内,以$root$为根是否可行

转移的时候需要枚举根,以及左儿子的根,右儿子的根。时间复杂度为$n ^ 4$

考虑我们在转移的时候仅仅需要知道左右儿子是否能拼接起来,因此我们换一种表示方法

$L[l][r]$表示以$r$为根,向左能否扩展到$l$

$R[l][r]$表示以$l$为根,向右能否扩展到$r$

转移的时候仍然需要枚举根节点,但是不需要枚举左右儿子。

时间复杂度$O(n^3)$

代码语言:javascript
复制
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int MAXN = 1001;
int N;
int a[MAXN], can[MAXN][MAXN], L[MAXN][MAXN], R[MAXN][MAXN], ans[MAXN][MAXN];
int main() {
    cin >> N;
    for(int i = 1; i <= N; i++) cin >> a[i], L[i][i] = R[i][i] = 1;
    for(int i = 1; i <= N; i++)
        for(int j = i + 1; j <= N; j++)
            can[i][j] = can[j][i] = (__gcd(a[i], a[j]) > 1);
    for(int len = 1; len <= N; len++)
        for(int l = 1; l + len - 1 <= N; l++) {
            int r = l + len - 1;
            for(int k = l; k <= r; k++)
                if(L[l][k] && R[k][r])
                    ans[l][r] = 1, L[l][r + 1] |= can[k][r + 1], R[l - 1][r] |= can[l - 1][k];
        }
    puts(ans[1][N] ? "Yes" : "No"); 
    return 0;
} 

E

F

据说是原题?fateice大爷抄了题解?https://blog.csdn.net/lych_cys/article/details/51000549

G

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • hackforces + fstforces
  • A
  • B
  • C
  • D
  • E
  • F
  • G
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档