Codeforces Round#197 前三題

果7發表於2013-08-27
很簡單的題目,直接上程式碼吧。
             題目地址:A. Helpful Maths
AC程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int a[102],len;
char s[500];

int main()
{
    int i,p;
    while(~scanf("%s",s))
    {
        p=0;
        len=strlen(s);
        for(i=0;i<len;i+=2)
            a[p++]=s[i]-'0';
        sort(a,a+p);
        cout<<a[0];
        for(i=1;i<p;i++)
            cout<<"+"<<a[i];
        cout<<endl;
    }
    return 0;
}


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.



很簡單的題目,直接上程式碼吧。
 題目地址:B. Xenia and Ringroad
AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    __int64 sum,i;
    int a,b,x;
    int pre;
    while(~scanf("%d%d",&a,&b))
    {
        sum=0;
        pre=1;
        for(i=1;i<=b;i++)
        {
           scanf("%d",&x);
           if(x>=pre)
               sum+=x-pre;
           else
              sum+=a-(pre-x);
           pre=x;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


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


           題目大意:1~10類別的砝碼。0表示表示沒有,1表示有。然後給你一個數m。每次放砝碼的時候放的這個不能跟前一個是一個類別。並且需要放的哪邊哪邊就重一點。依次是先放左邊再放右邊。。如此迴圈。如果能放,輸出一組m即可。不能輸出No。

           解題思路:當時我就覺得每次放的時候如果能選小一點的砝碼,就選小一點的。當時先給的幾組小測試資料過了,看了下第四題,覺得有點不是很懂。覺得第五題蠻有意思的。後來太困了,就直接關了電腦準備睡了。隨手看了下手機,發現群裡面boblee說了組資料1110000000 4 正確答案是2 3 2 3。當時12:0x了。我想了下自己的資料,開始選1 然後選2 再選3然後就會輸出NO,想算了吧,最後還是忍不住自己在第一次的時候列舉了一下。最後發現A了。詳見程式碼。貪心思想。

         題目地址:C. Xenia and Weights

AC程式碼:
//程式碼寫的有點亂

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;

int visi[12];
int wei[12];  //wei裡面記錄的是擁有的砝碼類別
char s[12];
int a[1002];  //存放放的砝碼

int main()
{
    int i,m,sum1,sum2,p,step,j;
    while(~scanf("%s",s))
    {
        p=0;
        memset(visi,0,sizeof(visi));
        for(i=0; i<10; i++)
            if(s[i]=='1')
            {
                visi[i+1]=1;
                //cnt++;
            }
        for(i=1; i<=10; i++)
            if(visi[i]==1)
            {
                wei[p++]=i;
                //cout<<wei[p-1]<<endl;
            }
        scanf("%d",&m);

        if(p==0)  //沒有砝碼直接輸出NO
        {
            printf("NO\n");
            continue;
        }

        int flag1=0;
        int flag,pre;
        for(j=0; j<p; j++)  //列舉放的第一個砝碼
        {
            flag=0;
            pre=wei[j];   //記錄上一個放的砝碼
            a[0]=wei[j];  //第一個
            sum1=pre;     //左邊的總數
            step=1;       //步數判定放左邊還是右邊
            sum2=0;       //右邊的總數
            while(step<m)
            {
                if(step&1)  //該放右邊了
                {
                    for(i=0; i<p; i++)
                    {
                        if(wei[i]!=pre)
                        {
                            if(sum2+wei[i]>sum1)
                            {
                                a[step]=wei[i];
                                pre=wei[i];
                                sum2=sum2+wei[i];
                                break;
                            }
                        }
                    }
                    if(sum2<=sum1)
                    {
                        flag=1;
                        break;
                    }
                }
                else  //該放左邊了
                {
                    for(i=0; i<p; i++)
                    {
                        if(wei[i]!=pre)
                        {
                            if(sum1+wei[i]>sum2)
                            {
                                a[step]=wei[i];
                                pre=wei[i];
                                sum1=sum1+wei[i];
                                break;
                            }
                        }
                    }
                    if(sum1<=sum2)
                    {
                        flag=1;
                        break;
                    }
                }
                step++;
            }
            if(flag)
                continue;
            flag1=1;
            break;
        }

        if(flag1==0) puts("NO");
        else
        {
            puts("YES");
            printf("%d",a[0]);
            for(i=1; i<m; i++)
                printf(" %d",a[i]);
            cout<<endl;
        }
    }
    return 0;
}


感覺這應該是最簡單的依次codeforces了,聽說D題是很水的線段樹,我覺得E題還可以看下。


推薦一首很好聽的歌:
夜空中最亮的星來自逃跑計劃

相關文章