CF611F. New Year and Cleaning
生活随笔
收集整理的這篇文章主要介紹了
CF611F. New Year and Cleaning
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
CF611F. New Year and Cleaning
Solution
還挺巧妙的套路。
把起點整體看作一個矩陣,在操作時移出原來矩陣外的部分的起點都是超越邊界的,可以直接通過超出的面積計算貢獻,再把超出的部分刪去,模擬即可(第一輪模擬會與之后有所不同)。
Code
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <cassert> #include <string.h> //#include <unordered_set> //#include <unordered_map> //#include <bits/stdc++.h>#define MP(A,B) make_pair(A,B) #define PB(A) push_back(A) #define SIZE(A) ((int)A.size()) #define LEN(A) ((int)A.length()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define fi first #define se second #define int llusing namespace std;template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; } template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }typedef long long ll; typedef unsigned long long ull; typedef long double lod; typedef pair<int,int> PR; typedef vector<int> VI;const lod eps=1e-11; const lod pi=acos(-1); const int oo=1<<30; const ll loo=1ll<<62; const int mods=1e9+7; const int MAXN=1000005; const int INF=0x3f3f3f3f;//1061109567 /*--------------------------------------------------------------------*/ inline int read() {int f=1,x=0; char c=getchar();while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }return x*f; } char st[MAXN]; PR c[MAXN]; int upd(int x,int y) { return x+y>=mods?x+y-mods:x+y; } void get(int &x,int &y,char ch) {if (ch=='R') y++;if (ch=='L') y--;if (ch=='D') x--;if (ch=='U') x++; } signed main() {int n=read(),H=read(),W=read(),x2=H,y2=W,x1=1,y1=1,ans=0;scanf("%s",st+1);for (int i=1;i<=n;i++){get(x1,y1,st[i]);get(x2,y2,st[i]);if (x1==0&&x1<=x2&&y1<=y2) x1=1,ans=upd(ans,(y2-y1+1)*i%mods);if (y1==0&&x1<=x2&&y1<=y2) y1=1,ans=upd(ans,(x2-x1+1)*i%mods);if (x2==H+1&&x1<=x2&&y1<=y2) x2=H,ans=upd(ans,(y2-y1+1)*i%mods);if (y2==W+1&&x1<=x2&&y1<=y2) y2=W,ans=upd(ans,(x2-x1+1)*i%mods);}int num=0;for (int i=1;i<=n;i++){get(x1,y1,st[i]);get(x2,y2,st[i]);if (x1==0&&x1<=x2&&y1<=y2) x1=1,ans=upd(ans,(y2-y1+1)*(i+n)%mods),c[++num]=MP(i,0);if (y1==0&&x1<=x2&&y1<=y2) y1=1,ans=upd(ans,(x2-x1+1)*(i+n)%mods),c[++num]=MP(i,1);if (x2==H+1&&x1<=x2&&y1<=y2) x2=H,ans=upd(ans,(y2-y1+1)*(i+n)%mods),c[++num]=MP(i,0);if (y2==W+1&&x1<=x2&&y1<=y2) y2=W,ans=upd(ans,(x2-x1+1)*(i+n)%mods),c[++num]=MP(i,1);}if (!num&&x1<=x2&&y1<=y2) { puts("-1"); return 0; }for (int nw=2,x=x2-x1+1,y=y2-y1+1;x>0&&y>0;nw++)for (int i=1;i<=num;i++) {if (c[i].se==0&&x>0&&y>0) ans=upd(ans,max(y,0ll)*(nw*n%mods+c[i].fi)%mods),x--;if (c[i].se==1&&x>0&&y>0) ans=upd(ans,max(x,0ll)*(nw*n%mods+c[i].fi)%mods),y--;}printf("%lld\n",ans);return 0; }總結
以上是生活随笔為你收集整理的CF611F. New Year and Cleaning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 半边脸疼吞咽口水都疼怎么回事?
- 下一篇: 国家一级学会创办的期刊