"Uvalive 7505" Hungry Game of Ants (DP)

Source: Internet
Author: User
Tags fread pow

"Uvalive 7505" Hungry Game of Ants (DP)

Main topic:
A chain of n ants, the weight of the first ant is I. Each ant chooses an initial direction, left or right. When two ants meet, the heavy ants eat the small-weight ants and increase the weight of the ants on the small weight. If two ants have the same weight, the left side will eat the right one. The leftmost and most right is the boundary, and the ant will turn around when it touches the border.

Now define the initial direction for all the ants and ask how many of the programs will allow the K ant to eventually survive.

First clear: K ants want to survive, the initial direction must not be to the right (unless k = = N)
, second, it will eat all the ants on the left and then turn around.

So first consider K eat the left ant and turn around the plan number, in fact, is to find a largest j, meet
∑J<C≤KANT[C] \sum\limits_{j \lt c \le k} Ant[c] >∑1≤c≤jant[c] \sum\limits_{1 \le c \le J} Ant[c]

K the j+1 to K of the ants are eaten, can eat 1 to J after the merger of the remaining Ant.

Because for each ant, walking right must be eaten. So 1~j ants are free to choose the direction and ensure that the worst case can be solved. J+1 to k-1 ants are right (eaten by k), because if there is any choice left, it will merge with 1~j, and eventually become an ant to eat K. So the solution is 2j 2^j

And then considering turning around, for the first ant, if left, there is a place where you can go farthest (nearest to K), meet
∑1≤C<JANT[C]>=∑J≤C≤IANT[C] \sum\limits_{1 \le c \lt j}ant[c] >= \sum\limits_{j \le c \le I}ant[c], i.e. J to i merge, K eats the left side again Eat to j-1.
For the larger J, this inequality is satisfied, and for the smaller J, this condition is not satisfied.

In this way, in fact, J to k-1 ants arbitrarily choose the direction, k+1 to j-1 to be eaten into a k. i.e. Dp[i]=∑j≤c<idp[c] dp[i] = \sum\limits_{j \le c \lt I}dp[c],dp[j] means that the ants that k+1 to J are eaten by K, and the K-direction must be toward the right.

This allows you to traverse through, then maintain a pointer to the smallest J, move right to the appropriate position before each transfer, and maintain the value of the interval DP value of J to I on a global sum of dp[i].

It is important to note that the final answer is dp[n]*2, because the nth ant goes right and left.

The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack&
Gt  #include <list> #include <algorithm> #include <map> #include <set> #define LL Long Long #define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace s
td
const int INF = 0X3F3F3F3F;
const int mod = 1E9+7;
Const double EPS = 1e-8;

const int MAXN = 1123456;

LL DP[MAXN];

    ll Pow (ll A,ll b) {ll ans = 1;
        while (b) {if (b&1) ans = ans*a%mod;
        b >>= 1;
    A = A*a%mod;
} return ans;

} ll Sum (ll x) {return x* (x+1)/2;}
    ll Search (ll l,ll r,ll k) {if (R < L) return 1;
    LL sum = SUM (r);

    LL ans =-1;

        while (L <= R) {LL mid = (l+r) >>1; if (Sum-sum (mid) +k > sum (miD) {ans = mid;
        L = mid+1;
    } else R = mid-1;

    } if (ans = =-1) return 0;
Return Pow (2,ans);

    } ll solve (int n,int k) {ll ans = 0;
    Dp[k] = Search (1,k-1,k);
    int L = k;

    ans = dp[k]; for (int i = k+1; I <= N, ++i) {while (L < i && sum (l) < sum (i)-sum (l)) ans = ((ans-dp[l++])%m

        Od+mod)%mod;
        Dp[i] = ans;

    Ans = (ans+dp[i])%mod;
} return dp[n]*2%mod;
    } int main () {//fread ("");

    Fwrite ("");

    int t,n,k;

    scanf ("%d", &t);
        for (int z = 1; z <= t; ++z) {scanf ("%d%d", &n,&k);
    printf ("Case #%d:%lld\n", Z,solve (n,k));
} return 0; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.