无奈只能打打虚赛...

一共写了4道题,但是d没在规定时间调出来。

A. Chess For Three
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three.

So they play with each other according to following rules:

  • Alex and Bob play the first game, and Carl is spectating;
  • When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner.

Alex, Bob and Carl play in such a way that there are no draws.

Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it!

Input

The first line contains one integer n (1 ≤ n ≤ 100) — the number of games Alex, Bob and Carl played.

Then n lines follow, describing the game log. i-th line contains one integer ai (1 ≤ ai ≤ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game.

Output

Print YES if the situation described in the log was possible. Otherwise print NO.

Examples
input
3112

output
YES

input
212

output
NO

Note

In the first example the possible situation is:

  1. Alex wins, Carl starts playing instead of Bob;
  2. Alex wins, Bob replaces Carl;
  3. Bob wins.

The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one.

大意:一共有三个人在玩游戏,1号和2号先玩,3号看着,输的那个和看的那个换。然后给一个序列表示每一局赢的人是谁,问合不合法。

解:模拟

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,a[3];
 4 int main()
 5 {
 6     scanf("%d",&n);
 7     a[0]=1;a[1]=2;a[2]=3;
 8     for(int i=1;i<=n;i++){
 9         int x;
10         scanf("%d",&x);
11         if(x!=a[0]&&x!=a[1]){
12             printf("NO\n");
13             return 0;
14         }
15         if(a[0]==x)swap(a[1],a[2]);
16         else swap(a[0],a[2]);
17     }
18     printf("YES\n");
19     return 0;
20 }

代码

B. Beautiful Divisors
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautiful iff its binary representation consists of k + 1 consecutive ones, and then k consecutive zeroes.

Some examples of beautiful numbers:

  • 12 (110);
  • 1102 (610);
  • 11110002 (12010);
  • 1111100002 (49610).

More formally, the number is beautiful iff there exists some positive integer k such that the number is equal to (2k - 1) * (2k - 1).

Luba has got an integer number n, and she wants to find its greatest beautiful divisor. Help her to find it!

Input

The only line of input contains one number n (1 ≤ n ≤ 105) — the number Luba has got.

Output

Output one number — the greatest beautiful divisor of Luba's number. It is obvious that the answer always exists.

Examples
input
3

output
1

input
992

output
496

大意:定义一个“美丽数”表示二进制下可以表示成连续k+1个1和连续k个0,然后对于每一个n求出他因子里面的美丽数最大是多少,因为1是美丽数,所以一定有解。

解:题目中给了美丽数满足的公式,预处理出来依次枚举就行

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int p[11],n,a[11];
 4 int main()
 5 {
 6     p[0]=1;
 7     for(int i=1;i<=10;i++)p[i]=p[i-1]<<1;
 8     for(int i=1;i<=10;i++)a[i]=(p[i]-1)*p[i-1];
 9     scanf("%d",&n);
10     for(int i=10;i>=1;i--){
11         if(n%a[i]==0){
12             printf("%d\n",a[i]);
13             return 0;
14         }
15     }
16     return 0;
17 }

代码

C. Rumor
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.

Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.

Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.

The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?

Take a look at the notes if you think you haven't understood the problem completely.

Input

The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.

The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.

Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ nxi ≠ yi). It is guaranteed that each pair is listed at most once.

Output

Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

Examples
input
5 22 5 3 4 81 44 5

output
10

input
10 01 2 3 4 5 6 7 8 9 10

output
55

input
10 51 6 2 7 3 8 4 9 5 101 23 45 67 89 10

output
15

Note

In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.

In the second example Vova has to bribe everyone.

In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

大意:你可以收买每一个人,收买他之后相当于收买了他所有的朋友(双向的),问最少多少钱可以收买到所有人。

解:枚举每一个未被收买的人,dfs他所有的朋友,在这些人中取一个最便宜的累加即可。

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int inf=1e5+10;
 5 int n,m,fi[inf],ne[inf<<1],to[inf<<1],tot,w[inf],Min;
 6 ll ans;
 7 bool vis[inf];
 8 void edge_add(int x,int y){
 9     ne[++tot]=fi[x];
10     to[tot]=y;
11     fi[x]=tot;
12 }
13 void dfs(int x){
14     vis[x]=1;
15     Min=min(Min,w[x]);
16     for(int i=fi[x];i;i=ne[i]){
17         if(vis[to[i]])continue;
18         dfs(to[i]);
19     }
20 }
21 int main()
22 {
23     scanf("%d%d",&n,&m);
24     for(int i=1;i<=n;i++)scanf("%d",&w[i]);
25     for(int i=1;i<=m;i++){
26         int x,y;
27         scanf("%d%d",&x,&y);
28         edge_add(x,y);
29         edge_add(y,x);
30     }
31     for(int i=1;i<=n;i++){
32         if(vis[i])continue;
33         Min=0x7fffffff;
34         dfs(i);
35         ans+=Min;
36     }
37     printf("%lld\n",ans);
38     return 0;
39 }

代码

D. Credit Card
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card.

She starts with 0 money on her account.

In the evening of i-th day a transaction ai occurs. If ai > 0, then ai bourles are deposited to Luba's account. If ai < 0, then ai bourles are withdrawn. And if ai = 0, then the amount of money on Luba's account is checked.

In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d.

It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1».

Luba must not exceed this limit, and also she wants that every day her account is checked (the days when ai = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her!

Input

The first line contains two integers nd (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) —the number of days and the money limitation.

The second line contains n integer numbers a1, a2, ... an ( - 104 ≤ ai ≤ 104), where ai represents the transaction in i-th day.

Output

Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.

Examples
input
5 10-1 5 0 -5 3

output
0

input
3 4-10 0 20

output
-1

input
5 10-5 0 10 -11 0

output
2

大意,每天晚上一个数值a,如果a>0表示今天收入a元,<0表示亏损a元,=0表示去检验自己的卡。每天早上(在前者之前)可以去充卡,可以随便充,但是充完之后不能大于上限d。每一次去查卡的时候

卡上的钱不能是个负数,任何时刻如果卡上的钱大于d了就视为不合法,直接输出-1。问最少充几次卡可以保证n天完全合法。

解:

首先可以发现一个很显然的性质,我们每一次充卡一定是在查卡的时候去充,因为其他时间出现负数是没有关系的,但是却不能超过d,所以只在查卡时充保证不会更差。

其次我们可以发现每一次充卡要尽可能多充,来减少后面冲的次数,但是又得保证当前充的这么多钱不会使得后面的某一天超过了d,也就是说要在允许的范围内多取,那么我们

可以求出这个最大值,如果有哪一天的最大值是个负数,意味着无论怎么弄,最终总会超过d元,即-1的情况。

那么我们就可以从前往后模拟了,一旦出现负数就ans++,然后变成当前那个位置允许的最大值。

考虑如何求,我们可以从后往前倒着推,第i个位置的max就是第i+1个的max和保证第i到第i+1这个过程出现的最大值不超过d所允许的max取一个min即可,求出的max是可能大于d的,这时需要变成d。

至于边界问题,我们可以把最后一次查的位置拿出来单独算一下,保证最后一次查卡的后面怎么加也不会大于d。

但这样做无法保证第一次查卡前的情况合法,所以还需要在判断过程中判断当前卡上的钱是不是已经大于d,一旦出现即为-1。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf=1e5+10;
 4 int n,a[inf],s[inf],tot,d,ans,hs[inf];
 5 struct ghb{
 6     int seat,ma,ans,Ma;
 7 }p[inf];
 8 int main()
 9 {
10 //    freopen("1.txt","r",stdin);
11     scanf("%d%d",&n,&d);
12     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
13     int now=0,Max=-0x7fffffff;
14     for(int i=1;i<=n;i++){
15         now+=a[i];
16         Max=max(Max,now);
17         if(a[i]==0){
18             p[++tot].ans=now;
19             p[tot].seat=i;
20             hs[i]=tot;
21             p[tot].Ma=Max;
22             Max=-0x7fffffff;
23             now=0;
24         }
25     }
26     int tem1=0,tem2=-0x7fffffff;
27     for(int i=p[tot].seat;i<=n;i++){
28         tem1+=a[i];
29         tem2=max(tem2,tem1);
30     }
31     p[tot].ma=min(d-tem2,d);
32     if(p[tot].ma<0){
33         printf("-1\n");
34         return 0;
35     }
36     for(int i=tot-1;i>=1;i--){
37         p[i].ma=min(d-p[i+1].Ma,p[i+1].ma-p[i+1].ans);
38         p[i].ma=min(p[i].ma,d);
39         if(p[i].ma<0){
40             printf("-1\n");
41             return 0;
42         }
43     }
44     now=0;
45     for(int i=1;i<=n;i++){
46         now+=a[i];
47         if(now>d){
48             printf("-1\n");
49             return 0;
50         }
51         if(a[i]==0){
52             if(now<0){
53                 now=p[hs[i]].ma;
54                 ans++;
55             }
56         }
57     }
58     printf("%d\n",ans);
59     return 0;
60 }

代码

转载于:https://www.cnblogs.com/hyghb/p/7896423.html

Educational Codeforces Round 33 (Rated for Div. 2)相关推荐

  1. Educational Codeforces Round 33 (Rated for Div. 2)C.Rumor并查集

    Educational Codeforces Round 33 (Rated for Div. 2)C.Rumor并查集 题意:首先用并查集把N个人分成几块,然后每个块当中选取一个最小权值 加到答案中 ...

  2. Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays

    题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi​的的幂为kkk,则这个 ...

  3. Educational Codeforces Round 33 (Rated for Div. 2) B题

    B. Beautiful Divisors Recently Luba learned about a special kind of numbers that she calls beautiful ...

  4. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  5. Educational Codeforces Round 100 (Rated for Div. 2)

    文章目录 Educational Codeforces Round 100 (Rated for Div. 2) A. Dungeon B. Find The Array C. Busy Robot ...

  6. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  7. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  8. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

  9. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

最新文章

  1. 滴滴千万级ElasticSearch平台发展之路!
  2. memcache及其telnet命令使用详解
  3. SharePoint 2013 REST 服务使用简介
  4. 笔记-信息化与系统集成技术-云计算的服务形式(IaaS/PaaS/SaaS/DaaS)
  5. OpenShift 4 之 GitOps(6)用ArgoCD部署MongoDB主从集群
  6. myEclipse的subversion插件Subclipse
  7. 反编译OD工具OllyDBG 2.0.1下载
  8. quartus仿真34:74161构成长度为10的序列发生器
  9. php sns 源码,ThinkSNS V2.3源码下载(SNS系统)
  10. 常用的国产计算机软件,国产常用操作系统介绍,亲身使用告诉你答案!
  11. 功能测试转测试开发的正确姿势
  12. Android开发--WIFI输入密码Dialog的实现
  13. YDOOK:PyDraw 所见即所得 Python GUI 绘制框架 编程源自 JY Lin
  14. Cesium本地加载地形(dem高程)数据
  15. 接口练习--设计一个辅助英雄 接口实现技能治疗
  16. 非参数估计—Parzen窗与K-nearest-neighbor
  17. c语言程序编程线性方程,C语言编程求解线性方程.doc
  18. 三年级信息技术用计算机打字教案,信息技术教案三年级(下册)
  19. Android数据持久化技术
  20. 综合布线包括计算机网络,综合布线系统包括哪7个子系统?

热门文章

  1. vue中tab切换前端实现_使用vue实现tab操作
  2. ${}什么意思在web开发中
  3. ida 调试android之路
  4. 软件测试基础知识整理,都给你准备好了
  5. mysql fetchall获取不到数据_解决pymysql cursor.fetchall() 获取不到数据的问题
  6. Jest测试框架学习(一)
  7. 如何卸载极品五笔(奥运版)
  8. DeepID1 DeepID2 DeepID2+ DeepID3
  9. pes2019手游服务器维护,【实况手游】解读PES2019手游未来的更新内容!
  10. 如何更准确的预估开发时间?