hdu 4038 Stone
思路:
如果负数的个数为偶数则不必改变,为奇数就将最大负数变为正;
对于正数,尽量将1,2变为3即可。
代码如下:
1 #include<cstring> 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 #include<vector> 9 #include<string> 10 #define Maxn 2010 11 #define LL __int64 12 #define MM 1000000007 13 using namespace std; 14 priority_queue<LL> les; 15 priority_queue<LL ,vector<LL> ,greater<LL> >mor; 16 LL mul(LL x,LL e) 17 { 18 LL temp=1; 19 while(e){ 20 if(e&1) temp=temp*x%MM; 21 e>>=1; 22 x=x*x%MM; 23 } 24 return temp; 25 } 26 int main() 27 { 28 LL i,j,n,t,x,Case=0; 29 LL m; 30 scanf("%I64d",&t); 31 while(t--) 32 { 33 while(!mor.empty()) 34 mor.pop(); 35 while(!les.empty()) 36 les.pop(); 37 scanf("%I64d%I64d",&n,&m); 38 for(i=1;i<=n;i++) 39 { 40 scanf("%I64d",&x); 41 if(x<0) 42 les.push(x); 43 else 44 mor.push(x); 45 } 46 LL temp; 47 LL lz,mz; 48 mz=mor.size(); 49 lz=les.size(); 50 if(lz%2) 51 { 52 temp=les.top(); 53 if(m+(LL)temp>=0){ 54 m+=(LL)temp; 55 les.pop(); 56 mor.push(0); 57 } 58 else{ 59 les.pop(); 60 les.push(temp+(LL)m); 61 m=0; 62 } 63 } 64 while(m&&!mor.empty()){ 65 temp=mor.top(); 66 if(temp==0){ 67 mor.pop(); 68 mor.push(1); 69 m--; 70 } 71 if(temp==1){ 72 mor.pop(); 73 mor.push(2); 74 m--; 75 } 76 if(temp==2){ 77 mor.pop(); 78 mor.push(3); 79 m--; 80 } 81 if(temp>=3){ 82 if(m==1){ 83 mor.pop(); 84 mor.push(temp+1); 85 m--; 86 } 87 break; 88 } 89 } 90 LL ans=0; 91 LL mod=m%3; 92 if(mod==1){ 93 if(m>=3){ 94 m-=3; 95 mor.push(2); 96 mor.push(2); 97 } 98 else{ 99 temp=mor.top(); 100 mor.pop(); 101 mor.push(temp+1); 102 } 103 } 104 ans=mul(3,m/3); 105 if(mod==2) 106 mor.push(2); 107 while(!mor.empty()){ 108 temp=mor.top(); 109 mor.pop(); 110 ans*=(LL)temp; 111 ans%=MM; 112 } 113 while(!les.empty()){ 114 temp=les.top(); 115 les.pop(); 116 ans*=(LL)temp; 117 ans%=MM; 118 } 119 ans%=MM; 120 printf("Case %I64d: %I64d\n",++Case,ans); 121 } 122 return 0; 123 }