A. Alyona and copybooks
题意
Alyona已有n支笔,现在想再买k支笔使满足(n+k) %4 == 0;
已知买一支装的笔需a元,两支装的笔为b元,三支装的笔为c元,问最少花多少钱买笔可满足条件
分析
假设买一支装、两支装、三支装的笔各A,B,C。
- 若n%4 == 0,则最少花费为0;
- 若n%4 == 1,则应额外买 3,7,11,… ,最少花费为min{C,3A,A+B};
- 若n%4 == 2,则应额外买 2,6,10,… ,最少花费为min{B,2A,2C};
- 若n%4 == 3,则应额外买 1,5,9,… ,最少花费为min{A,3C,B+C};
代码
#include<bits/stdc++.h>
using namespace std;
int main() {
long long n,a,b,c;
scanf("%I64d %I64d %I64d %I64d",&n,&a,&b,&c);
c = min(a*3,c); c = min(a+b,c);
b = min(a*2,b); b = min(c*2,b);
a = min(c*3,a); a = min(b+c,a);
n = n%4;
if(n == 0) printf("0\n");
else if(n == 3) printf("%I64d\n",a);
else if(n == 2) printf("%I64d\n",b);
else if(n == 1) printf("%I64d\n",c);
}
B. Alyona and flowers
题意
n个数字组成的数组,有m个子数组存在。
问m个子数组中元素和最大的一个。
分析
预处理数组前缀和。
#include<bits/stdc++.h>
using namespace std;
int pre[110];
int main() {
int n,m;
scanf("%d %d",&n,&m);
for(int i=1,x;i<=n;i++)
scanf("%d",&x), pre[i] = pre[i-1] + x;
int ans = 0;
for(int i=0,l,r;i<m;i++) {
scanf("%d %d",&l,&r);
if(pre[r] - pre[l-1] > 0)
ans += pre[r] - pre[l-1];
}
printf("%d\n",ans);
}
C. Alyona and mex
题意
mex —— 在数组的一段子序列[l,r]中,最小的未出现在该序列中的非负整数。
现在求满足对于m对子序列[l,r]的 mexi , 使得 min{mex1,mex2,…,mexm} 最大的一个数组。
分析
求任意一个满足上述条件的数组。
对于可以获得的 min{mex1,mex2,…,mexm} 的最大值,很容易想到是m对子序列[l,r]的最小的区间长度,即 minlen=min{(r1−l1+1),…,(rm−lm+1)}
故对于构造此数组,只需要将0到minlen-1的数循环输出n个即可。此处不给证明
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
scanf("%d %d",&n,&m);
int ans = 1<<30;
for(int i=0,l,r;i<m;i++)
scanf("%d %d",&l,&r), ans = min(ans,r-l+1);
printf("%d\n",ans);
for(int i=0;i<n;i++)
printf("%d ",i%ans);
}
D. Alyona and a tree
Description
Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai . Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).
Let’s define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.
The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au .
Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v*controls *u.
Input
The first line contains single integer n ( 1 ≤ n ≤ 2⋅105 ).
The second line contains n integers a1,a2,…,an(1≤ai≤109) — the integers written in the vertices.
The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1) .
It is guaranteed that the given graph is a tree.
Output
Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
题意
求对于每个节点p,其满足 a[v]≥dist(p,v) 的后续节点v的个数。
分析
用深搜从根开始向叶节点遍历,记录路径中 path[v] 表示从根节点到节点v的边权和。
dist(p,v)=path[v]−path[p] ;
a[v]≥dist(p,v) ;
结合两式: path[v]−a[v]≤path[p]
即节点v能够覆盖在路径上的满足 path[v]−a[v]≤path[p] 的所有点p。由于 path[p] 必然是递增的,二分确定区间覆盖。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn],id[maxn],mark[maxn],ans[maxn];
long long path[maxn];
vector<pair<int,long long> > g[maxn];
long long dfs(int root,int dep,long long dist){
long long ret = 0;
path[dep] = dist; id[dep] = root;
for(int i=0;i<g[root].size();i++)
ret += dfs(g[root][i].first,dep+1,dist+g[root][i].second);
int pos = lower_bound(path,path+dep,dist - a[root]) - path;
mark[root]++, mark[id[pos]]--;
ans[root] = ret;
return ret + mark[root];
}
int main(){
int n; scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=2,p,w;i<=n;i++)
scanf("%d %d",&p,&w), g[p].push_back(make_pair(i,w));
dfs(1,0,0);
for(int i=1;i<=n;i++)
printf("%d ",ans[i]);
}
E题待补
http://blog.youkuaiyun.com/dormousenone/article/details/53363374