Codeforces Round#197 前三题

很简单的题目,直接上代码吧。
              题目地址: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题还可以看下。


推荐一首很好听的歌:
夜空中最亮的星 来自逃跑计划
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值