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;
}