您的位置:首页 > 其它

Codeforces Round #265 (Div. 2)

2014-09-08 17:40 966 查看
A. inc ARG

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting of n bits. These bits are numbered from 1 to n.
An integer is stored in the cell in the following way: the least significant bit is stored in the first bit of the cell, the next significant bit is stored in the second bit, and so on; the most significant bit is stored in the n-th
bit.

Now Sergey wants to test the following instruction: "add 1 to the value of the cell". As a result of the instruction, the integer that is written in the cell must
be increased by one; if some of the most significant bits of the resulting number do not fit into the cell, they must be discarded.

Sergey wrote certain values ​​of the bits in the cell and is going to add one to its value. How many bits of the cell will change after the operation?

Input

The first line contains a single integer n (1 ≤ n ≤ 100)
— the number of bits in the cell.

The second line contains a string consisting of n characters — the initial state of the cell. The first character denotes the state of the first bit of the
cell. The second character denotes the second least significant bit and so on. The last character denotes the state of the most significant bit.

Output

Print a single integer — the number of bits in the cell which change their state after we add 1 to the cell.

Sample test(s)

input
4
1100


output
3


input
4
1111


output
4


Note

In the first sample the cell ends up with value 0010, in the second sample — with 0000.

对于A题这种水题最烦人的就是题意的理解,这个题目的意思是将第一位+1,看后面有几位二进制发生改变

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
string str;
int main()
{
    int n;
    while(cin>>n)
    {
        cin>>str;
        int res = 0;
        str[0] += 1;
        for(int i = 0; i < n; i++)
        {
            if(str[i]  == '2')
            {
                res++;
                if(i+1 == n) break;
                str[i+1] += 1;
            }
            else {res++;break;}
        }
        cout<<res<<endl;
    }
    return 0;
}


B. Inbox (100500)

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.

Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In
one click he can do any of the following operations:

Move from the list of letters to the content of any single letter.

Return to the list of letters from single letter viewing mode.

In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.

The program cannot delete the letters from the list or rearrange them.

Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?

Input

The first line contains a single integer n (1 ≤ n ≤ 1000)
— the number of letters in the mailbox.

The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th
number equals either 1, if the i-th number is unread, or 0,
if the i-th letter is read.

Output

Print a single number — the minimum number of operations needed to make all the letters read.

Sample test(s)

input
5
0 1 0 1 0


output
3


input
5
1 1 0 0 1


output
4


input
2
0 0


output
0


Note

In the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.

In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.

In the third sample all letters are already read.

这题我觉得应该用贪心做,但我贪心不知道怎么用,于是用动态规划做的,首先dp记录每位前面有几个连续的1(包括它本身),每次操作完

以后,将这块连续的1置为0,然后继续找最大的那个dp,如果不是找最大的会多算

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[1005];//代表每位数组前面连续有几个1(包括其本身在内)
int num[1005];

int main()
{
int n;
while(cin>>n)
{
memset(dp,0,sizeof(dp));
int Max,index;
for(int i = 1; i <= n; i++)
{
cin>>num[i];
if(num[i] == 1) dp[i] = dp[i-1] + 1;
if(dp[i] > dp[i-1])
{
index = i;
Max = dp[i];//开始记录最大的dp[i]和它的位置
}
}

int res = 0;
while(1)
{
res += Max;//记住选择一次要算在内,所以是+Max而不是+Max-1
for(int j = index; j > index - Max; j--)
{
dp[j] = 0;//把浏览过的置为0 }
Max = 0;
for(int i = 1; i <= n ; i++)//重新找最大的dp
{
if(dp[i] > dp[i-1])
{
index = i;
Max = dp[i];
}
}

if(Max == 0) break;//如果全都为0,退出
res += 1;//每次都要返回目录+1

}
cout<<res<<endl;

}
return 0;
}


C. No to Palindromes!

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Paul hates palindromes. He assumes that string s is tolerable if
each its character is one of the first p letters of the English alphabet ands doesn't
contain any palindrome contiguous substring of length 2 or more.

Paul has found a tolerable string s of length n.
Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.

Input

The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26).
The second line contains string s, consisting of n small
English letters. It is guaranteed that the string is tolerable (according to the above definition).

Output

If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).

Sample test(s)

input
3 3cba


output
NO


input
3 4cba


output
cbd


input
4 4abcd


output
abda


Note

String s is lexicographically larger (or simply larger)
than string t with the same length, if there is number i,
such that s1 = t1,
..., si = ti, si + 1 > ti + 1.

The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.

A palindrome is a string that reads the same forward or reversed.

题意:

给出一个由n个字符,由前p个小写英文字母组成的字符串,这个字符串长度大于等于2的子串都不回文。求字典序大于这个的 字典序最小的符合这个条件的字符串,若没有则输出NO思路:

把字符串当成p位数字,+1然后进位,判断是否符合规则。

规则是不回文,考虑回文长度为2,则相邻的数字不能相同;考虑回文长度为3,则隔一个的数字不能相同;考虑更高长度的情况,发现如果没有长度为2或者3的,就没有更高长度的情况了。所以我们只用判相邻的和隔一个的等不等

然后只这样的话final system test会跪,有数据会TLE。因为可能会加一加很多次都是回文的,我们要让这种情况一次直接加很多,不慢慢加一加一。我们发现加一进位只会改变低的若干位,回文判断只用判断这若干位,找到出现回文的最高位,如果这位不改变则再加一也会有回文,所以我们要让这位改变,最简单的就是把比它低的位全部置为最高的数字

比如a c b a 当你把第三位变成c的时候第四位怎么变都不行,做的是无用功,所以直接让第四位变最大的c,如果后面还有为数照样置为最大的,这样你下次进位的时候可以一起变换

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
using namespace std;
const int MAX = 1000+5;
char str[MAX];
int n,p,ok;
int solve()
{
    int index = n-1,i,j;
    while(1)
    {
        int flag = 0;
        j = n-1;
        str[j]++;//每次最低位+1
        while(j > 0 && str[j] >= 'a'+p)//进位
        {
            str[j] = 'a';
            str[j-1]++;
            j--;
        }

        if(j == 0 && str[j] >= 'a' + p) return 0;//如果最高位都超了的话直接爆了

        index = min(j,index);//从改变的位置开始

        for(i = index; i < n; i++)//检查是否出现回文子串
        {
            if(i >= 2 && str[i] == str[i-2])
            {
                flag = 1;
                break;
            }

            if(i >= 1 && str[i] == str[i-1])
            {
                flag = 1;
                break;
            }
        }

        if(flag)
        {
            index = i;

           //第i位不符合要求,我们需要让第i位改变,最简单的就是把之后的为都置为最高数字,让它下次加会进位
        for(j = i+1; j < n; j++)
            {
                str[j] = 'a'+p-1;
            }
        }
        else return 1;
    }
}

int main()
{
    cin>>n>>p;
    cin>>str;
    ok = solve();
    if(ok) cout<<str<<endl;
    else cout<<"NO"<<endl;
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: