CF Hello 2018

  昨晚上做了2018的首个cf,明明是看着pass了两个题的,结果今天发现都没有对,才知道还能hack掉这一回事,不过另一道显示了wrong,莫非是我昨天眼花看错了?今天又补了下题,一共三个,题解如下:

Hello 2018

A. Modular Exponentiatio

  让求 , 但是(1 ≤ n ≤ 108).(1 ≤ m ≤ 108).数据量很吓人的,这怎么求幂?但是想想就知道,2^n大于m后就不需要求了,直接就是m。一下子简单多了。我算了下1e8的n恰好是24的,所以用的n>24,当时是对了,结果被hack,看人题解上大多是30,以后还是保险点的好。

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n, m;
    while(cin>>n>>m)
    {
        if(n>=30)
        {
            cout<<m<<endl;
            break;
        }
        int temp=pow(2, n);
        cout<<(m%temp)<<endl;
    }
    return 0;
}

B. Christmas Spruce

  B是让判断一个树是否为杉树,杉树就是非叶子结点的点,必要要有三个以上的叶子节点,的树。其实很好判断的,记录下来树,一遍深搜遍历下来加几句判断就ok,但是我判断上出了错,今天补题时候交了好几次才发现多余判断。说来也巧,我那题连着交了三四次错解,一个English邮件就给我发过来了,告诉我哪里错了,整的我还以为cf还有这种服务,居然帮忙debug,受宠若惊。和人聊了下才知道是一个好心老哥(当然是English交流),万分感谢,好人一生平安。

#include<iostream>
#include<cstring>
using namespace std;
struct Edge
{
    int next;
    int to;
}edge[1010];
int head[1010];
int K;
int flag;
void addEdge(int u, int v)
{
    edge[++K].next=head[u];
    edge[K].to=v;
    head[u]=K;
}
void dfs(int now)
{
    if(head[now]==0)//叶子结点
        return ;
    int son=0;
    for(int i=head[now]; i!=0; i=edge[i].next)
    {
        int next=edge[i].to;
        if(head[next]==0)
            son++;
        dfs(next);
    }
    if(son<3)
    {
        flag=1;
    }
    return ;//非叶子节点
}
int main()
{
    int N;
    int x;
    while(cin>>N)
    {
        K=0;
        flag=0;
        memset(head, 0, sizeof(head));
        memset(edge, 0, sizeof(edge));
        for(int i=2; i<=N; i++)
        {
            cin>>x;
            addEdge(x, i);
        }
        dfs(1);
        if(flag==1)
        {
            cout<<"No"<<endl;
        }
        else
        {
            cout<<"Yes"<<endl;
        }
    }
    return 0;
}

C. Party Lemonade

  给n(1<=n<=30)个商店去买柠檬水,第i个商店一瓶有2^i-1升,价格cost[i],让求买L(1<=L<=1e9)升,每个商店有无限瓶,最少多少钱。

  典型的背包,完全背包,但是2^29怎么开数组背包?偏偏我最近dp优化看太多了非得觉得可以背,想把它给优化出来,结果昨天完全没有思路。今天看题解才知道是贪心,确实,30个商店而已,按单价排序,贪心就好了。后来同学又给说了另一种解法,在最开始预处理掉2^i升的最少钱,然后利用L的二进制加就好了,这个真的是神级操作,不过想想应该是能想到的才对,提上给的就是2^i这种,想二进制应该在情理之中。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
struct Node
{
    LL volume;
    LL cost;
    double price;
}node[50];
LL ans;
LL N, L;
int cmp(Node a, Node b)
{
    return a.price<b.price;
}
void getAns(LL V, LL index, LL sum)
{

   LL temp=V/node[index].volume;
   sum+=temp*node[index].cost;
   V=V%node[index].volume;
   if(V==0)
   {
       ans=min(ans, sum);
       return ;
   }
   if(index==N)
   {
       ans=min(ans, sum+node[index].cost);
       return ;
   }
   ans=min(ans, sum+node[index].cost);
   getAns(V, index+1, sum);
}
int main()
{
    while(cin>>N>>L)
    {
        LL temp=1;
        for(LL i=1; i<=N; i++)
        {
            cin>>node[i].cost;
            node[i].volume=temp;
            temp*=2;
            node[i].price=1.0*node[i].cost/node[i].volume;
        }
        sort(node+1, node+N+1, cmp);
        ans=1e18;
        getAns(L, 1, 0);
        cout<<ans<<endl;
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值