hdu 5122 树状数组?..其实是乱搞
来源:互联网 发布:掷骰子抽奖js特效 编辑:程序博客网 时间:2024/05/02 02:39
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-9const 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-9const 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>
0 0
- hdu 5122 树状数组?..其实是乱搞
- HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】
- HDU 5122 树状数组应用
- BZOJ 1878: [SDOI2009]HH的项链 树状数组+乱搞
- 2016 10 28考试 dp 乱搞 树状数组
- HDU 1394 树状数组
- 树状数组 hdu
- hdu 1166 树状数组
- HDU 2838 树状数组
- hdu 2492【树状数组】
- hdu 3015【树状数组】
- hdu 3874 树状数组
- hdu 3874(树状数组)
- hdu 3890 树状数组
- 【树状数组】hdu 2852
- 【树状数组】hdu 4000
- HDU 4046 树状数组
- hdu 1166(树状数组)
- js常用方法总结-url中文乱码问题
- Eclipse右键New菜单项的自定义设置
- Html5中不在适用的html4的属性总结
- 第十五周项目二-阅读程序1
- hdu 1595 find the longest of the shortest (spfa)
- hdu 5122 树状数组?..其实是乱搞
- Get Start StrangeIOC for Unity3D
- 黑马程序员——java加强之注解
- 教皇修改之后丢失的十天
- eclipse中汉字过小的解决办法
- 重装win7后如何恢复ubuntu引导
- 防止页面客户端被缓存
- hdu45221——小明系列问题——小明序列 线段树优化dp
- 定时关闭消息对话框