sgu548

http://acm.sgu.ru/problem.php?contest=0&problem=548 还是太弱了,只会模板题和水题,稍微需要思考的就不会了,跟江哥讨论了一下午,还问了两个牛,才搞懂。。。 自己想偏了一些,总是卡在最后一步上,以后要从多个方向想问题,前后。。。 [code lang="cpp"] #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <cassert> #include <iostream> #include <sstream> #include <fstream> #include <map> #include <set> #include <vector> #include <queue> #include <algorithm> #include <iomanip> #define maxn 210000 using namespace std; int n; int f[maxn]; int wei[maxn]; struct node {     char op[5];     int x;     int id;     bool operator< (const node &a) const     {         return x<a.x;     } } a[maxn]; int last,ff; int num; priority_queue<node> xx; int res,no; int ans[maxn]; int main() {     char ch[5];     int x;     node tmp;     while(scanf("%d",&n)!=EOF)     {         num = 1 ;         wei[1] = 1;         for(int i=2 ; i<=n ; i++)         {             scanf("%s%d",ch,&x);             strcpy(a[i].op,ch);             strcpy(a[i].op,ch);             a[i].x = x;             a[i].id = i;             if(ch[0]=='p')             {                 num ++;                 wei[num] = i;             }         }         for(int i= 0 ; i<= n ; i++) f[i] = -1;         f[1] = 1;         f[wei[num-1]] = a[wei[num-1]].x;         for(int i = num - 2 ; i >= 2; i -- )         {             f[wei[i]] = min(f[wei[i+1]],a[wei[i]].x);         }         res = 0;         no = 0;         while(!xx.empty()) xx.pop();         last = wei[ num - 1 ];         int  i;         i = num - 1;         while( i >= 1 )         {             if(last == - 1) last = wei[i];             for(int j = wei[i] + 1 ; j < last; j ++)             {                 if(a[j].op[0]=='d') xx.push(a[j]);             }             int sec;             sec = f[last] - f[wei[i]];             if(sec > 0)                 while(sec-- && !xx.empty())   //!!!!!!!空的话再往外top或pop就会re                 {                     tmp = xx.top();                     res += tmp.x;                     no++;                     ans[no] = tmp.id;                     xx.pop();                 }             last = wei[i];             i --;         }         for(int j = wei[ num - 1 ] + 1 ; j < n ; j ++)         {             res += a[j].x;             no ++;             ans[no] = a[j].id;         }         if(no < a[n].x)         {             printf("-1\n");         }         else         {             printf("%d\n",res);             printf("%d\n",no);             sort(ans+1,ans+no+1);             for(i=1 ; i<=no; i++) printf("%d ",ans[i]);             printf("\n");         }     }     return 0; } [/code] 我的空格被吃掉了,好可恶。。。。 用IE写就好了。。。

posted on 2012-10-24 09:21  oshixiaoxiliu  阅读(143)  评论(0编辑  收藏  举报

导航