hdu 5122 樹狀數組?..其實是亂搞

http://acm.hdu.edu.cn/showproblem.php?pid=5122

求一個序列最少進行幾次變換(把某個數一冒泡排序的方式改變位置)才能使其有序。

對於每一個數只要後面的數有比它小就要換
所以逆着記錄最小值,計算下就可以了。。


但是一開始沒想到...寫了個樹狀數組過的..蠢哭了

<span style="font-size:14px;">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 1e6+5;
map<int,int > hash;
//#define a first
//#define b second
//typedef pair<int,int> p2;

//p2 s[maxn];
int bit[maxn],b[maxn];
template<class T> T min(T a,T b,T c)
{
    return min(min(a,b),c);
}
int n;
void add(int x,int y)
{
    for(int i = x;i <= n;i += i&-i)
        bit[i] += y;
}
int cal(int x)
{
    int sum = 0;
    for(int i = x;i > 0;i -= i&-i)
        sum += bit[i];
    return sum;
}
int work()
{
    clr0(bit);
    RD(n);
    int res = 0,ans = 0;
    for(int i = 1;i <= n;++i){
        RD(b[i]);
    }
    for(int i = n;i >= 1;--i){
        if(0 != cal(b[i]))
            ans++;
        add(b[i],1);
    }
    return ans;
}
int main()
{
    int _,cas = 1;
    RD(_);
    while(_--){
        printf("Case #%d: %d\n",cas++,work());
    }
    return 0;
}
/*

*/
</span>

正解..時間上沒差多少..

<span style="font-size:14px;">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 1e6+5;
map<int,int > hash;
//#define a first
//#define b second
//typedef pair<int,int> p2;

//p2 s[maxn];
int bit[maxn],b[maxn],n;
int work()
{
    RD(n);
    int mx = 0,ans = 0;
    for(int i = 1;i <= n;++i){
        RD(b[i]);
        mx = max(mx,b[i]);
    }
    for(int i = n;i >= 1;--i){
        if(mx < b[i])
            ans++;
        mx = min(b[i],mx);
    }
    return ans;
}
int main()
{
    int _,cas = 1;
    RD(_);
    while(_--){
        printf("Case #%d: %d\n",cas++,work());
    }
    return 0;
}
/*

*/
</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章