hdu3706單調隊列
阿新 • • 發佈:2017-07-11
fine cassert lap style 差距 pragma lib () open
很基礎的單調隊列
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define能ac的錯解pi acos(-1) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-9; const int N=100000+10,maxn=500+100,inf=0x3f3f3f; ll q[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n,a,b; while(cin>>n>>a>>b){ ll ans=1,s=0,t=0,res=1; for(int i=1;i<=n;i++) { res=(res*a)%b; while(s<t&&q[t-1]>res)t--;//單調遞減隊列 q[t++]=res; ans=(ans*q[s])%b; } cout<<ans<<endl; }return 0; }
突然發現這代碼是有問題的,沒有考慮到s和t的間距問題,可能是數據太弱了,居然過了
所以重新改了一下,加一個id記錄標號,如果s的標號小於i-a就說明差距大於a了,需要把s++
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-9; const int N=100000+10,maxn=500+100,inf=0x3f3f3f; ll q[N],id[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n,a,b; while(cin>>n>>a>>b){ ll ans=1,s=0,t=0,res=1; for(int i=1;i<=n;i++) { res=(res*a)%b; while(s<t&&q[t-1]>res)t--;//單調遞減隊列 q[t++]=res; id[t-1]=i; if(id[s]<i-a)s++; ans=(ans*q[s])%b; } cout<<ans<<endl; } return 0; }正解
hdu3706單調隊列