• 【codeforces 797E】Array Queries


    【题目链接】:http://codeforces.com/problemset/problem/797/E

    【题意】

    给你一个n个元素的数组;
    每个元素都在1..n之间;
    然后给你q个询问;
    每个询问由p和k构成;
    会对p进行
    p=p+a[p]+k操作若干次;
    你要输出p第一次大于n之后操作了多少次;

    【题解】

    部分DP;
    这里对于k>400的询问;
    我们直接暴力求解;
    因为这时暴力所花费的时间已经可以接受了;
    而对于剩余的k<=400的询问;
    我们写一个记忆化搜索来求解;
    很棒的思路吧

    【Number Of WA

    0

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 1e5+100;
    
    int n,a[N],q;
    int f[N][400+20];
    
    int dfs(int p,int k)
    {
        if (p>n)
            return 0;
        if (k>400)
            return 1+dfs(p+a[p]+k,k);
        else
            return f[p][k]=(f[p][k]?f[p][k]:1+dfs(p+a[p]+k,k));
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        ios::sync_with_stdio(false);
        cin >> n;
        rep1(i,1,n)
            cin >> a[i];
        cin >> q;
        rep1(i,1,q)
        {
            int p,k;
            cin >> p >> k;
            cout << dfs(p,k) << endl;
        }
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    Oracle FGA审计记录的清理步骤
    UVa11488-Hyper Prefix Sets(trie树)
    配置Log4j(非常具体)
    poj1190生日蛋糕
    zju1610Count the Colors
    【例9.3】求最长不下降序列
    P1364 医院设置
    P1629 邮递员送信
    P1476 休息中的小呆
    P1330 封锁阳光大学
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626407.html
Copyright © 2020-2023  润新知