博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fxx and game
阅读量:5068 次
发布时间:2019-06-12

本文共 3877 字,大约阅读时间需要 12 分钟。

Fxx and game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Problem Description
Young theoretical computer scientist Fxx designed a game for his students.
In each game, you will get three integers
X,k,t.In each step, you can only do one of the following moves:
1.X=Xi(0<=i<=t).
2.if k|X,X=X/k.
Now Fxx wants you to tell him the minimum steps to make X become 1.
 
Input
In the first line, there is an integer
T(1T20) indicating the number of test cases.
As for the following T lines, each line contains three integers X,k,t(0t106,1X,k106)
For each text case,we assure that it's possible to make X become 1。
 
Output
For each test case, output the answer.
 
Sample Input
2 9 2 1 11 3 3
 
Sample Output
4 3
分析:dp+单调队列(需手写,否则难以卡过去);
代码:
手写:
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set
::iterator it=s.begin();it!=s.end();it++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector
#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
#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
d[f[head]])f[head+1]=f[head],head++; } printf("%d\n",d[1]); } //system("Pause"); return 0;}
stl:
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set
::iterator it=s.begin();it!=s.end();it++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector
#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
#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
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;}

转载于:https://www.cnblogs.com/dyzll/p/6012152.html

你可能感兴趣的文章
HttpReceiveRequestEntityBody 使用应注意的地方
查看>>
CentOS Linux iptables 防火墙
查看>>
Android AsyncTask 的实现及 cancel 方式
查看>>
李超线段树学习笔记
查看>>
java swing 按钮事件触发两次或者多次
查看>>
论演员的自我修养2
查看>>
常用算法大全-贪婪算法
查看>>
Apache Commons CLI 开发命令行工具示例
查看>>
Laravel的生命周期
查看>>
自己编写php框架(一)
查看>>
优化MySchool数据库设计
查看>>
Flink - Checkpoint
查看>>
Apache Kafka源码分析 – Controller
查看>>
查看eclipse ADT SDK JDK版本号
查看>>
保龄球计分
查看>>
在MySql中实现MemberShip的权限管理
查看>>
vim: vs sp 调整窗口高度和宽度
查看>>
预防数据库攻击
查看>>
建立组织级过程性能基线的注意事项
查看>>
coding++:java操作 FastDFS(上传 | 下载 | 删除)
查看>>