xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····

下面的代码(比较垃圾)在vs2008下有运行错误...我找了很久,终于找到错误...不过不知道什么地方有问题...

  1 #include <iostream>
  2 #include <queue>
  3 
  4 using namespace std;
  5 struct Node
  6 {
  7     int rate;
  8     int index;
  9     Node(int r=0,int i=0):rate(r),index(i){};
 10     bool operator<(const Node& c)const{
 11         if(rate!=c.rate)return rate<c.rate;
 12         return index>c.index;
 13     };
 14 };
 15 priority_queue<Node> que1,que2;
 16 int main()
 17 {
 18     int n,t;
 19     while(cin>>n>>t){
 20         while(!que1.empty())que1.pop();
 21         while(!que2.empty())que2.pop();
 22         int r;
 23         for(int i=1;i<=n;i++){
 24             cin>>r;
 25             Node tmp(r,i);
 26             que1.push(tmp);
 27         }
 28         if(n==1){
 29             for(int i=0;i<t;i++)
 30                 cout<<r<<endl;
 31             continue;
 32         }
 33         int num=0;
 34         while(1){
 35             cout<<que1.top().index<<endl;        
 36             num++;
 37             if(num==t)break;
 38             int mo=que1.top().rate%(n-1);
 39             int ev=(que1.top().rate-mo)/(n-1);
 40             que1.top().rate=0;
 41             Node tj=que1.top();
 42             //tj.rate=0;
 43             if(que1.top().index<=mo){
 44                 que1.pop();
 45                 while(!que1.empty()){
 46                     Node tp=que1.top();
 47                     if(tp.index<=mo+1){
 48                         tp.rate+=1+ev;
 49                     }
 50                     else tp.rate+=ev;
 51                     que1.pop();
 52                     que2.push(tp);
 53                 }
 54                 que2.push(tj);
 55             }
 56             else {
 57                 que1.pop();
 58                 while(!que1.empty()){
 59                     Node tm=que1.top();
 60                     if(tm.index<=mo){
 61                         tm.rate+=1+ev;
 62                     }
 63                     else tm.rate+=ev;
 64                     que1.pop();
 65                     que2.push(tm);
 66                 }
 67                 que2.push(tj);
 68             }
 69 
 70             cout<<que2.top().index<<endl;
 71             num++;
 72             if(num==t)break;
 73 
 74             mo=que2.top().rate%(n-1);
 75             ev=(que2.top().rate-mo)/(n-1);
 76             que2.top().rate=0;
 77             tj=que2.top();
 78             //tj.rate=0;
 79             if(que2.top().index<=mo){
 80                 que2.pop();
 81                 while(!que2.empty()){
 82                     Node tp=que2.top();
 83                     if(tp.index<=mo+1){
 84                         tp.rate+=1+ev;
 85                     }
 86                     else tp.rate+=ev;
 87                     que2.pop();
 88                     que1.push(tp);
 89                 }
 90                 que1.push(tj);
 91             }
 92             else {
 93                 que2.pop();
 94                 while(!que2.empty()){
 95                     Node tm=que2.top();
 96                     if(tm.index<=mo){
 97                         tm.rate+=1+ev;
 98                     }
 99                     else tm.rate+=ev;
100                     que2.pop();
101                     que1.push(tm);
102                 }
103                 que1.push(tj);
104             }
105         }
106     }
107     return 0;
108 }
修改后无错误的代码:
  1 #include <iostream>
  2 #include <queue>
  3 
  4 using namespace std;
  5 struct Node
  6 {
  7     int rate;
  8     int index;
  9     Node(int r=0,int i=0):rate(r),index(i){};
 10     bool operator<(const Node& c)const{
 11         if(rate!=c.rate)return rate<c.rate;
 12         return index>c.index;
 13     };
 14 };
 15 priority_queue<Node> que1,que2;
 16 int main()
 17 {
 18     int n,t;
 19     while(cin>>n>>t){
 20         while(!que1.empty())que1.pop();
 21         while(!que2.empty())que2.pop();
 22         int r;
 23         for(int i=1;i<=n;i++){
 24             cin>>r;
 25             Node tmp(r,i);
 26             que1.push(tmp);
 27         }
 28         if(n==1){
 29             for(int i=0;i<t;i++)
 30                 cout<<r<<endl;
 31             continue;
 32         }
 33         int num=0;
 34         while(1){
 35             cout<<que1.top().index<<endl;        
 36             num++;
 37             if(num==t)break;
 38             int mo=que1.top().rate%(n-1);
 39             int ev=(que1.top().rate-mo)/(n-1);
 40             //que1.top().rate=0;
 41             Node tj=que1.top();
 42             tj.rate=0;
 43             if(que1.top().index<=mo){
 44                 que1.pop();
 45                 while(!que1.empty()){
 46                     Node tp=que1.top();
 47                     if(tp.index<=mo+1){
 48                         tp.rate+=1+ev;
 49                     }
 50                     else tp.rate+=ev;
 51                     que1.pop();
 52                     que2.push(tp);
 53                 }
 54                 que2.push(tj);
 55             }
 56             else {
 57                 que1.pop();
 58                 while(!que1.empty()){
 59                     Node tm=que1.top();
 60                     if(tm.index<=mo){
 61                         tm.rate+=1+ev;
 62                     }
 63                     else tm.rate+=ev;
 64                     que1.pop();
 65                     que2.push(tm);
 66                 }
 67                 que2.push(tj);
 68             }
 69 
 70             cout<<que2.top().index<<endl;
 71             num++;
 72             if(num==t)break;
 73 
 74             mo=que2.top().rate%(n-1);
 75             ev=(que2.top().rate-mo)/(n-1);
 76             //que2.top().rate=0;
 77             tj=que2.top();
 78             tj.rate=0;
 79             if(que2.top().index<=mo){
 80                 que2.pop();
 81                 while(!que2.empty()){
 82                     Node tp=que2.top();
 83                     if(tp.index<=mo+1){
 84                         tp.rate+=1+ev;
 85                     }
 86                     else tp.rate+=ev;
 87                     que2.pop();
 88                     que1.push(tp);
 89                 }
 90                 que1.push(tj);
 91             }
 92             else {
 93                 que2.pop();
 94                 while(!que2.empty()){
 95                     Node tm=que2.top();
 96                     if(tm.index<=mo){
 97                         tm.rate+=1+ev;
 98                     }
 99                     else tm.rate+=ev;
100                     que2.pop();
101                     que1.push(tm);
102                 }
103                 que1.push(tj);
104             }
105         }
106     }
107     return 0;
108 }
原因分析(个人):vs08有检查机制,当对pq顶部直接改没问题,但是又调用pop的时候,其实现在的堆已不是正常的
了。所以回出现错误,而03下没检查机制,所以不会出现错误.如果想对pq的顶部改时,好的习惯是先弹出,修改后再压回去...
posted on 2008-07-22 19:18 小果子 阅读(127) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理