贪心
B. Color the Fence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105).

Output

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.

Examples
input
5
5 4 3 2 1 2 3 4 5
output
55555
input
2
9 11 1 12 5 8 9 10 6
output
33
input
0
1 1 1 1 1 1 1 1 1
output
-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 ;
}
 
数学
C. Mafia
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

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.

Examples
input
3
3 2 2
output
4
input
4
2 2 2 2
output
3
Note

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

D. Apple Tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
6
0 0 12 13 5 6
1 2
1 3
1 4
2 5
2 6
output
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 ;
}

数据结构

E. Subset Sums
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. Find the sum of elements with indices from set Sk. The query format is "? k".
  2. Add number x to all elements at indices from set SkaSk, 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.

Input

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.

Output

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.

Examples
input
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
output
-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的更多相关文章

  1. Codeforces Round #202 (Div. 2)

    第一题水题但是wa了一发,排队记录下收到的25,50,100,看能不能找零,要注意100可以找25*3 复杂度O(n) 第二题贪心,先找出最小的花费,然后就能得出最长的位数,然后循环对每个位上的数看能 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #633 (Div. 2)

    Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...

  7. 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 ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. 小米 OJ 编程比赛 02 月常规赛

    Carryon 数数字 描述 Carryon 最近迷上了数数字,然后 Starry 给了他一个区间[l,r] ,然后提了几个要求, 需要将 ll 到 rr 之间的数全部转化成 16 进制,然后连起来. ...

  2. Kali渗透测试工具-nslookup

    1.交互模式 终端输入nslookup进入交互模式 (1)查询A地址记录(默认) set q=a A记录简单理解将域名转换成对应的IP地址 (2)查询mail exchanger set q=mx m ...

  3. php 安全方面面试题

    1 MySQL数据库作发布系统的存储,一天五万条以上的增量,预计运维三年,怎么优化? a. 设计良好的数据库结构,允许部分数据冗余,尽量避免join查询,提高效率.b. 选择合适的表字段数据类型和存储 ...

  4. 你真的了解JAVA里的String么

    Java中String类细节问题 (考察点Java内存分配问题) 1. String str1 = "abc";   System.out.println(str1 == &quo ...

  5. 关于ES6-{块级作用域 let const 解构赋值 数组 字符串 函数的扩展 箭头函数}

    关于ES6 块级作用域 任何一对花括号({})中的语句集都属于一个块,在块中声明的变量在代码块外都是不可访问的,称之为块级作用域,ES5以前没有块级作用域 let let 是ES6新增的声明变量的一种 ...

  6. 【转】自定义(滑动条)input[type="range"]样式

    1.如何使用滑动条? 用法很简单,如下所示: <input type="range" value="0"> 各浏览器原始样式如下: Chrome:  ...

  7. lintcode-161-旋转图像

    161-旋转图像 给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像. 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成? 标 ...

  8. iOS- <项目笔记>项目配置常见文件

    项目常见文件 1.main.m * 里面有一个程序的入口:main函数 2.Prefix.pch文件 * pch文件中的内容能被项目中的其他任何文件共享\包含\访问 * 如果定义的内容只用在OC环境中 ...

  9. 【week3】词频统计 单元测试

    使用Eclipse 集成的Junit进行单元测试.单元测试的核心包括断言.注解. 测试代码如下: @BeforeClass // 针对所有测试,只执行一次,且必须为static void public ...

  10. LR脚本编写时的几个小技巧

    参数化空值 如上图所示,当参数化时某个值需要为空值(非空格),直接在参数化文件中空一行/格即可,虽然Parameter List界面上没有显示空的那一行,但并不影响取值. 手工日志跟踪 lr_set_ ...