CodeForces 884A.B.C Book Reading(水) | Japanese Crosswords Strike Back(水) | Bertown Subway(DFS)

Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.

But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.

Help Luba to determine the minimum number of day when she finishes reading.

It is guaranteed that the answer doesn‘t exceed n.

Remember that there are 86400 seconds in a day.

Input
The first line contains two integers n and t (1?≤?n?≤?100, 1?≤?t?≤?106) — the number of days and the time required to read the book.

The second line contains n integers ai (0?≤?ai?≤?86400) — the time Luba has to spend on her work during i-th day.

Output
Print the minimum day Luba can finish reading the book.

It is guaranteed that answer doesn‘t exceed n.

Example
Input
2 2
86400 86398
Output
2
Input
2 86400
0 86400
Output
1

题意:

给出天数n,和看完一本书需要的时间,接下来的N个数代表那一天能用来看书的时间,问最少需要多少天能把书看完。

题解:

水题。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main()
{
    int n,x;
    while(cin>>n>>x)
    {
        int sum=0;
        for(int i=0;i<n;i++)
            cin>>a[i],sum+=a[i];
        if(sum+n-1==x)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

CodeForces 884B Japanese Crosswords Strike Back

A one-dimensional Japanese crossword can be represented as a binary string of length x. An encoding of this crossword is an array a of size n, where n is the number of segments formed completely of 1‘s, and ai is the length of i-th segment. No two segments touch or intersect.

For example:

If x?=?6 and the crossword is 111011, then its encoding is an array {3,?2};
If x?=?8 and the crossword is 01101010, then its encoding is an array {2,?1,?1};
If x?=?5 and the crossword is 11111, then its encoding is an array {5};
If x?=?5 and the crossword is 00000, then its encoding is an empty array.
Mishka wants to create a new one-dimensional Japanese crossword. He has already picked the length and the encoding for this crossword. And now he needs to check if there is exactly one crossword such that its length and encoding are equal to the length and encoding he picked. Help him to check it!

Input
The first line contains two integer numbers n and x (1?≤?n?≤?100000, 1?≤?x?≤?109) — the number of elements in the encoding and the length of the crossword Mishka picked.

The second line contains n integer numbers a1, a2, ..., an (1?≤?ai?≤?10000) — the encoding.

Output
Print YES if there exists exaclty one crossword with chosen length and encoding. Otherwise, print NO.

Example
Input
2 4
1 3
Output
NO
Input
3 10
3 3 2
Output
YES
Input
2 10
1 3
Output
NO

题意:

给出01字符串,串中连续的1的长度构成一个数列。给出这个数列,问是否能保证得到的字符串是唯一的?

题解:

假如存在0,那么0的相邻两个数必须是1。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main()
{
    int n,x;
    while(cin>>n>>x)
    {
        int sum=0;
        for(int i=0;i<n;i++)
            cin>>a[i],sum+=a[i];
        if(sum+n-1==x)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

CodeForces 884C Bertown Subway

The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.

There are n stations in the subway. It was built according to the Bertown Transport Law:

For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi?=?i;
For each station i there exists exactly one station j such that pj?=?i.
The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x,?y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1?≤?x,?y?≤?n).

The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn‘t want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.

The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get!

Input
The first line contains one integer number n (1?≤?n?≤?100000) — the number of stations.

The second line contains n integer numbers p1, p2, ..., pn (1?≤?pi?≤?n) — the current structure of the subway. All these numbers are distinct.

Output
Print one number — the maximum possible value of convenience.

Example
Input
3
2 1 3
Output
9
Input
5
1 5 4 3 2
Output
17
Note
In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1,?1), (1,?2), (1,?3), (2,?1), (2,?2), (2,?3), (3,?1), (3,?2), (3,?3).

In the second example the mayor can change p2 to 4 and p3 to 5.

题意:

每个地铁站i通往pi站,(x,y)代表x地铁站能够通往y地铁站(x可以等于y)。能够改变两个地铁站的通往方向,问最多能使(x,y)这样的有序数对最多有多少?

题解:

因为有n条边,所以必定有环的存在,用DFS判断有多少个环,然后将最大的两个环连在一起就行了。(感觉自己的DFS写的很不好。。)

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
bool vis[maxn];
int b[maxn];
int dfs(int s,int next,int res)
{
    if(next==s)
        return res;
    if(!vis[next])
    {
        vis[next]=true;
        return dfs(s,a[next],res+1);
    }
    return res;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        memset(vis,false,sizeof(vis));
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                vis[i]=true;
                b[cnt++]=dfs(i,a[i],1);
            }
        }
        sort(b,b+cnt);
        long long ans=0;
        for(int i=0;i<cnt-2;i++)
            ans+=b[i]*b[i];
        ans+=(long long)(b[cnt-2]+b[cnt-1])*(b[cnt-2]+b[cnt-1]);
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-13 22:19:40

CodeForces 884A.B.C Book Reading(水) | Japanese Crosswords Strike Back(水) | Bertown Subway(DFS)的相关文章

百病皆由痰作祟~一碗神奇的水?(生姜红糖水、姜糖水、中医)

百病皆由痰作祟~一碗神奇的水?                  (2015-04-13 16:21:45)转载▼       标签: 画者一剑 子墨 生姜红糖水 健康 分类: 生存与健康                                       董洪涛  2015-1-27        百病皆由痰作祟~一碗神奇的水?              在漫长的医疗 实践中,中医对痰病.痰证有一套比较完整的理论体系和治疗方法:将痰分为广义和狭义两大类.狭义的痰,一般是指呼吸系统的分泌

codeforces 500C New Year Book Reading (贪心,很好的思维题)

C. New Year Book Reading time limit per test 2 seconds memory limit per test 256 megabytes New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-

【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)

[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Gerald bought two very rare paintings at the Sotheby's a

两个和尚抬水有水喝,三个和尚抬水没水喝------IT项目管理之组织架构

说到项目经理岗位,一般的想法是,一个项目只能有一个项目经理,否则责任不明,互相推诿.偏偏IT项目需要有两个甚至三个项目经理.原因何在呢? 典型的IT项目(不包含纯技术或工具类项目)是把用户的需求转化成一个IT系统的实现.这就存在着一对矛盾,即需求与实现的矛盾.需求不确定,方案就无法展开,需求范围变化,实现方案也要随之变化.看起来方案只要跟着需求走就可以了,并没有什么矛盾.但实际的情况是,IT系统的实现并不如一些厂商所承诺的那样具备灵活性,随时随地,想怎么改就怎么改.就算具备了这个灵活性,所花费的

崆若的水题之--全世界最水的邻接表二连发哈哈哈哈

有n个城市,编号为1到n,这些城市当中有m条有向边,现在Kong_Ruo在1号城市,他准备去旅游.如果Kong_Ruo能从城市1到达城市i,并能从城市i回到城市1,那么Kong_Ruo就能去城市i游览.现在请你从小到大输出所有能去游览的城市编号. 输入 第一行:两个整数n,m,分别表示城市个数与有向边个数.后m行:每行两个整数a和b,表示a城市到b城市有一条有向边. 输出 输出所有符合要求的城市编号,每个一行,从小到大输出. 输入示例 5 43 12 34 31 2 输出示例 123 #incl

Codeforces 884C.Bertown Subway ----判环,思路

The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station 

CodeForces - 404B(模拟题)

Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose

Codeforces Round #561 (Div. 2) 题解

Codeforces Round #561 (Div. 2) 题解 题目链接 A. Silent Classroom 水题. Code #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; int n; char s[N], t[N]; int main() { cin >> n; for(int i = 1; i <= n; i++) { scanf(&q

洛谷—— P1190 接水问题

https://www.luogu.org/problem/show?pid=1190#sub 题目描述 学校里有一个水房,水房里一共装有 m 个龙头可供同学们打开水,每个龙头每秒钟的 供水量相等,均为 1. 现在有 n 名同学准备接水,他们的初始接水顺序已经确定.将这些同学按接水顺序从 1到 n 编号,i 号同学的接水量为 wi.接水开始时,1 到 m 号同学各占一个水龙头,并同时打开水龙头接水.当其中某名同学 j 完成其接水量要求 wj后,下一名排队等候接水的同学 k马上接替 j 同学的位置