1. 程式人生 > >Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意門:http://codeforces.com/contest/1073/problem/C

C. Vasya and Robot

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

Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell 

(0,0)">(0,0)(0,0). Robot can perform the following four kinds of operations:

  • U — move from (x,y) to (x,y+1)
  • D — move from (x,y)to (x,y1)
  • L — move from (x,y)to (x1,y)
  • R — move from 
(x,y)">(x,y) to (x+1,y)

Vasya also has got a sequence of nn operations. Vasya wants to modify this sequence so after performing it the robot will end up in (x,y)(x,y).

Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: 

maxID−minID+1">maxIDminID+1maxID−minID+1, where maxIDmaxID is the maximum index of a changed operation, and minIDminID is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices 22, 55 and 77 are changed, so the length of changed subsegment is 72+1=67−2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 11.

If there are no changes, then the length of changed subsegment is 00. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.

Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from (0,0)(0,0) to (x,y)(x,y), or tell him that it's impossible.

Input

The first line contains one integer number n (1n2105)n (1≤n≤2⋅105) — the number of operations.

The second line contains the sequence of operations — a string of nn characters. Each character is either U, D, L or R.

The third line contains two integers x,y (109x,y109)x,y (−109≤x,y≤109) — the coordinates of the cell where the robot should end its path.

Output

Print one integer — the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from (0,0)(0,0) to (x,y)(x,y). If this change is impossible, print 1−1.

Examples input Copy
5
RURUU
-2 3
output Copy
3
input Copy
4
RULR
1 1
output Copy
0
input Copy
3
UUU
100 100
output Copy
-1
Note

In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is 31+1=33−1+1=3.

In the second example the given sequence already leads the robot to (x,y)(x,y), so the length of the changed subsegment is 00.

In the third example the robot can't end his path in the cell (x,y)(x,y).

 

題意概括:

輸入N個操作,詢問修操作是否能到達終點,如果能到達終點輸出“修改的區間”;

修改區間的定義:修改的最大座標操作的座標 - 修改的最小座標操作的座標

【規定起點為 (0,0),輸入終點( x, y );】

操作(上下左右):

解題思路:二分 || 尺取

預處理路徑字首和(壓縮路徑)sum_x [ ],sum_y[ ] ;則sum_x[ N ] , sum_y[ N ] 為實際的終點;

輸入的終點為 (ex, ey),假設能修改若干個操作到達輸入的終點,那麼:

某一段 [ st, ed ] 所走的影響為:

              X軸方向:xx = ex - ( sum_x[ N ] - sum_x[ ed -1 ] + sum_x[ st - 1 ] )

              Y軸方向:yy = ey - ( sum_y[ N ] - sum_y[ ed - 1 ] + sum_y[ st - 1 ] )

二分

二分修改區間長度 len ,尺取判斷在該長度是否滿足修改條件;

 

①操作所走最大範圍不得超過 len ,因為每次操作只是上下左右移動一步

②判斷能否完成假設的影響  len - abs(xx) - abs(yy))%2 ?= 0;

 abs(xx)表示的是 x 方向 到達終點 ex 的差值

 abs(yy)表示的是 y 方向 到達終點 ey 的差值

 假如 len > abs(xx)+abs(yy) 說明這段區間有操作是多餘的,但是隻要剩下的運算元是偶數就可以兩兩抵消。

 

尺取:

直接定義一個頭指標一個尾指標,暴力一遍,條件判斷是要頭指標加還是尾指標加,記錄最小修改區間。

 

AC code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e6+6;
 4 int x[N],y[N];
 5 int sx,sy,n;
 6 char s[N];
 7 bool check(int m)
 8 {
 9     for(int i=1;i<=n-m+1;i++)
10     {
11         int tx=x[n]-x[i+m-1]+x[i-1];  //當前原來選項造成的橫座標影響
12         int ty=y[n]-y[i+m-1]+y[i-1];  //當前原來選項造成的縱座標影響
13         int ex=sx-tx, ey=sy-ty;        //消除當前影響
14         printf("%d %d m: %d\n",ex,ey,m);
15         if(m>=(abs(ex)+abs(ey)) && (m-abs(ex)-abs(ey))%2==0) //可以構造
16             return 1;
17     }
18     return 0;
19 }
20 int main()
21 {
22     //string s;
23     while(~scanf("%d",&n))
24     {
25         scanf("%s",s+1);
26         x[0]=y[0]=0;
27         for(int i=1;i<=n;i++)
28         {
29             x[i]=x[i-1];y[i]=y[i-1];  //累積前面步數的結果
30 
31             if(s[i]=='L') x[i]-=1;    //當前步數造成的影響
32             else if(s[i]=='R') x[i]+=1;
33             else if(s[i]=='D') y[i]-=1;
34             else y[i]+=1;
35         }
36        // printf("%d %d\n",x[n],y[n]);
37         scanf("%d %d",&sx,&sy);       //終點
38        // printf("HH");
39         int l=0,r=n,ans=-1;
40         while(l<=r)                   //二分答案
41         {
42             printf("l:%d r:%d\n", l, r);
43             int mid=(l+r)>>1;
44             if(check(mid)) ans=mid,r=mid-1;
45             else l=mid+1;
46         }
47         printf("%d\n",ans);
48     }
49 }
View Code

 

相關推薦

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 二分 +

任意門:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second memory limit per test

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分+模擬)

題目連結 題意 一個二維平面上有個機器人初始在 ( 0 ,

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 二分+前後綴預處理

題意: 給定長度為n的字串,每個字元表示朝上下左右四個方向前進,給定一個目標位置,找一個最小的區間,使得改變這個區間的若干個字元,使得整個串的操作能到達目標位置只需要輸出最小區間長度 思路: 首先暴力的想法就是列舉所有的區間,這樣這個區間兩邊就會有一些操作不會改變,然後我們根據這些操

Educational Codeforces Round 53 (Rated for Div. 2)-C. Vasya and Robot二分

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 題意 在 二

Educational Codeforces Round 53 (Rated for Div. 2)-C. Vasya and Robot

思路:一開始做思路就錯了,我考慮的是如何轉變方向使其有效到達目標位置,但是比較複雜。這題要求更改的區間最小長度。因此可以二分割槽間長度,在遍歷左端點看是否存在即可。判斷存在,即更改的區間的個數是否能將其他區域的缺少或多餘步驟給消掉即可。 Code: #includ

Educational Codeforces Round 51 (Rated for Div. 2)E. Vasya and Big Integers(二分雜湊+差分)

題目傳送門 題意   給出長度小於等於10610^6106的數字串a,l,r,求把串a拆分後,每段數字大小都是≥l\geq l≥l並且≤r\leq r≤r的方案有多少種。 分析   首先我們可以發現一個很顯然的結論,即如果從第i位開始截成一段,那麼這一

Educational Codeforces Round 54 (Rated for Div. 2) E. Vasya and a Tree(dfs+思維)

E. Vasya and a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 貪心

傳送門:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds memory limit per test

Educational Codeforces Round 55 (Rated for Div. 2) A - Vasya and Book

傳送門 https://www.cnblogs.com/violet-acmer/p/10035971.html   題意:   一本書有n頁,每次只能翻 d 頁,問從x頁到y頁需要翻動幾次?   注意:往前翻最少翻到第1頁,往後翻最多翻到n頁。 題解:   一開始想找規律來著,emm

Educational Codeforces Round 52 (Rated for Div. 2)B. Vasya and Isolated Vertices·「模擬,思維」

B. Vasya and Isolated Vertices time limit per test 1 second memory limit per test 256 megabytes input standard input output standard o

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition vector 預處理優化

long long i++ ins pat nds won not total mathjax 傳送門:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limi

Educational Codeforces Round 51 (Rated for Div. 2).A. Vasya And Password(模擬)

A. Vasya And Password time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Educational Codeforces Round 52 (Rated for Div. 2) B. Vasya and Isolated Vertices

題解 題目大意 給你n個點使用m條邊組成一個圖 問你最多和最小能有多少個孤立節點(度為0) 最少的點個數就是每個邊連兩個點m-n2和0取max 最多的點計算d(d-1)=m*2用n-d 需要特判0 AC程式碼 #include <stdio.h> #

Educational Codeforces Round 56 (Rated for Div. 2) C - Mishka and the Last Exam(貪心/差分約束)

題意 給一個n,一個序列b[], bi=ai+a(n+i-1), 求不降序的a序列[] 思路來源 組裡各神犇&&自己 題解 Solution1: 顯然a1=0,an=b1的時候,區間長度最長 區間裡面內建區間的時候如果內區間能左對齊,顯

Educational Codeforces Round 54 (Rated for Div. 2) E. Vasya and a Tree dfs+樹狀陣列

HEU 大三蒟蒻一枚 有任何問題或建議都可以加Q聯絡我^_^ QQ : 986195894 CodeForces id : lajiyuan CodeForces id : biubiubiu_ Vjudge id : heu2016201206

Educational Codeforces Round 53 (Rated for Div. 2)

space iostream ace ted color 代碼 ces pan else A. Diverse Substring(前綴和) 題意:給一個字符串,找出一個子串滿足該子串中每個字母出現的次數不超過子串的長度/2,字符串的長度n<1000. 題解:n方枚舉

Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair

題意:一個人  有T塊錢 有一圈商店 分別出售 不同價格的東西  每次經過商店只能買一個  並且如果錢夠就必須買  這個人一定是從1號店開始的!(比賽的時候讀錯了題,以為隨意起點。。。)問可以買多少個 思路:這個人有T塊錢  走一圈之後可以買num個 花了

Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair(貪心)

題目連結 題意 有 n n n個物品圍成一圈,每個物品都有一個價值