ZOJ 3872 Beauty of Array&&zoj 3870 Team formation

Source: Internet
Author: User

3872 Links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872

Title: give you the number of N, ask all the successive subsequence of all the elements of the and (the subsequence has the same element only once) (n<100000).

The even if sequence is 1 2 3, then the composition 1,2,3,1 2,2 3,1 2 3, and 20;

If the sequence is 1 2 2, then the composition 1,2,2,1 2,2 2,1 2 2, and 13;

Problem-solving ideas: not repeating the sequence and very simple, the key is to go heavy.

Now look at a sequence: 3 4 7 6 4 ; in the 4,

We record each element on the last occurrence of position 2, the next time to 5 o'clock again encountered this element, then in the interval [3,5] This interval of the sub-sequence and the need for the current Blue 4 of this element, in the range containing [1, 2] the sub-sequence of all numbers within this interval does not have to calculate the second 4 this element, but Count back to 4, but all the subsequence of the preceding 4 elements is still to be computed, except that the last 4 is not included.

Dp[I] represents the sum of all the sub-sequences of the first I element (including i), then dp[i]=dp[i-1]+ (n-mp[s[i]]) *s[i], where mp[s[i]] means s[i] the last occurrence of the position;

Of course dp[I] can be expressed in DP, because the space optimization like 0/1 backpack is similar.

The code is as follows:

#include <cstdio> #include <cstring>using namespace std; #define LL long long#define M 1000005int mp[m];int Main () {    int t,n,a;    scanf ("%d", &t);    while (t--)    {        LL sum=0;        Memset (Mp,0,sizeof (MP));        scanf ("%d", &n);        LL dp=0;        for (int i=1;i<=n;i++)        {            scanf ("%d", &a);            dp=dp+ (I-mp[a]) *a;            mp[a]=i;            SUM+=DP;        }        printf ("%lld\n", sum);}    }

3870 Links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870

The main question: give you the number of N, ask to meet the two number of different or after the value of the combination of the two is a large number of how many? (n<100000)

Problem-solving ideas: For some of these: 1 (1)

1 0 (2)

1 1 (3)

1 0 0 (4)

1 0 1 (5)

1 1 0 (6)

For the above number we found that the second-highest bit of the binary must be 1,1^1=0, then the same number of bits must not be combined, but also found that only when a one appears 0 o'clock and above the same one appears 1 of the number of combinations can increase, why is it possible? For example, the combination of 3 and 6, the lowest bit is 0^1=1 is increased, but the second bit for 1^1=0 reduction, so does not conform; how does it fit? That is, with the current bit combination, the small number of the front all bits (high) is 0, then the solution has, first of all the number of orders, for each number of binary decomposition, traverse each bit, when the I bit is 1 o'clock, s[len [i]++, where Len represents the current binary length of the number, s[Len] [i] The number of numbers in the number of Len that is 1 in length, and the number of the digits of the I-bit 1 in the number of all the numbers in which the binary length is 0 o'clock, when the first bit of I is the first, and the previous traversal has been preprocessed, and the line is directly added.

The code is as follows:

#include <cstdio> #include <algorithm> #include <cstring> #include <cmath>using namespace std; int s[35][35];int D[100005];int Bin (int x)///number of binary decomposition bits {    int cnt;    for (int i=1;i<32;i++)    if ((1<<i) -1>=x)        {Cnt=i;return cnt;}} int main () {    int t,n;    scanf ("%d", &t);    while (t--)    {        scanf ("%d", &n);        for (int i=0;i<n;i++)            scanf ("%d", &d[i]);        Sort (d,d+n);        Memset (s,0,sizeof (s));        int ans=0;        for (int i=0;i<n;i++)        {            int cnt=bin (d[i]);            int k=1;            while (D[i])///binary decomposition            {                int r=d[i]%2;                if (r==1) s[cnt][k]++;                else ans+=s[k][k];                k++;                d[i]/=2;            }        }        printf ("%d\n", ans);    }    return 0;}

ZOJ 3872 Beauty of Array&&zoj 3870 Team formation

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.