Codeforces Round #318(Div 1) 573A, 573B,573C

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

这场的前两题完全是手速题。。。A题写了7分钟,交的时候已经500+了,好在B题出的速度勉强凑活吧,and C题也没有FST

A. Bear and Poker

Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.

Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?

Input

First line of input contains an integer n (2 ≤ n ≤ 105), the number of players.

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — the bids of players.

Output

Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise.

Sample test(s)

input

475 150 75 50

output

Yes

input

3100 150 250

output

No

Note

In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.

It can be shown that in the second sample test there is no way to make all bids equal.

A题没啥好说的,把因为可以乘2或乘三,那只要看看除了2和3之外,其他的因子是否相同即可

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define mp(X,Y) make_pair(X,Y)
30 #define pb(X) push_back(X)
31 #define rep(X,N) for(int X=0;X<N;X++)
32 #define rep2(X,L,R) for(int X=L;X<=R;X++)
33 #define dep(X,R,L) for(int X=R;X>=L;X--)
34 #define clr(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 int a[100010];
41 int main()
42 {
43     ios::sync_with_stdio(false);
44     int n;
45     cin>>n;
46     rep(i,n){
47         cin>>a[i];
48         while(a[i]%2==0)a[i]/=2;
49         while(a[i]%3==0)a[i]/=3;
50     }
51     int ff = 0;
52     rep(i,n-1)if(a[i]!=a[i+1])ff = 1;
53     if(ff)cout<<"No"<<endl;
54     else cout<<"Yes"<<endl;
55     return 0;
56 }

代码君

B. Bear and Blocks

Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.

Limak will repeat the following operation till everything is destroyed.

Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.

Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.

Input

The first line contains single integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers.

Output

Print the number of operations needed to destroy all towers.

Sample test(s)

input

62 1 4 6 2 2

output

3

input

73 3 3 1 3 3 3

output

2

Note

The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.

After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.

同样很水,从左到右以及从右到左各扫一遍即可

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define mp(X,Y) make_pair(X,Y)
30 #define pb(X) push_back(X)
31 #define rep(X,N) for(int X=0;X<N;X++)
32 #define rep2(X,L,R) for(int X=L;X<=R;X++)
33 #define dep(X,R,L) for(int X=R;X>=L;X--)
34 #define clr(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 int h[100010];
41 int dp[100010];
42 int dp2[100010];
43 int main()
44 {
45     ios::sync_with_stdio(false);
46     int n;
47     cin>>n;
48     rep(i,n)cin>>h[i];
49     int maxx = 0;
50     dp[0] = dp2[n-1] = 1;
51     rep2(i,1,n-1)dp[i] = min(h[i],dp[i-1]+1);
52     dep(i,n-2,0)dp2[i] = min(h[i],dp2[i+1]+1);
53     rep(i,n)dp[i] = min(dp[i],dp2[i]);
54     rep(i,n)maxx = max(dp[i],maxx);
55     cout<<maxx<<endl;
56     return 0;
57 }

代码君

C. Bear and Drawing

Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree.

Recall that tree is a connected graph consisting of n vertices and n - 1 edges.

Limak chose a tree with n vertices. He has infinite strip of paper with two parallel rows of dots. Little bear wants to assign vertices of a tree to some n distinct dots on a paper so that edges would intersect only at their endpoints — drawn tree must be planar. Below you can see one of correct drawings for the first sample test.

Is it possible for Limak to draw chosen tree?

Input

The first line contains single integer n (1 ≤ n ≤ 105).

Next n - 1 lines contain description of a tree. i-th of them contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) denoting an edge between vertices ai and bi. It‘s guaranteed that given description forms a tree.

Output

Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes).

Sample test(s)

input

81 21 31 66 46 76 57 8

output

Yes

input

131 21 31 42 52 62 73 83 93 104 114 124 13

output

No

一开始少考虑了一些,然后只是判断儿子不符合的情况,然后还有一些漏考虑的细节。

统计有多少个一定要占一半边的子树,如果超过两个,那就是不符合的。

树形dp,第一次dfs统计好所有子树,第二遍,把根的信息传下来一起考虑就ok了。

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define mp(X,Y) make_pair(X,Y)
30 #define pb(X) push_back(X)
31 #define rep(X,N) for(int X=0;X<N;X++)
32 #define rep2(X,L,R) for(int X=L;X<=R;X++)
33 #define dep(X,R,L) for(int X=R;X>=L;X--)
34 #define clr(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 vector<int>G[100010];
41 int dp[100010];
42 int ff = 0;
43 void dfs(int u,int fa){
44     int num = 0;
45     int sz = G[u].size();
46     rep(i,sz){
47         int v = G[u][i];
48         if(v == fa)continue;
49         dfs(v,u);
50         dp[u] += dp[v];
51         num++;
52     }
53     if(!dp[u])dp[u] = 1;
54     if(dp[u] == 2 && num == 1)dp[u]++;
55 }
56 void dfs2(int u,int fa,int num){
57     int sz = G[u].size();
58     int n = 0;
59     if(num>2)n++;
60     rep(i,sz){
61         int v = G[u][i];
62         if(v == fa)continue;
63         int tmp = 1;
64         tmp = max(num+dp[u]-dp[v],tmp);
65         if(tmp == 2 && num == 2)tmp = 3;
66         dfs2(v,u,tmp);
67         if(dp[v]>2)n++;
68     }
69     if(n>2)ff = 1;
70 }
71
72 int main()
73 {
74     ios::sync_with_stdio(false);
75     int n;
76     cin>>n;
77     int u,v;
78     rep(i,n-1){
79         cin>>u>>v;
80         u--;v--;
81         G[u].pb(v);
82         G[v].pb(u);
83     }
84     dfs(0,-1);
85     dfs2(0,-1,0);
86
87
88     if(ff)cout<<"No"<<endl;
89     else cout<<"Yes"<<endl;
90
91
92     return 0;
93 }
时间: 2024-10-12 18:55:26

Codeforces Round #318(Div 1) 573A, 573B,573C的相关文章

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces Round #318 (Div. 2) A、B、C

574A - Bear and Elections 题意: 输入一个数字n,接着输入n个正整数. 题目要求第一个整数要大于其余整数,其余整数每次可以减小1并增加到第一个数字中. 问至少要多少次才能满足要求. 思路: 用优先队列维护一下就可以了. #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <queue> using

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

不要想多了直接暴.对于u枚举a和b,判断一个是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; const int maxn = 4001; #define PB push_back vector<int> G[maxn]; bool g[maxn][maxn]; int deg[maxn]; const int INF = 0x3f3f3f3f; int main() { //freopen("

Codeforces Round #318 (Div. 2) A Bear and Elections

优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main() { int n; scanf("%d",&n); int t; scanf("%d",&t); for(int i = 2; i <= n; i++){ int v; scanf("%d",&v); q.push(v

Codeforces Round #318 (Div. 2) C Bear and Poker

很简单,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll a[maxn]; int main() { //freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(i

Codeforces Round #318 (Div. 2) D Bear and Blocks

不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k == min(hi-j+j,hi+j+j)时hi会变成0,因为min具有传递性,那么可以左右分开来考虑,hi-j+j化一下就是hi-j-(i-j)+i,其中后面i和j无关,所以只要找出前面最小的hi-i 对于右边的类似处理,最后再扫一遍,得出最大的一个k就是答案. #include<bits/std

Codeforces Round #318 (Div. 2)

<pre name="code" class="cpp"> 思路:最暴力的想法,用一个三重循环去遍历,这道题给出的时间是2秒,而且cf的服务器肯定很好,所以时间上应该是没有压力的,但我心里一直没底,总感觉会超时. 在这里,看了大牛们的代码,学了一个新思路,对于一个三重循环,可以找出他们两两之间有什么关系,用pair,或strcut保存下来,题目里的m是4000,远远小于n,这样会使时间压力减少很多, 62ms #include<bits/stdc+

Codeforces Round #318(Div. 2)(A,B,C,D)

A题: 题目地址:Bear and Elections 题意:最少变换多少次可以使得第一个数字大于后面所有数字. 思路:把后面n-1个数排序,让第一个和最后一个数比较,然后增减.知道第一个数大于最后一个数为止 #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream>

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/