1612 - Guess(贪心)

本文探讨了一个在处理浮点数时遇到精度问题的算法案例,并详细阐述了如何通过四舍五入操作(使用round函数)来解决此类问题,确保计算结果的准确性。文章还介绍了输入和输出数据的格式调整,以及算法实现过程中的关键细节。

该题的思路很好想,就是尽量取大一些的成绩为后面的人留出更大的 “空间” 。   细节也很好想,这里不再赘述,值得一提的是 : 该题存在精度问题 , 导致我一开始一直WA。

由于输入和输出只保留两位小数,所以我们不妨将浮点数变成小数,这里要用到round函数,将不需要的小数位四舍五入到整数 。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 16384 + 10;
int n,rankk[maxn],Case = 0;
struct node {
    double a,b,c;
}a[maxn];
int main() {
    while(~scanf("%d",&n)&&n) {
       for(int i=1;i<=n;i++) { scanf("%lf%lf%lf",&a[i].a,&a[i].b,&a[i].c); a[i].a*=100.0; a[i].b*=100.0; a[i].c*=100.0; }
       for(int i=1;i<=n;i++) scanf("%d",&rankk[i]);
       int cur = a[rankk[1]].a + a[rankk[1]].b + a[rankk[1]].c;
       bool ok = true;
       for(int i=2;i<=n;i++) {
            int cnt[10];
            cnt[0] = (int)round(a[rankk[i]].a); cnt[1] = (int)round(a[rankk[i]].b); cnt[2] = (int)round(a[rankk[i]].c);
            cnt[3] = (int)round(a[rankk[i]].a + a[rankk[i]].b); cnt[4] = (int)round(a[rankk[i]].a + a[rankk[i]].c);
            cnt[5] = (int)round(a[rankk[i]].b + a[rankk[i]].c); cnt[6] = (int)round(a[rankk[i]].a + a[rankk[i]].b + a[rankk[i]].c); cnt[7] = 0;
            if(rankk[i] > rankk[i-1]) {
                sort(cnt,cnt+8);
                bool flage = false;
                for(int i=7;i>=0;i--) {
                    if(cnt[i] > cur) continue;
                    else { cur = cnt[i]; flage = true; break; }
                }
                if(!flage) { ok = false; break; }
            }
            else {
                sort(cnt,cnt+8);
                bool flage = false;
                for(int i=7;i>=0;i--) {
                    if(cnt[i] >= cur) continue;
                    else { cur = cnt[i]; flage = true; break; }
                }
                if(!flage) { ok = false; break; }
            }
       }
       printf("Case %d: ",++Case);
       if(ok) printf("%.2lf\n",cur/100.0);
       else printf("No solution\n");
    }
    return 0;
}


 

Algorithm 1: Process of selecting guess bits to attack 2-round AsconXof using the greedy algorithm Input: N : Number of bits of Y 2 0 to be linearized (1 < N < 64) Output: B: Set of bit indices to be guessed in X1 0 L: Set of bit indices to be linearized in Y 2 0 1: I := {0, 1, . . . , 63} 2: B := ∅, Ball := ∅, L := ∅, Lall := ∅ 3: for j ∈ I do 4: T := I, BC := ∅, LC := ∅ 5: LC ← LC ∪ {j} 6: T ← T − {j} 7: BC ← BC ∪ {j} ∪ {j − 19} ∪ {j − 28} 8: for cnt ∈ {0, 1, . . . , N − 2} do 9: M ← The bit index of Y 2 0 that can be linearized by guessing the least bit of X1 0 (M ∈ T ) 10: Mbit ← The bit indices of X1 0 that linearize M of Y 2 0 11: BC ← BC ∪ {Mbit} 12: LC ← LC ∪ {M} 13: T ← T − {M} 14: end for 15: for k ∈ I − LC do 16: if Y 2 0 [k] is linearized because of the elements of BC then 17: LC ← LC ∪ {k} 18: end if 19: end for 20: if there is any one of the bit indices of I − BC that cannot be included as a variable in the linear equations generated from the bits of LC then 21: continue 22: else 23: Ball ← Ball ∪ {BC } 24: Lall ← Lall ∪ {LC } 25: end if 26: end for 27: Min ← Minimum size of elements of Ball 28: Max ← Of the elements of Ball whose size is Min, the maximum size of the corresponding elements of Lall 29: B ← Element of Ball that has a size Min and has a size Max of an element of Lall in that index 30: L ← Element of Lall that correspond to B 31: return B, L这是一个贪心算法的位代码,我现在要优化该代码实现更少的猜测位,帮我优化一下并用python实现
最新发布
05-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值