标签:getc back ring 队列 input const step algo 否则
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <ctime> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") #define freopen freopen("in.txt","r",stdin) const int maxn=1e6+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int n,m,k,t,d[maxn],f[maxn],head,tail; int main() { int i,j; scanf("%d",&t); while(t--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); memset(d,inf,sizeof(d)); d[a]=0; head=tail=a; f[head]=a; for(i=a-1;i>=1;i--) { if((ll)i*b<=a)d[i]=d[i*b]+1; while(head<=tail&&f[tail]-i>c)tail--; if(head<=tail)d[i]=min(d[i],d[f[tail]]+1); f[--head]=i; while(head<tail&&d[f[head+1]]>d[f[head]])f[head+1]=f[head],head++; } printf("%d\n",d[1]); } //system("Pause"); return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <ctime> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") #define freopen freopen("in.txt","r",stdin) const int maxn=1e6+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int n,m,k,t,dp[maxn]; struct node { int c,id; node(int _c,int _id):c(_c),id(_id){} bool operator<(const node&p)const { return c>p.c; } }; priority_queue<node>pq; int main() { int i,j; scanf("%d",&t); while(t--) { int a,b,c; while(!pq.empty())pq.pop(); scanf("%d%d%d",&a,&b,&c); memset(dp,inf,sizeof(dp)); pq.push(node(0,a)); dp[a]=0; for(i=a-1;i>=1;i--) { while(!pq.empty()) { node x=pq.top(); if(x.id-i>c)pq.pop(); else { dp[i]=x.c+1; break; } } if((ll)b*i<=a)dp[i]=min(dp[i],dp[i*b]+1); pq.push(node(dp[i],i)); } printf("%d\n",dp[1]); } //system("Pause"); return 0; }
标签:getc back ring 队列 input const step algo 否则
原文地址:http://www.cnblogs.com/dyzll/p/6012152.html