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.
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.
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即可。
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)?
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).
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;
}
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.
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.
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
当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;
}
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?
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 "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.
#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;
}