Codeforces Round #326 (Div. 2) (588A,588B,587A)

1.Duff and Meat


题目链接:

http://codeforces.com/problemset/problem/588/A

解题思路:

Idea is a simple greedy, buy needed meat for i - th day when it's cheapest among days 1, 2, ..., n.

So, the pseudo code below will work:

ans = 0
price = infinity
for i = 1 to n
      price = min(price, p[i])
      ans += price * a[i]

Time complexity: 


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

int main(){
    int n,a,p;
    while(~scanf("%d",&n)){
        ll sum = 0;
        int minp = 200;
        for(int i = 0; i < n; i++){
            scanf("%d%d",&a,&p);
            if(p < minp)
                minp = p;
            sum += a*minp;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

2.Duff in Love

题目链接:

http://codeforces.com/problemset/problem/588/B

解题思路:

Find all prime divisors of n. Assume they are p1, p2, ..., pk (in ). If answer is a, then we know that for each 1 ≤ i ≤ k

obviously a is not divisible by pi2 (and all greater powers of pi). So a ≤ p1 × p2 × ... × pk. And we know that p1 × p2 × ... × pk is itself 

lovely. So,answer is p1 × p2 × ... × pk

Time complexity: 

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
int vis[1000005];
int prime[80000];
ll prime2[80000];
int nprime;

void getprime(){
    nprime = 0;
    memset(vis,0,sizeof(vis));
    for(int i = 2; i <= 1000000; i++){
        int t = 1000000/i;
        for(int j = 2; j <=t; j++)
            vis[i*j] = 1;
    }
    for(int i = 2; i <= 1000000; i++){
        if(!vis[i]){
            prime[nprime] = i;
            prime2[nprime++] = (ll)i*i;
        }
    }
}

int main(){
    getprime();
    //cout<<nprime<<endl;
    ll n;
    while(~scanf("%lld",&n)){
        for(int i = 0; i < nprime; i++){
            if(n % prime2[i] == 0){
                while(n % prime2[i] == 0)
                    n /= prime[i];
            }
        }
        printf("%lld\n",n);
    }
    return 0;
}

3.Duff in Beach


题目链接:

http://codeforces.com/problemset/problem/587/A

解题思路:

Problem is: you have to find the minimum number of k, such there is a sequence a1, a2, ..., ak with 

condition2a1 + 2a2 + ... + 2ak = S = 2w1 + 2w2 + ... + 2w2. Obviously, minimum value of k is the number of set bits in binary r

epresentation of S(proof is easy, you can prove it as a practice :P).

Our only problem is how to count the number of set bits in binary representation of S? Building the binary representation of S as an 

array in is easy:

算法思想:

因为后来题意有更正,更改为:

Duff may throw out an arbitrary subsequence of weights, not necessarily contiguous. The weights aren't necessarily sorted in the input.

使得题目变简单了。因为不需要连续,所以只要记录一下每个数的次数,判断奇偶即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int vis[1000005];

int main(){
    int n;
    while(~scanf("%d",&n)){
        memset(vis,0,sizeof(vis));
        int x;
        for(int i = 0; i < n; i++){
            scanf("%d",&x);
            vis[x]++;
        }
        int sum = 0;
        for(int i = 0; i <= 1000000; i++){
            if(vis[i]){
                if(vis[i] % 2)
                    sum++;
                vis[i+1] += vis[i] / 2;
            }
        }
        int t = vis[1000001];
        while(t){
            if(t&1)
                sum++;
            t >>= 1;
        }
        printf("%d\n",sum);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值