• 数组(array)


    由于负数是肯定小于正数的,我们首先想到的就是将乘积变成负数。我们可以统计输入的负数个数。

    那么会有两种情况:

    (i)无论怎么变变不了负数,这个时候我们将所有数取绝对值。

    我们需要使乘积最小,这时只需要将绝对值最小的数减小即可。

    (ii)可以变成负数,我们变完负数后同样将所有数取绝对值。

    既然已经是负数了,那我们只需所有的数的绝对值积最大即可。

    证明同上,只是改变了加减。

    所以我们只需要用小根堆维护绝对值最小的数即可。

    我是用两个堆分别维护正数和负数。同时注意不要边减边取模,因为保存的是绝对值,和负数直接取模不同。

    贴出原文网址:http://www.cnblogs.com/NaVi-Awson/p/7382746.html

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<cmath>
      6 #include<queue>
      7 using namespace std;
      8 typedef long long LL;
      9 //priority_queue<LL>大根 
     10 priority_queue<LL,vector<LL>,greater<LL> >a,b;//小根堆 
     11 LL n,x,k,s;
     12 LL Mod=1000000007;
     13 void print()
     14 {LL ans=1;
     15 LL tmp;
     16     while (a.empty()==0)
     17     {tmp=a.top()%Mod;
     18         ans=(ans*tmp)%Mod;
     19         a.pop();
     20     }
     21     while (b.empty()==0)
     22     {tmp=-b.top()%Mod;
     23         ans=(ans*tmp)%Mod;
     24         b.pop();
     25     }
     26 cout<<(ans+Mod)%Mod<<endl;
     27 exit(0);
     28 }
     29 void cuta()
     30 {
     31     if (a.top()/x+(bool)(a.top()%x)>=k)
     32     {
     33         LL tmp=a.top();
     34         a.pop();
     35          tmp=(tmp-(LL)k*(LL)x);
     36          a.push(tmp);
     37          print();
     38     }
     39     else 
     40     {
     41         LL tmp=a.top();
     42         k-=tmp/x+(bool)(tmp%x);
     43          tmp=(tmp-(tmp/x+(bool)(tmp%x))*(LL)x);
     44           a.pop();
     45          b.push(-tmp);
     46     }
     47 }
     48 void cutb()
     49 {
     50     if (b.top()/x+(bool)(b.top()%x)>=k)
     51     {
     52         LL tmp=b.top();
     53         b.pop();
     54          tmp=(tmp-(LL)k*(LL)x);
     55          b.push(tmp);
     56          print();
     57     }
     58     else 
     59     {
     60         LL tmp=b.top();
     61         k-=tmp/x+(bool)(tmp%x);
     62          tmp=(tmp-(tmp/x+(bool)(tmp%x))*(LL)x);
     63           b.pop();
     64          a.push(-tmp);
     65     }
     66 }
     67 int main()
     68 {int i,cnt=0;
     69 //freopen("array.in","r",stdin);
     70 //freopen("array.out","w",stdout);
     71     cin>>n>>k>>x;
     72      for (i=1;i<=n;i++)
     73       {
     74         scanf("%lld",&s);
     75          if (s>=0) a.push(s);
     76          else cnt++,b.push(-s);
     77       }
     78      if (cnt%2==0)
     79      {
     80         if (cnt==0) {cuta();}
     81         else if (cnt==n) {cutb();}
     82         else 
     83         {
     84             if (a.top()<=b.top()) cuta();
     85             else cutb();
     86         }
     87         //cout<<cnt<<endl;
     88      }
     89       for (i=1;i<=k;i++)
     90       {LL tmp;
     91       int flag;
     92             if (a.empty()) 
     93             {tmp=b.top();b.pop();flag=0;}
     94             else if (b.empty())
     95             {tmp=a.top();a.pop();flag=1;}
     96             else
     97             {
     98                 if (a.top()<b.top()) 
     99                 {tmp=a.top();a.pop();flag=1;}
    100                 else {tmp=b.top();b.pop();flag=0;} 
    101             }
    102         tmp=tmp+x;
    103          if (flag) a.push(tmp);
    104          else b.push(tmp);
    105       }
    106     print();
    107 }
  • 相关阅读:
    hdu 1423 LICS
    poj 1135
    poj 1112
    poj 1087
    poj 1094
    谷歌浏览器字体小于12px不能正常显示bug
    gulpfile.js配置 实现ctrl+s自动编译和刷新浏览器
    <hr>标签横线的颜色
    jQuery轮播图鼠标移入停止,移出播放,点击小横条切换图片
    最简单的jq轮播图
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7383904.html
Copyright © 2020-2023  润新知