HDU 3584 Cube POJ 2299 Ultra-QuickSort

来源:互联网 发布:landsat数据预处理 编辑:程序博客网 时间:2024/05/29 09:30
Problem Description
Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. Initially we have A[i, j, k] = 0 (1 <= i, j, k <= N). 
We define two operations, 1: “Not” operation that we change the A[i, j, k]=!A[i, j, k]. that means we change A[i, j, k] from 0->1,or 1->0. (x1<=i<=x2,y1<=j<=y2,z1<=k<=z2).
0: “Query” operation we want to get the value of A[i, j, k].
 

Input
Multi-cases. First line contains N and M, M lines follow indicating the operation below. Each operation contains an X, the type of operation. 1: “Not” operation and 0: “Query” operation. If X is 1, following x1, y1, z1, x2, y2, z2. If X is 0, following x, y, z.
 

Output
For each query output A[x, y, z] in one line. (1<=n<=100 sum of m <=10000)
 

Sample Input
2 51 1 1 1 1 1 10 1 1 11 1 1 1 2 2 20 1 1 10 2 2 2
 

Sample Output
101
题意:初始是0,两种操作,1 把这个空间内全部变成1或0取决于原来是什么是整个空间内的点
0 查询这个点是1 or 0
思路:放在树桩数组专题里,肯定是树桩数组啦,但是我一看数据量好像也不怎么大,有点勉强,果断试水了一发,tle,修改了输入输出格式之后,竟然就这么a了。下面这个代码就谈不上什么思路了,直接就是计数修改次数,暴力查找。之后看了看树桩数组的解法果然是快。。。
这个时间我自己都怕啊~~
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int n,m;int x,y,z;struct node{    int x1,y1,z1;    int x2,y2,z2;}map[10010];int main(){    int num=0;    int temp;    while(scanf("%d %d",&n,&m)!=EOF)    {        num=0;        while(m--)        {            scanf("%d",&temp);            if(temp==1)            {                scanf("%d%d%d%d%d%d",&map[num].x1,&map[num].y1,&map[num].z1,&map[num].x2,&map[num].y2,&map[num].z2);                num++;            }            else            {                scanf("%d%d%d",&x,&y,&z);                int ans=0;                for(int i=0;i<num;i++)                if(map[i].x1<=x&&x<=map[i].x2&&map[i].y1<=y&&y<=map[i].y2&&map[i].z1<=z&&z<=map[i].z2)                ans++;                if(ans%2==0)                printf("0\n");                else                printf("1\n");                }                }        }    return 0;    }

Problem Description
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,
Ultra-QuickSort produces the output 
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
 

Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
 

Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
 

Sample Input
59105431230
 

Sample Output
60
题意:逆序数 哎 就是求全部位置忠当前位置比队伍前面小的数的个数的和
PPT正版,可以把数一个个插入到树状数组中,每插入一个数,统计比他小的数的个数,对应的逆序为 i- getsum( data[i] ),其中 i 为当前已经插入的数的个数, getsum( data[i] )为比 data[i] 小的数的个数,i- getsum( data[i] ) 即比 data[i] 大的个数,即逆序的个数。最后需要把所有逆序数求和,就是在插入的过程中边插入边求和。
#include<iostream>#include<stdio.h>#include<cmath>#include<algorithm>#include<string.h>using namespace std;int b[500005],c[500005];int n;struct node{    int num,id;}a[500005];bool cmp(node a,node b){    return a.num<b.num;}int lowbit(int x){    return x&(-x);}void update(int i,int x){    while(i<=n)    {        c[i]+=x;        i+=lowbit(i);    }}int sum(int i){    int sum=0;    while(i>0)    {        sum+=c[i];        i-=lowbit(i);    }    return sum;}int main(){    long long ans;    while(scanf("%d",&n),n)    {        memset(b,0,sizeof(b));        memset(c,0,sizeof(c));        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i].num);            a[i].id=i;        }        sort(a+1,a+n+1,cmp);        b[a[1].id]=1;        for(int i=2;i<=n;i++)        {            if(a[i].num!=a[i-1].num)                b[a[i].id]=i;            else b[a[i].id]=b[a[i-1].id];        }        ans=0;        for(int i=1;i<=n;i++)        {            update(b[i],1);            ans+=(sum(n)-sum(b[i]));        }        printf("%lld\n",ans);    }    return 0;}


原创粉丝点击