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;
}