【赛后补题】(HDU6223) Infinite Fraction Path {2017-ACM/ICPC Shenyang Onsite}

来源:互联网 发布:阿里云盾 事故 编辑:程序博客网 时间:2024/05/18 06:49

场上第二条卡我队的题目。

题意与分析

按照题意能够生成一个有环的n个点图(每个点有个位数的权值)。图上路过n个点显然能够生成一个n位数的序列。求一个最大序列。
这条题目显然是搜索,但是我队在场上(我负责)最傻的穷举然后wa了(不是tle。。。),于是愣是没想到什么bfs/dfs,调到最后。
回来做,一次tle一次wa。tle显然是因为小霸王评测机变成了土豆评测机,然后采用bfs+剪枝的策略做题。(最优解)这里有两个显然的剪枝:

  • 如果当前节点的值不如节点所在深度的目前最优解,剪枝
  • 如果当前节点的当前值已经被搜索过(需要一个数组存储),剪枝

具体实现的细节见代码。

代码

#include <bits/stdc++.h>#define PB push_back#define MP make_pairusing namespace std;#define NQUICKIO#define NFILEtypedef long long ll;typedef unsigned long long ull;int mod[150005];int ans[150005];string str; int len,n,kase=0;struct Node{    int dep,cur;    Node(int _d,int _c):dep(_d),cur(_c) {}    bool operator<(const Node& rhs) const    {        if(dep!=rhs.dep) return dep>rhs.dep;        else if(str[cur]!=str[rhs.cur]) return str[cur]<str[rhs.cur]; // > before.        else return cur>rhs.cur;    }};void solve(int maxid){    priority_queue<Node> pq;    for(int i=0;i!=n;++i)    {        if(str[i]==str[maxid])        {            pq.push(Node(0,i)); //printf("push %d.\n",i);        }    }    string ans; ans.resize(n,char('0'-1));    int ac[150005]; memset(ac,-1,sizeof(ac));    while(!pq.empty())    {        auto now=pq.top(); pq.pop();       // printf("now dep=%d,now cur=%d.\n",now.dep,now.cur);        if(ans[now.dep]>str[now.cur]) continue;        if(ac[now.dep]!=-1 && ac[now.dep]>=now.cur) continue;        if(now.dep>=n) continue;        ans[now.dep]=str[now.cur];        ac[now.dep]=now.cur;        pq.push(Node(now.dep+1,mod[now.cur]));    }    cout<<"Case #"<<++kase<<": "<<ans<<endl;}int main(){#ifdef QUICKIO    ios::sync_with_stdio(false);    cin.tie(0); cout.tie(0);#endif#ifdef FILE    freopen("datain.txt","r",stdin);    freopen("dataout.txt","w",stdout);#endif    int t; cin>>t;    while(t--)    {        cin>>n>>str;        for(int i=0;i<=n;++i) mod[i]=((ll)i*(ll)i+1)%n;        len=str.length();        int maxx=0;        for(int i=0;i!=len;++i)            if(str[i]>str[maxx]) maxx=i;        solve(maxx);    }    return 0;}
原创粉丝点击