hdu 4038 Stone

时间:2024-07-11 20:06:44

思路:

如果负数的个数为偶数则不必改变,为奇数就将最大负数变为正;

对于正数,尽量将1,2变为3即可。

代码如下:

 #include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<string>
#define Maxn 2010
#define LL __int64
#define MM 1000000007
using namespace std;
priority_queue<LL> les;
priority_queue<LL ,vector<LL> ,greater<LL> >mor;
LL mul(LL x,LL e)
{
LL temp=;
while(e){
if(e&) temp=temp*x%MM;
e>>=;
x=x*x%MM;
}
return temp;
}
int main()
{
LL i,j,n,t,x,Case=;
LL m;
scanf("%I64d",&t);
while(t--)
{
while(!mor.empty())
mor.pop();
while(!les.empty())
les.pop();
scanf("%I64d%I64d",&n,&m);
for(i=;i<=n;i++)
{
scanf("%I64d",&x);
if(x<)
les.push(x);
else
mor.push(x);
}
LL temp;
LL lz,mz;
mz=mor.size();
lz=les.size();
if(lz%)
{
temp=les.top();
if(m+(LL)temp>=){
m+=(LL)temp;
les.pop();
mor.push();
}
else{
les.pop();
les.push(temp+(LL)m);
m=;
}
}
while(m&&!mor.empty()){
temp=mor.top();
if(temp==){
mor.pop();
mor.push();
m--;
}
if(temp==){
mor.pop();
mor.push();
m--;
}
if(temp==){
mor.pop();
mor.push();
m--;
}
if(temp>=){
if(m==){
mor.pop();
mor.push(temp+);
m--;
}
break;
}
}
LL ans=;
LL mod=m%;
if(mod==){
if(m>=){
m-=;
mor.push();
mor.push();
}
else{
temp=mor.top();
mor.pop();
mor.push(temp+);
}
}
ans=mul(,m/);
if(mod==)
mor.push();
while(!mor.empty()){
temp=mor.top();
mor.pop();
ans*=(LL)temp;
ans%=MM;
}
while(!les.empty()){
temp=les.top();
les.pop();
ans*=(LL)temp;
ans%=MM;
}
ans%=MM;
printf("Case %I64d: %I64d\n",++Case,ans);
}
return ;
}