[贪心+暴力矫正] zoj 3685 Cube

题目链接:

http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=4938

Cube

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

S=(-1)Kn*n3+(-1)Kn-1*(n-1)3+...+(-1)K2*23+(-1)K1*13.
Your task is to change Ki to find the minimum |S|.

Input

Mutiple test cases, process to the end of file.
Each case has a single line with one integer n (1≤n≤10000).

Output

For each case:
Output n characters with '+' or '-'. ('+' means (-1)Ki=1,'-' means (-1)Ki=-1)

Sample Input
1
3
Sample Output
+
-++
Hint
Sample 1: |+13|=1
Sample 2: |-33+23+13|=18

Author:  HE, Yueyang
Submit     Status

题目意思:

S=(-1)Kn*n3+(-1)Kn-1*(n-1)3+...+(-1)K2*23+(-1)K1*13.
确定每个ki,使得|S|最小。

解题思路:

贪心+暴力矫正

对于超过20的,如果当前是大于0,就把系数置为-1,否则置为+1.

然后枚举20内的+-情况,把能凑成最小的那个状态找到。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 11000

LL sum[1<<20]; //sum[i]:1~20内的状态i所表示+的数的三次方和
int n;
char save[Maxn];

void init()
{
    int lim=1<<20;
    for(int i=0;i<lim;i++)
    {
        sum[i]=0;
        for(int j=0;j<20;j++)
            if(i&(1<<j))
                sum[i]+=(j+1)*(j+1)*(j+1);
    }

}
int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   init();

   //printf("%d\n",1<<20);
   while(~scanf("%d",&n))
   {
       int k=min(n,20); //20以内的暴力矫正
       LL a=0,ans=(1LL<<60),lim=1<<k,all=0,re;

       for(int i=n;i>k;i--)
       {
           if(a>0)
           {
               a-=(LL)i*(LL)i*(LL)i;
               save[i]='-';
           }
           else
           {
               a+=(LL)i*(LL)i*(LL)i;
               save[i]='+';
           }
       }
       for(int i=1;i<=k;i++)
            all+=(LL)i*(LL)i*(LL)i;

       for(int i=0;i<lim;i++) //把i状态 表示的数全都搞进去
       {
           if(abs(a+2*sum[i]-all)<ans) //状态i表示的数是加,其余数是减
           {
               ans=abs(a+2*sum[i]-all);
               re=i;
           }
       }
       for(int i=0;i<k;i++)
            if(re&(1<<i))
                save[i+1]='+';
            else
                save[i+1]='-';
       for(int i=n;i>=1;i--)
            printf("%c",save[i]);
       putchar('\n');


   }

   return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值