nyoj 55 懒省事的小明 (优先队列||直接插入)
2015-04-25 10:05
211 查看
//优先队列 #include <stdio.h>//老大们可以注意了 ,一定要用long long 啊啊啊 我错了半页 就是一直用的int #include <queue> using namespace std; priority_queue<int,vector<int>,greater<int> >s;//先出小的。。感觉这样解释比较容易懂,greater换成less就是先出大的。 int main() { long long test,n,sum,x,y; scanf("%lld",&test); while(test--) { scanf("%lld",&n); for(int i=0;i<n;i++) scanf("%lld",&x),s.push(x);// if(n==1) { printf("%d\n",s.top()); s.pop(); continue; } sum=0; while(!s.empty()) { x=s.top(),s.pop(); y=s.top(),s.pop(); sum+=x+y; if(!s.empty()) s.push(x+y); } printf("%lld\n",sum); } return 0; }
---------------------------------------------------------------------------------------- #include <stdio.h>//直接插入 以前做的 因为vc不支持long long 就用的double 你懂得。。。 #include <string.h> #include <algorithm> using namespace std; int main() { double a[12001],b[12001],sum; int t,i,q,j,n,temp; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); scanf("%d",&n); for(i=0;i<n;i++) scanf("%lf",&a[i]); sort(a,a+n);//想插入 肯定要排序啦 for(i=1,j=0;i<n;i++) { b[j++]=a[i]+a[i-1]; a[i]=a[i]+a[i-1]; for(q=i;q<n-1;q++)//慢慢寻找0.0 { if(a[q]>a[q+1]) temp=a[q],a[q]=a[q+1],a[q+1]=temp; else break; } } sum=0; for(i=0;i<j;i++) sum+=b[i]; printf("%.lf\n",sum); } return 0; }
相关文章推荐
- NYOJ 55 懒省事的小明 【优先队列】
- nyoj 55 懒省事的小明 优先队列 multiset 还有暴力
- NYOJ 55--懒省事的小明【优先队列】
- NYOJ - 55 - 懒省事的小明(优先队列-priority_queue)
- NYOJ 55 懒省事的小明(简单优先队列)
- nyoj 55 懒省事的小明 【优先队列】
- nyoj 55 懒省事的小明 优先队列 multiset 还有暴力
- nyoj--55--懒省事的小明(STL优先队列)
- nyoj--55--懒省事的小明(STL优先队列)
- nyoj55-懒省事的小明(优先队列排序,set)
- nyoj 55 懒省事的小明【贪心+优先队列】
- nyoj 55 懒省事的小明(priority_queue优先队列)
- priority_queue优先队列容器。(爱上原创和改造) nyoj 55 懒省事的小明
- nyoj 55 懒省事的小明【优先队列】
- nyoj 55 懒省事的小明【优先队列】
- 初试队列 nyoj55 懒省事的小明
- nyoj55懒省事的小明(STL优先队列)
- Num 22 : NYOJ : 0055 懒省事的小明 [ 优先队列 ]
- NY--55 -- 懒省事的小明 [STL] [优先队列]
- 【南理oj】55 - 懒省事的小明(优先队列,贪心)