HOJ 2275 Number sequence

题意:问你有多少个序列满足Ai < Aj > Ak and i < j < k.

思路:对每个数求它之前和之后分别有多少个个数比它小,两边相乘。最后求和。具体实现先用树状数组正序求,再反过来再用一遍树状数组。

代码:特别要注意的是题目中Ai的范围是从0开始,但是树状数组不能从0开始,所以统一加一处理。但是可惜的是题目并没有出现0这样的数据,让一些并不正确的代码过了。

 1 #include<stdio.h>
 2 #include<string.h>
 3 int lowbit(int x)
 4 {
 5     return x&-x;
 6 }
 7 const int N=5e4+111,Max=32769;
 8 int c[N],a[N],ans[N],n;
 9 void add(int i,int x)
10 {
11     i++;
12     while(i<Max)
13     {
14         c[i]+=x;
15         i+=lowbit(i);
16     }
17 }
18 int q(int i)
19 {
20     i++;
21     int sum=0;
22     while(i)
23     {
24         sum+=c[i];
25         i-=lowbit(i);
26     }
27     return sum;
28 }
29 int main()
30 {
31     while(scanf("%d",&n)!=EOF)
32     {
33         long long Ans=0;
34         memset(c,0,sizeof(c));
35         for(int i=1;i<=n;i++)
36         {
37             scanf("%d",&a[i]);
38             ans[i]=q(a[i]-1);
39             add(a[i],1);
40         }
41         memset(c,0,sizeof(c));
42         for(int i=n;i;i--)
43         {
44             Ans+=ans[i]*q(a[i]-1);
45             add(a[i],1);
46         }
47         printf("%lld\n",Ans);
48     }
49     return 0;
50 }
时间: 2024-11-02 21:12:54

HOJ 2275 Number sequence的相关文章

HOJ 2275 Number sequence(树状数组)

题意:给定一个n个元素的数列,令Ai, Aj, Ak,使得 Ai < Aj > Ak 且 i < j < k这样的三个数为一组.求出一共有多少组. 思路:可以用树状数组,每次输入一个Ai,可以查询到之前输入的比它小或比它大的有多少组,之后输入的就不得而知了,所以可以开个数组记录下来逆序再建树一次即可 另外数的范围取到了0,所以每个数要自加一次,而且组数是longlong的,而且HOJ的服务器系统支持的是lld,不是i64d..wa了几次 #include<cstdio>

求序列中满足Ai &lt; Aj &gt; Ak and i &lt; j &lt; k的组数 树状数组 HIT 2275 Number sequence

http://acm.hit.edu.cn/hoj/problem/view?id=2275 Number sequence   Source : SCU Programming Contest 2006 Final   Time limit : 1 sec   Memory limit : 64 M Submitted : 1632, Accepted : 440 Given a number sequence which has N element(s), please calculate

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

TOJ 1203: Number Sequence

1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 957            Accepted:208 Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Gi

ACM—Number Sequence(HDOJ1005)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 看到这样的公式很容易想到递归调用求解,但是在本题中n的取

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i