Codeforces Round #180 (Div. 2)——A,B,C,D

A. Snow Footprints
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.

At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in thet-th block. It is known that Alice never moves outside of the road.

You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.

Input

The first line of the input contains integer n (3 ≤ n ≤ 1000).

The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).

It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.

Output

Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.

不得不说,代码写的很水,不贴了。

判断是否L,R均出现:

1.如果出现,从第一个L向左走再优走再左走回到最后一个R即可。

2.如果只有R,从第一个R走到最后一个R的后一格即可。

3.如果只有L,从最后一个L的后一格走到最后一个L即可。

B. Sail
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).

  • If the wind blows to the east, the boat will move to (x + 1, y).
  • If the wind blows to the south, the boat will move to (x, y - 1).
  • If the wind blows to the west, the boat will move to (x - 1, y).
  • If the wind blows to the north, the boat will move to (x, y + 1).

Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?

Input

The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105,  - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different.

The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).

Output

If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).

水题。挂了很久没搞懂为什么。后来换了个写法,给过了。

思路:贪心即可。即每次只向终点靠近。在时间内 t 判断即可。

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string s;
int main()
{
    long long sx,sy,ex,ey,t;
    cin>>t>>sx>>sy>>ex>>ey;
    cin>>s;
    for(int i=0;i<t;i++)
    {
        if(s[i]=='E'&&sx<ex) sx++;
        if(s[i]=='W'&&sx>ex) sx--;
        if(s[i]=='S'&&sy>ey) sy--;
        if(s[i]=='N'&&sy<ey) sy++;
        if(sx==ex&&sy==ey)
        {
            cout<<i+1<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}

C. Parity Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations:

  • Write parity(a) to the end of a. For example, .
  • Remove the first character of a. For example, . You cannot perform this operation if a is empty.

You can use as many operations as you want. The problem is, is it possible to turn a into b?

The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.

Input

The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.

Output

Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.

想到1是解题的关键,就简单了。a串1的个数不能小于b串中1的个数。

当a中有奇数个1时,利用奇偶校验的特性,可增加一个1再减少.偶数则只能减少。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;
    int cnt1=0,cnt2=0;
    for(int i=0;a[i];i++)
        if(a[i]=='1') cnt1++;
    for(int i=0;b[i];i++)
        if(b[i]=='1') cnt2++;
    if(cnt1%2){
        if(cnt1!=cnt2-1&&cnt1<cnt2) {
            cout<<"NO";
            return 0;
        }
    }
    else{
        if(cnt1<cnt2){
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES";
    return 0;
}

D. Fish Weight
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 ≤ w2 ≤ ... ≤ wk holds.

Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she's caught. Given the type of the fish they've caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob's. In other words, does there exist a sequence of weights wi (not necessary integers), such that the fish caught by Alice has a strictly larger total weight?

Input

The first line contains three integers n, m, k (1 ≤ n, m ≤ 105, 1 ≤ k ≤ 109) — the number of fish caught by Alice and Bob respectively, and the number of fish species.

The second line contains n integers each from 1 to k, the list of fish type caught by Alice. The third line contains m integers each from 1 to k, the list of fish type caught by Bob.

Note that one may have caught more than one fish for a same species.

Output

Output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.

扫描一遍,看是否对于A的每一条鱼在B中都能找到大于等于其重量的鱼即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=100000+5;
int A[maxn],B[maxn];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        scanf("%d",&A[i]);
    for(int i=0;i<m;i++)
        scanf("%d",&B[i]);
    sort(A,A+n);
    sort(B,B+m);
    int j=0,i=0;
    while(i<n&&j<m)
    {
        if(A[i]<=B[j]) i++,j++;
        else j++;
    }
    if(i==n) cout<<"NO";
    else cout<<"YES";
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值