Codeforces Round #197 (Div. 2) A~D

A. Helpful Maths
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.

The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.

You've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.

Input

The first line contains a non-empty string s — the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters "+". Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.

Output

Print the new sum that Xenia can count.

Sample test(s)
input
3+2+1
output
1+2+3
input
1+1+3+1+3
output
1+1+1+3+3
input
2
output
2

沙茶题。。。。


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int num[3];

int main()
{
    char str[111];
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        if(str[i]=='1')
            num[0]++;
        else if(str[i]=='2')
            num[1]++;
        else if(str[i]=='3')
            num[2]++;
    }
    //cout<<num[0]<<","<<num[1]<<","<<num[2]<<endl;
    int sum=num[0]+num[1]+num[2];
    bool first=true;
    for(int i=1;i<=sum;i++)
    {
        if(first==true) first=false;
        else printf("+");
        if(i<=num[0])
            printf("1");
        else if(i-num[0]<=num[1])
            printf("2");
        else if(i-num[1]-num[0]<=num[2])
            printf("3");
    }
    putchar(10);
    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Codeblocks )



B. Xenia and Ringroad
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.

Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.

Output

Print a single integer — the time Xenia needs to complete all tasks.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Sample test(s)
input
4 3
3 2 3
output
6
input
4 3
2 3 3
output
2
Note

In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.

沙茶题。。。


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int task[110000];

int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>task[i];
    }
    int pos=1;long long sum=0;
    for(int i=0;i<m;i++)
    {
        if(task[i]>=pos)
        {
            sum+=task[i]-pos;
            pos=task[i];
        }
        else if(task[i]<pos)
        {
            sum+=n+1-pos;
            sum+=task[i]-1;
            pos=task[i];
        }
    }
    cout<<sum<<endl;
    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Codeblocks )


C. Xenia and Weights
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.

Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.

You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done.

Input

The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).

Output

In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can putm weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.

If there are multiple solutions, you can print any of them.

Sample test(s)
input
0000000101
3
output
YES
8 10 8
input
1000000000
2
output
NO
 
用DFS做的,DP似乎也可以。。。。。
貌似有人发现了一组贪心的反例,不少人靠HACK赚翻了。。。。
 
 
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

bool flag;    int m;   char str[12];
vector<int> v;

void dfs(int cnt,int last,int left,int right,bool k)
{
    if(flag) return ;
    if(cnt==m)
    {
        puts("YES");
        for(int i=0;i<m;i++)
            cout<<v[i]<<" ";
        cout<<endl;
        flag=true;
        return ;
    }
    for(int i=0;i<10;i++)
    {
        if(str[i]=='1')
        {
            if(i+1==last) continue;
            int thw=i+1;
            if(k==true)///jia left
            {
                if(left+thw>right)
                {
                    v.push_back(thw);
                    dfs(cnt+1,thw,left+thw,right,!k);
                    v.pop_back();
                }
            }
            else if(k==false)///jia right
            {
                if(right+thw>left)
                {
                    v.push_back(thw);
                    dfs(cnt+1,thw,left,right+thw,!k);
                    v.pop_back();
                }
            }
        }
    }
}

int main()
{
    cin>>str;
    cin>>m;
    flag==false;
    dfs(0,-1,0,0,true);
    if(flag==false)
        puts("NO");
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

D. Xenia and Bit Operations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.

Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.

Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.

You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional mqueries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.

Input

The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

Output

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

Sample test(s)
input
2 4
1 6 3 5
1 4
3 4
1 2
1 2
output
1
3
3
3
Note

For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation

线段树单点更新水题。。。 
高仿notonlysuccess风格的代码。。。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=211111;
int xo[maxn<<2];
int n,m; bool ca;

void pushUP(int rt,bool wocha)
{
    if(wocha==true)
       xo[rt]=xo[rt<<1]^xo[rt<<1|1];
    else if(wocha==false)
       xo[rt]=xo[rt<<1]|xo[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        cin>>xo[rt];
        return ;
    }
    int m=(l+r)>>1;
    ca=!ca;
    build(lson);
    build(rson);
    ca=!ca;
    pushUP(rt,ca);
}

void update(int k,int p,int l,int r,int rt)
{
    if(l==r)
    {
        xo[rt]=p;
        return ;
    }
    int m=(r+l)>>1;
    ca=!ca;
    if(k<=m) update(k,p,lson);
    else update(k,p,rson);
    ca=!ca;
    pushUP(rt,ca);
}

int main()
{
    cin>>n>>m;
    int len=pow(2,n);
    if(n%2==0) ca=true;
    else if(n%2==1) ca=false;
    build(1,len,1);
    //cout<<xo[1]<<endl;
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        if(n%2==0) ca=true;
        else if(n%2==1) ca=false;
        update(a,b,1,len,1);
        cout<<xo[1]<<endl;
    }

    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

 

原文地址:https://www.cnblogs.com/CKboss/p/3284717.html