Codeforces Round #202 (Div. 2) B,C,D,E
2 seconds
256 megabytes
standard input
standard output
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has.
Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.
Help Igor find the maximum number he can write on the fence.
The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105).
Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.
5
5 4 3 2 1 2 3 4 5
55555
2
9 11 1 12 5 8 9 10 6
33
0
1 1 1 1 1 1 1 1 1
-1
题意:
有v克油漆,可以写1~9九个数字,其中给出每个数字耗费的油漆量,问用这些油漆能写出的最大数字是多少。
代码:
//简单的贪心。显然能够写的数字位数越多就越大,其次是数字的字面值尽量大。
//最多能写多少位数字,找到最小的那个,求出剩余油漆,然后从大到小枚举数字,
//看看能否换上这个数字,能换几个,换了之后更新剩余油漆。
#include<bits/stdc++.h>
using namespace std;
const int inf=0x7fffffff;
int n,v,a[],ans[];
int main()
{
cin>>v;
int mini,minn=inf;
for(int i=;i<=;i++){
cin>>a[i];
if(a[i]<minn){
minn=a[i];
mini=i;
}
}
int maxn=v/a[mini];
int vv=v%a[mini];
queue<int>q;
for(int i=;i<=maxn;i++) q.push(mini);
if(maxn>){
for(int i=;i>=;i--){
int sum=vv,tmp=;
for(int j=;j<=maxn;j++){
if(q.front()>i) break;
sum+=a[q.front()];
if(sum<j*a[i]){
sum-=a[q.front()];
break;
}
q.pop();tmp=j;
}
if(tmp) vv=sum%a[i];
while(tmp--) q.push(i);
}
}
if(maxn==) cout<<"-1\n";
else{
int cnt=;
while(!q.empty()){
ans[cnt++]=q.front();q.pop();
}
sort(ans,ans+cnt);
for(int i=cnt-;i>=;i--) cout<<ans[i];
cout<<endl;
}
return ;
}
2 seconds
256 megabytes
standard input
standard output
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
3
3 2 2
4
4
2 2 2 2
3
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
题意:
有n个人玩游戏,每局游戏都要有一个人不能参与,给出每个人希望自己玩的次数,问满足所有人的情况下最少玩几局。
代码:
//设最少要玩x局。则x>=max(a[i])(1<=i<=n),(x-a[1])+(x-a[2])+...+(a-a[n])>=x;
//得到 x>=sum/(n-1).然后取max(x,max(a[i]));
#include<bits/stdc++.h>
using namespace std;
int n,a[];
int main()
{
scanf("%d",&n);
double sum=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
sort(a,a+n);
int x=ceil(sum/(double)(n-));
x=max(x,a[n-]);
printf("%d\n",x);
return ;
}
DFS
2 seconds
256 megabytes
standard input
standard output
You are given a rooted tree with n vertices. In each leaf vertex there's a single integer — the number of apples in this vertex.
The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf.
A tree is balanced if for every vertex v of the tree all its subtrees, corresponding to the children of vertex v, are of equal weight.
Count the minimum number of apples that you need to remove from the tree (specifically, from some of its leaves) in order to make the tree balanced. Notice that you can always achieve the goal by just removing all apples.
The first line contains integer n (2 ≤ n ≤ 105), showing the number of vertices in the tree. The next line contains n integers a1, a2, ..., an(0 ≤ ai ≤ 108), ai is the number of apples in the vertex number i. The number of apples in non-leaf vertices is guaranteed to be zero.
Then follow n - 1 lines, describing the tree edges. Each line contains a pair of integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi) — the vertices connected by an edge.
The vertices are indexed from 1 to n. Vertex 1 is the root.
Print a single integer — the minimum number of apples to remove in order to make the tree balanced.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier.
6
0 0 12 13 5 6
1 2
1 3
1 4
2 5
2 6
6
题意:
n个节点的树,每个节点有若干的苹果,要想使每个节点的子分支都有相同数量的苹果,最少要去掉多少苹果。
代码:
//假设根节点有三个儿子,每个儿子又分别有2,3,4个儿子,如果每个孙子节点上都有
//苹果那么三个儿子分别至少要分2,3,4个苹果才能够分给孙子,又要满足分支苹果相等的条件,
//可以取他们的最小公倍数,每个儿子分12个苹果(如果苹果数足够)。
//dfs计算每一层每个分支的苹果总数sum[i],每一个节点有多少个分支cnt[i],取cnt[i]的最小公
//倍数lcm,这一层每个分支应该拥有的苹果数m是取最小的一个sum[i]->m=sum[i]-sum[i]%lcm。
//即让m是lcm的倍数并且每个分支都至少能够提供m个苹果。最终递归到根节点就是满足条件的苹果数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[];
ll sum[],cnt[];
vector<int>g[];
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}
void dfs(int u,int fa)
{
int next=;
for(int i=;i<(int)g[u].size();i++){
int v=g[u][i];
if(v==fa) continue;
dfs(v,u);
if(!next){
sum[u]=sum[v];
cnt[u]=cnt[v];
}
else{
if(cnt[u]<5e13) cnt[u]=lcm(cnt[u],cnt[v]);
sum[u]=min(sum[u],sum[v])/cnt[u]*cnt[u];
}
next++;
}
if(!next){
sum[u]=a[u];cnt[u]=;
}
else{
sum[u]*=next;
if(cnt[u]<5e13) cnt[u]*=next;
}
}
int main()
{
scanf("%d",&n);
ll ans=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
ans+=a[i];
}
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(,);
printf("%I64d\n",ans-sum[]);
return ;
}
数据结构
3 seconds
256 megabytes
standard input
standard output
You are given an array a1, a2, ..., an and m sets S1, S2, ..., Sm of indices of elements of this array. Let's denote Sk = {Sk, i} (1 ≤ i ≤ |Sk|). In other words, Sk, i is some element from set Sk.
In this problem you have to answer q queries of the two types:
- Find the sum of elements with indices from set Sk: . The query format is "? k".
- Add number x to all elements at indices from set Sk: aSk, i is replaced by aSk, i + x for all i (1 ≤ i ≤ |Sk|). The query format is "+ k x".
After each first type query print the required sum.
The first line contains integers n, m, q (1 ≤ n, m, q ≤ 105). The second line contains n integers a1, a2, ..., an (|ai| ≤ 108) — elements of array a.
Each of the following m lines describes one set of indices. The k-th line first contains a positive integer, representing the number of elements in set (|Sk|), then follow |Sk| distinct integers Sk, 1, Sk, 2, ..., Sk, |Sk| (1 ≤ Sk, i ≤ n) — elements of set Sk.
The next q lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1 ≤ k ≤ m, |x| ≤ 108. The queries are given in order they need to be answered.
It is guaranteed that the sum of sizes of all sets Sk doesn't exceed 105.
After each first type query print the required sum on a single line.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
5 3 5
5 -5 5 1 -4
2 1 2
4 2 1 4 5
2 2 5
? 2
+ 3 4
? 1
+ 2 1
? 2
-3
4
9
题意:
大小为n的数组a[i], m个集合集合中的元素是数组a的下标,所有集合的元素的总数不大于n,有两种操作:? k 询问a数组下标在k集合中的a[i]的和,
+ k x表示a数组下标在k集合中的a[i]的值加x。
代码:
//普通暴力解法必然超时。把集合分为重集合和轻集合,元素个数大于sqrt(n)的集合是重集合,
//否则是轻集合。所有集合的元素总数不大于n,所以重集合个数不超过sqrt(n)个。
//算出每个重集合与其他集合有多少交集t(O(nsqrt(n)))。对于重集合先算出以其中的元素为下标的a[i]的总和sum,
//修改的重集合,直接记录该重集合中元素变化了多少tag,修该轻集合时直接修改a[i]的值然后更新
//每个重集合的sum+=两个集合的交集*变化值。
//询问轻集合是直接计算a[i]的和然后加上与他相交的重集合的t*tag。询问重集合时sum+=与他相交的
//重集合的t*tag。除了求交集外其他操作都是O(n),O(sqrt(n))。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
const int maxl=;
int n,m,q;
int id[maxn],t[maxl][maxn],vis[maxn];
ll a[maxn],sum[maxn],tag[maxl];
vector<int>s[maxn],big;
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=;i<=m;i++){
int k;
scanf("%d",&k);
s[i].resize(k);
for(int j=;j<k;j++) scanf("%d",&s[i][j]);
if(k>maxl){
id[i]=(int)big.size();
big.push_back(i);
for(int j=;j<k;j++) sum[id[i]]+=a[s[i][j]];
}
else id[i]=-;
}
memset(vis,,sizeof(vis));
memset(t,,sizeof(t));
memset(tag,,sizeof(tag));
for(int h=;h<(int)big.size();h++){
int i=big[h];
for(int j=;j<(int)s[i].size();j++)
vis[s[i][j]]=;
for(int k=;k<=n;k++){
for(int j=;j<(int)s[k].size();j++)
t[h][k]+=vis[s[k][j]];
}
for(int j=;j<(int)s[i].size();j++)
vis[s[i][j]]=;
}
char ch[];
int k,x;
while(q--){
scanf("%s%d",ch,&k);
if(ch[]=='?'){
ll res=;
if(id[k]<){
for(int j=;j<(int)s[k].size();j++)
res+=a[s[k][j]];
for(int i=;i<(int)big.size();i++)
res+=tag[i]*t[i][k];
}
else{
res=sum[id[k]];
for(int i=;i<(int)big.size();i++)
res+=tag[i]*t[i][k];
}
printf("%I64d\n",res);
}
else{
scanf("%d",&x);
if(id[k]<){
for(int j=;j<(int)s[k].size();j++)
a[s[k][j]]+=x;
for(int i=;i<(int)big.size();i++)
sum[i]+=t[i][k]*x;
}
else tag[id[k]]+=x;
}
}
return ;
}
Codeforces Round #202 (Div. 2) B,C,D,E的更多相关文章
- Codeforces Round #202 (Div. 2)
第一题水题但是wa了一发,排队记录下收到的25,50,100,看能不能找零,要注意100可以找25*3 复杂度O(n) 第二题贪心,先找出最小的花费,然后就能得出最长的位数,然后循环对每个位上的数看能 ...
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案
http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory limit p ...
- Codeforces Round #202 (Div. 1) D. Turtles DP
D. Turtles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/problem/B ...
- Codeforces Round #202 (Div. 2) A,B
A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #633 (Div. 2)
Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- Y460蓝牙键盘无法连接问题解决
mac坏了,无法启动,一直没时间去修理. 近期把大学的时候用的笔记本又翻了出来,小Y,经典的“娱乐本” Y460. Y460上之前被自己各种重装系统,反复从windows到双系统,再到linux之间来 ...
- 3D动态人脸识别技术分析——世纪晟人脸识别实现三维人脸建模
- 目录 - 国内3D动态人脸识别现状概况 - 新形势下人脸识别技术发展潜力 - 基于深度学习的3D动态人脸识别技术分析 1. 非线性数据建模方法 2. 基于3D变形模型的人脸建模 - 案例结合——世 ...
- 从oracle导入hive
sqoop import --connect jdbc:oracle:thin:@10.39.1.43:1521/rcrm --username bi_query --password ####### ...
- Python中的Comprehensions和Generations
Python中的Comprehensions和Generations语法都是用来迭代的.Comprehensions语法可用于list,set,dictionary上,而Generations语法分为 ...
- 团队选题报告(i know)
一.团队成员及分工 团队名称:I know 团队成员: 陈家权:选题报告word撰写 赖晓连:ppt制作,原型设计 雷晶:ppt制作,原型设计 林巧娜:原型设计,博客随笔撰写 庄加鑫:选题报告word ...
- PCA算法理解及代码实现
github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维 在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...
- C#控件DropDownList下拉列表默认打开
c#中的控件DropDownList要实现默打开确实不容易,之前也是想过页面上的点击之后就打开了,那直接模拟点击不就行了,试过后大失所望,根本没有效果. 于是网上找到了一个例子能实现IE浏览器下的打开 ...
- 201621044079 韩烨 week11-作业11-多线程
作业11-多线程 参考资料 多线程参考文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序 ...
- Zabbix监控配置
Zabbix在线文档 https://www.zabbix.com/documentation/4.0/zh/manual/config/hosts 1.我们启动服务后,我们看到了端口都正在监听,但是 ...
- C# 使用this的形参
示例1: public static RectangleF TransformRect(this Matrix mat, RectangleF rect) 是向Matrix类扩展带有Rectangle ...