Hello 2018(B、C)

B. Christmas Spruce
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.

Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.

The definition of a rooted tree can be found here.

Input

The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).

Vertex 1 is the root. It's guaranteed that the root has at least 2 children.

Output

Print "Yes" if the tree is a spruce and "No" otherwise.

Examples
input
4
1
1
1
output
Yes
input
7
1
1
1
2
2
2
output
No
input
8
1
1
1
1
3
3
3
output
Yes
Note

The first example:

The second example:

It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.

The third example:


代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
#include<string>
#define ll long long 
using namespace std;
bool vis[1005];
vector<int>v[1005];
int main(){
	int n;
	int i;
	int x;
	cin>>n;
	memset(vis,0,sizeof(vis));
	bool flag=true;
	for(i=2;i<=n;i++){
			cin>>x;
			v[x].push_back(i);
			vis[x]=1;
		}
		for(i=1;i<1005;i++){
			if(vis[i]==1){ 
			int len=v[i].size();
			for(int k=0;k<v[i].size();k++){
				if(vis[v[i][k]]==1) len--;
			}
			if(len<3) {
			flag=false;
			break;
			}
			}
		}
      	if(flag) cout<<"Yes"<<endl;
		else cout<<"No"<<endl; 
	return 0;
} 





C. Party Lemonade
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.

Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.

You want to buy at least L liters of lemonade. How many roubles do you have to spend?

Input

The first line contains two integers n and L (1 ≤ n ≤ 301 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.

Output

Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.

Examples
input
4 12
20 30 70 90
output
150
input
4 3
10000 1000 100 10
output
10
input
4 3
10 100 1000 10000
output
30
input
5 787787787
123456789 234567890 345678901 456789012 987654321
output
44981600785557577
Note

In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.

In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.

In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.



题意:

有n家店铺,每个店铺有一种商品(无限个),每种商品有体积(2的i-1次方),每种商品有价格c[i],问至少买l体积的物品最少需要花费多少钱?

思路:

看到题目第一反应是完全背包,可是仔细想想之后,发现背包公式并不适用。后来想到了贪心,时间太晚,逻辑混乱陆,没AC。今早上又补了补,大概就是:按照单位毫升的价格(c[i]/2i1)降序来排列,首先第一家一定是最划算的,然后看在第一家购买剩余多少,继而判断是继续在当前家买便宜还是到另一家去买便宜,进行一个判断即可

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
#include<string>
#define ll long long 
#define  maxn 30
using namespace std;
//结构体,并重载<运算符 
struct Node{
	ll vol; //体积 
	ll cost;//花费 
	bool operator<(Node a){//重载<运算符 
		return (a.cost*vol>cost*a.vol);
	}
}s[maxn+5];
ll n,l; 
//left为剩余的体积,cost为当前的花费,num为到了第几家 
ll dfs(ll left,ll cost,ll num){  
	ll count=left/s[num].vol; //判断当前家需要购买几瓶 
	cost+=count*s[num].cost;//加上花费 
	left-=count*s[num].vol;//计算剩余体积 
	if(left==0) return cost; //如果不剩余了,则方案最优,直接返回 
	//if(num==n) return cost+s[num].cost;
	return min(cost+s[num].cost,dfs(left,cost,num+1));//处理剩余部分,再买一杯超出需要的量或者去下一家店,两者去最少花费的那种。 
}
int main(){
	s[1].vol=1;
	cin>>n>>l;
	cin>>s[1].cost;
	for(int i=2;i<=n;i++){
		cin>>s[i].cost;
		s[i].vol=2*s[i-1].vol;
	}
	sort(s+1,s+1+n);
	
//	ll tempm=0;
//	ll left=1;
//	ll temp;
	cout<<dfs(l,0,1)<<endl; 
	return 0;
} 
考虑二进制,因为每个物品的体积都是2的多少次方,且前面的体积一定是后面的体积的1/2,然后如果当前的可以被前面的替代,那么就考虑替换下来,然后判断每一位i是否与l符合
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,l,ans;
ll a[34];
int main()
{
    ans=0;
    scanf("%lld%lld",&n,&l);
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        
        if(i!=0&&a[i]>2*a[i-1])
        
            a[i]=2*a[i-1];
    }
    
    
    for(int i=n;i<32;i++)
        a[i]=2*a[i-1];
        
    for(int i=0;i<32;i++)
    {
        if(ans>a[i]) ans=a[i];
        if(l&(1<<i)) ans+=a[i];
    }
    
    printf("%lld\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值