uva 11166 - Power Signs (贪心)

最小非零位表示法
本文探讨了整数的一种特殊二进制表示方法——带有正负符号的二进制表示,并提供了一种算法来找到该表示下具有最少非零位的整数表示形式。文章还包含了一个具体的实现示例。
部署运行你感兴趣的模型镜像

Problem B
Power Signs
Input: 
Standard Input

Output: Standard Output

 

"Increase my killing power, eh?"

–        Homer Simpson

–         

You are probably familiar with the binary representation of integers, i.e. writing a nonnegative integer n as ∑ai2i, where each ai is either 0 or 1. In this problem, we consider a so called signed binary representation, in which we still write n as ∑ai2i, but allow ai to take on the values -1, 0 and 1. We write a signed binary representation of a number as a vector (akak-1, ..., a1a0). For instance, n = 13 can be represented as (1, 0, 0, -1, -1) = 24 - 21 - 20.

The binary representation of a number is unique, but obviously, the signed binary representation is not. In certain applications (e.g. cryptography), one seeks to write a number n in signed binary representation with as few non-zero digits as possible. For example, we consider the representation (1, 0, 0, -1) to be a better representation of n = 7 than (1, 1, 1). Your task is to write a program which will find such a minimal representation.

Input
The input consists of several test cases (at most 25), one per line. Each test case consists of a positive integer n ≤ 250000 written in binary without leading zeros. The input is terminated by a case where n = 0, which should not be processed.

 

Output
For each line of input, output one line containing the signed binary representation of n that has the minimum number of non-zero digits, using the characters '-' for -1, '0' for 0 and '+' for +1. The number should be written without leading zeros. If there are several possible answers, output the one that is lexicographically smallest (by the ASCII ordering).

 

Sample Input                               Output for Sample Input

10000
1111
10111

0

 

+0000
+000-
++00-

 


Problem setter: Per Aurtrin

Special Thanks: Mikael Goldmann

让0减少的唯一方式,就是类似用+00-这样的形式替换111,还要注意连续的2个1,是否替换?如果2个1在开头,就不换,否则换,字典序会变小。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 50000 + 5;
typedef long long LL;
typedef pair<int,int> P;

char s[maxn];

void change(int last, int pos){
    s[last] = '-';
    for(int j = last-1;j > pos;j--){
        s[j] = '0';
    }
    s[pos] = '1';
}

int main(){
    while(scanf("%s", s+1)){
        if(s[1] == '0') break;
        int n = strlen(s+1);
        s[0] = '0';
        int pos = n;
        int sum = 0;
        int last = -1;
        while(pos >= 1){
            if(s[pos]=='1'){
                if(last == -1){
                    last = pos;
                }
                sum++;
            }
            else{
                if(sum >= 2){
                    change(last, pos);
                    sum = 1;
                    last = pos;
                }
                else{
                    sum = 0;
                    last = -1;
                }
            }
            pos--;
        }
        if(sum > 2){
            change(last, pos);
        }
        for(int i = 0;i <= n;i++){
            if(s[i]=='1')
                s[i] = '+';
        }
        if(s[0]=='0')
            printf("%s\n",s+1);
        else
            printf("%s\n",s);
    }
    return 0;
}


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### Windows 11 Kernel-Power 41 Event Overview Kernel-Power 41 events occur when the system experiences a sudden loss of power or an unexpected shutdown, which can be caused by hardware issues such as faulty batteries or power supplies, software conflicts, driver problems, or operating system errors[^1]. ### Common Causes of Kernel-Power 41 Events One common cause is related to battery management where the system might not correctly interpret low battery signals leading to abrupt shutdowns. Another frequent issue involves malfunctioning drivers that may fail during critical operations causing instability in the system's power state transitions. Additionally, overheating components within the computer could trigger protective mechanisms resulting in unplanned reboots. ### Solutions for Resolving Kernel-Power 41 Issues To address these concerns effectively: #### Update Drivers Ensure all device drivers are up-to-date since outdated versions can lead to compatibility issues affecting stability. This includes graphics cards, network adapters, and especially chipset drivers provided by motherboard manufacturers. ```powershell Get-WindowsUpdateLog | Select-String -Pattern "Driver" ``` #### Check Power Settings Review current power settings ensuring they do not aggressively reduce performance levels under load conditions potentially triggering thermal throttling scenarios. ```powershell powercfg /list ``` #### Inspect Hardware Components Physically examine internal parts like RAM modules, cooling fans, heat sinks, etc., verifying proper installation without physical damage signs indicating potential failure points needing replacement. #### Analyze System Logs Utilize built-in tools such as Event Viewer focusing on entries around times corresponding with occurrence instances providing clues towards underlying faults requiring attention. ```powershell Get-WinEvent -FilterHashtable @{LogName='System'; Id=41} ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值