Educational Codeforces Round 40 (Rated for Div. 2)

自信不被FST

A. Diagonal Walking
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.

In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.

Your problem is to print the minimum possible length of the sequence of moves after the replacements.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 100) — the length of the sequence. The second line contains the sequence consisting of n characters U and R.

Output

Print the minimum possible length of the sequence of moves after all replacements are done.

Examples
input
Copy
5
RUURU
output
3
input
Copy
17
UUURRRRRUUURURUUU
output
13
Note

In the first test the shortened sequence of moves may be DUD (its length is 3).

In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).

UR和RU都可以代替为D,URU的话当然怎么选都行,顺序不会影响个数,虽然影响了这个序列

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int ans=0;
    for(int i=0;s[i];i++)
    {
        ans++;
        if(s[i]=='U')
        {
            if(s[i+1]=='R')
                i++;
        }
        else if(s[i]=='R')
        {
            if(s[i+1]=='U')
                i++;
        }
    }
    cout<<ans;
    return 0;
}
B. String Typing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard.

Initially, you have an empty string. Until you type the whole string, you may perform the following operation:

  • add a character to the end of the string.

Besides, at most once you may perform one additional operation: copy the string and append it to itself.

For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.

If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.

Print the minimum number of operations you need to type the given string.

Input

The first line of the input containing only one integer number n (1 ≤ n ≤ 100) — the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.

Output

Print one integer number — the minimum number of operations you need to type the given string.

Examples
input
Copy
7
abcabca
output
5
input
Copy
8
abcdefgh
output
8
Note

The first test described in the problem statement.

In the second test you can only type all the characters one by one.

 

 一个字符串,你可以一个一个的打印,还可以直接复制前面的字符串粘贴,那就是找前半串的最长字符串

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int ans=1;
    string c;
    for(int i=1;s[i];i++)
    {
        c=s.substr(0,i);
        if(s.substr(i,c.length())==c)ans=c.length();
    }
    cout<<n-ans+1;
    return 0;
}
C. Matrix Walk
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a matrix A of size x × y filled with integers. For every  Ai, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix.

You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, ..., an denoting that you started in the cell containing the number a1, then moved to the cell with the number a2, and so on.

From the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells:

  1. (i + 1, j) — only if i < x;
  2. (i, j + 1) — only if j < y;
  3. (i - 1, j) — only if i > 1;
  4. (i, j - 1) — only if j > 1.

Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a1, then move to the cell containing a2 (in one step), then move to the cell containing a3 (also in one step) and so on. Can you choose x and y so that they don't contradict with your sequence of moves?

Input

The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of cells you visited on your path (if some cell is visited twice, then it's listed twice).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers in the cells on your path.

Output

If all possible values of x and y such that 1 ≤ x, y ≤ 109 contradict with the information about your path, print NO.

Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.

Examples
input
Copy
8
1 2 3 6 9 8 5 2
output
YES
3 3
input
Copy
6
1 2 1 2 5 3
output
NO
input
Copy
2
1 10
output
YES
4 9
Note

The matrix and the path on it in the first test looks like this:

Also there exist multiple correct answers for both the first and the third examples.

情况太多了,大力模拟

#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int a[N];
int main()
{
    int n;
    int maxx=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        maxx=max(maxx,a[i]);
    }
    int tmp=-1;
    for(int i=1;i<n;i++)
    {
        if(a[i]-a[i-1]==0)
        {
            cout<<"NO"<<endl;
            return 0;
        }
        if(tmp==-1&&abs(a[i]-a[i-1])!=1)
        {
            tmp=abs(a[i]-a[i-1]);
        }
        else
        {
            if(abs(a[i]-a[i-1])!=tmp&&abs(a[i]-a[i-1])!=1)
            {
                cout<<"NO"<<endl;
                return 0;
            }
        }
    }
    if(tmp==-1)
    {
        cout<<"YES"<<endl;
        cout<<maxx<<" "<<1<<endl;
        return 0;
    }
       for(int i=1;i<n;i++)
       {
           if(abs(a[i]-a[i-1])==1&&min(a[i],a[i-1])%tmp==0)
           {
               cout<<"NO"<<endl;
               return 0;
           }
       }
       cout<<"YES"<<endl;
    cout<<maxx/tmp+1<<" "<<tmp<<endl;
    return 0;
}
D. Fight Against Traffic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them.

In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction s to work located near junction t. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease.

You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won't decrease.

Input

The firt line of the input contains integers nms and t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, 1 ≤ s, t ≤ ns ≠ t) — the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The i-th of the following m lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi), meaning that this road connects junctions ui and vi directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.

Output

Print one integer — the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions s and t.

Examples
input
Copy
5 4 1 5
1 2
2 3
3 4
4 5
output
0
input
Copy
5 4 3 5
1 2
2 3
3 4
4 5
output
5
input
Copy
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
output
3

 

 给你n个点m条边的无向图,从s到t的最短路不变的话你最多可以加几条边,无非就是判断这些边是否合理,那么我就求下两个点分别到s t的距离,然后判断是否合法

#include <bits/stdc++.h>
using namespace std;
const int N=1005;
int n,m,pre[N],s,t;
set<int> G[N];
int d1[N],d2[N];
priority_queue<int> Q;
int main()
{
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for(int i=0,u,v; i<m; i++)
        scanf("%d%d",&u,&v),G[u].insert(v),G[v].insert(u);
    fill(d1,d1+n+1,N);
    d1[s]=0,Q.push(s);
    while(!Q.empty())
    {
        int u=Q.top();
        Q.pop();
        for(int E:G[u])
        {
            if(d1[u]+1<d1[E])d1[E]=d1[u]+1,Q.push(E);
        }
    }
    fill(d2,d2+n+1,N);
    d2[t]=0,Q.push(t);
    while(!Q.empty())
    {
        int u=Q.top();
        Q.pop();
        for(int E:G[u])
        {
            if(d2[u]+1<d2[E])d2[E]=d2[u]+1,Q.push(E);
        }
    }
    int ans=0;
    for(int i=1; i<n; i++)
        for(int j=i+1; j<=n; j++)
            if(G[i].count(j)==0&&d1[i]+1+d2[j]>=d1[t]&&d1[j]+1+d2[i]>=d1[t])
            ans++;
    cout<<ans;
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/8625855.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值