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>