zoukankan      html  css  js  c++  java
  • Fruit Ninja(树状数组+思维)

    Fruit Ninja

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2164    Accepted Submission(s): 838


    Problem Description
    Recently, dobby is addicted in the Fruit Ninja. As you know, dobby is a free elf, so unlike other elves, he could do whatever he wants.But the hands of the elves are somehow strange, so when he cuts the fruit, he can only make specific move of his hands. Moreover, he can only start his hand in point A, and then move to point B,then move to point C,and he must make sure that point A is the lowest, point B is the highest, and point C is in the middle. Another elf, Kreacher, is not interested in cutting fruits, but he is very interested in numbers.Now, he wonders, give you a permutation of 1 to N, how many triples that makes such a relationship can you find ? That is , how many (x,y,z) can you find such that x < z < y ?
     
    Input
    The first line contains a positive integer T(T <= 10), indicates the number of test cases.For each test case, the first line of input is a positive integer N(N <= 100,000), and the second line is a permutation of 1 to N.
     
    Output
    For each test case, ouput the number of triples as the sample below, you just need to output the result mod 100000007.
     
    Sample Input
    2 6 1 3 2 6 5 4 5 3 5 2 4 1
     
    Sample Output
    Case #1: 10 Case #2: 1
    题解:
    给你一个1到n的排列,让求满足posx < posy < posz  && x < z < y 的组数有多少。
    宇神写的非常好,思路是对于每个数找出后面比a[i]大的数n2;则可以组成的i < j < k && (a[i] < a[j] < a[k] || a[i] < a[k] < a[j])的组合数为C(2,n2);再减去i < j < k && a[i] < a[j] < a[k]就好了,其实就是前面比a[i]小的数乘以后面比a[i]大的数;另外,要用LL
    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 typedef long long LL;
     9 const int MAXN=1e5+100;
    10 const int MOD=100000007;
    11 LL tree[MAXN+1];
    12 int lowbit(int x){return x&(-x);}
    13 void update(int x,int y){
    14     while(x<=MAXN){
    15         tree[x]++;
    16         x+=lowbit(x);
    17     }
    18 }
    19 LL sum(int x){
    20     LL sum=0;
    21     while(x>0){
    22         sum+=tree[x];
    23         x-=lowbit(x);
    24     }
    25     return sum;
    26 }
    27 int main(){
    28     int T,N,temp,flot=0;
    29     scanf("%d",&T);
    30     while(T--){
    31         scanf("%d",&N);
    32         mem(tree,0);
    33         LL n1,n2;
    34         LL ans=0;
    35         for(int i=1;i<=N;i++){
    36             scanf("%d",&temp);
    37             update(temp,1);
    38             n1=sum(temp-1);//比temp小的;
    39             n2=N-temp-(i-n1-1);
    40         //    printf("%d %d
    ",n1,n2);
    41             ans+=n2*(n2-1)/2;
    42             ans-=n1*n2;
    43         }
    44         printf("Case #%d: %lld
    ",++flot,ans%MOD);
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    微软面试题_中文字符串转换为数字
    微软面试题_3个字母的组合
    LeetCode Notes_#1031_两个非重叠子数组的最大和
    剑指Offer_#57-II_和为 s 的连续正数序列
    LeetCode Notes_#560 和为k的子数组
    LeetCode Notes_#84 柱状图中最大的矩形
    LeetCode Notes_#221 最大正方形
    LeetCode Notes_#146 LRU缓存机制
    码云团队开发项目拉取与推送
    vue父子组件的双向绑定
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4966686.html
Copyright © 2011-2022 走看看