Educational Codeforces Round 40 (Rated for Div. 2)


/*
B:
题意:给定一个字符串,让你去构造它;有两种构造的方式:
a,在末尾加一个字母;
b,将前面的已有的字符复制一次加到末尾(这个操作只能执行一次)
问最少执行多少次操作可以得到给定的字符;

思路:看给定的字符串中有哪些位置是可以进行复制操作的,
然后枚举可以进行操作的起点,更新最小值;

*/
#include<bits/stdc++.h>
using namespace std;
char a[105];
int b[105];
int main()
{
    int n;
    memset(b,0,sizeof(b));
    scanf("%d",&n);
    scanf("%s",a);
    int cnt,mi = 105;
    for(int i = 0; i < n; i++)
    {
        //cout<<i<<endl;
        int k = 0;
        for(int j = i+1; j < n; j++)
        {
            //cout<<"ok"<<endl;
            if(a[k] != a[j])
                break;
            k++;
            //cout<<k<<endl;
            if(k == i+1)
            {
                k = 0;
                b[i]++;
                break;
            }
        }
    }
    //for(int i = 0; i < n; i++)
        //cout<<b[i]<<endl;
    for(int i = 0; i < n; i++)
    {
        if(b[i])
            cnt = n-i;
        else
            cnt = n;
        mi = min(mi,cnt);
    }
    printf("%d\n",mi);
    return 0;
}
/*
C:

题意:从矩阵中的某个位置出发,每次可向上下左右移动一步,
每到一个位置,记录下此位置上的数,如此可得到一个序列。
现给定序列 ,问是否存在x行y列的矩阵,使得这个序列成立;

思路:序列中两个数之差要么是1要么是y(列数),行数可以是任意
的,先找出列数,然后判断每个a[i]是否合理;

*/
#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL long long
LL a[200010];
int main()
{
    int n;
    int h,d = 1000000000;
    scanf("%d",&n);
    LL ma = 0;
    for(int  i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
        ma = max(ma,a[i]);
    }
    for(int i = 1; i < n; i++)
    {
        if(fabs(a[i] - a[i-1]) != 1)
            d = fabs(a[i] - a[i-1]);
    }
    if(d == 0)
    {
        printf("NO\n");
        return 0;
    }
    //printf("d:%d\n",d);
    for(int i = 0; i < n-1; i++)
    {
        //cout<<i<<endl;
        if(a[i] == 1)
        {
            if(a[i+1] == a[i]+1 || a[i+1] == a[i]+d)
            {
                continue;
            }
            else
            {
                printf("NO\n");
                return 0;
            }

        }
        else if(a[i] == d)
        {
            if(a[i+1] == a[i]-1 || a[i+1] == a[i]+d)
            {
                continue;
            }printf("NO\n");
                return 0;
        }
        else if(a[i] > 1 && a[i] < d)
        {
            if(a[i+1] == a[i]-1 || a[i+1] == a[i]+1 || a[i+1] == a[i]+d)
            {
                continue;
            }printf("NO\n");
                return 0;
        }
        else if(a[i]%d == 1)
        {
            if(a[i+1] == a[i] + 1 || a[i+1] == a[i] - d ||a[i+1] == a[i] + d)
            {
                continue;
            }printf("NO\n");
                return 0;
        }
        else if(a[i]%d == 0)
        {
            if(a[i+1] == a[i] - 1 || a[i+1] == a[i] - d ||a[i+1] == a[i] + d)
            {
                continue;
            }printf("NO\n");
                return 0;
        }
        else if(a[i] > d)
        {
            if(a[i+1] == a[i] - 1 || a[i+1] == a[i] - d ||a[i+1] == a[i] + d || a[i+1] == a[i] + 1)
                continue;
            printf("NO\n");
                return 0;
        }
    }
    h = 1000000000;
    printf("YES\n%d %d\n",h,d);
    return 0;
}

/*
D:
题意:给你一个联通图,每条边的距离为1,给出起点
和终点,加一条边,使起点到终点的最短路不变,问有
多少种加边方案,已有的边不能再加;

思路:用bfs(或者spfa)求出起点,终点到各个点的最短路,
然后枚举加的边;

(ps:假如枚举的是i,j这条边,要考虑:起点->i->j->终点,
起点->j->i->终点);
*/
#include<bits/stdc++.h>
using namespace std;
struct node
{
    int u,v;
}cl[1000];
vector<int> g[1005];
queue<int> q;
int mp[1005][1005];
bool vis[1005];
int ss[1005],st[1005];
int n,m;
void bfs(int x,int *step)
{
    memset(step,0,sizeof(step));
    memset(vis,0,sizeof(vis));
    int now = x;
    vis[x] = 1;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i = 0; i < g[now].size(); i++)
        {
            int next= g[now][i];
            if(!vis[next])
            {
                step[next] = step[now]+1;
                vis[next] = 1;
                q.push(next);
            }
        }
    }
    //cout<<x<<":\n";
    //for(int i = 1; i <= n; i++)
        //cout<<step[i]<<endl;
}
int main()
{
    int s,t;
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for(int i = 1; i <= n; i++)
        mp[i][i] = 1;
    for(int i = 0; i < m; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
        mp[x][y] = 1;
        mp[y][x] = 1;
    }
    int cnt = 0;
    bfs(s,ss);
    int d = ss[t];
    bfs(t,st);
    for(int i = 1; i <= n; i++)
    {
        for(int j = i+1; j <= n; j++)
        {
            if(!mp[i][j])
            {
                if(ss[i] + 1 + st[j] < d)
                    continue;
                if(ss[j] + 1 + st[i] < d)
                    continue;
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值