Educational Codeforces Round 31(ABCD):有向图求环长度/数据结构贪心

比赛地址

A. Book Reading
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
input
2 2
86400 86398
output
2
input
2 86400
0 86400
output
1

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main(){
    int n,i,j,t,now;
    scanf("%d%d",&n, &t);
    int sum = 0;
    int res ;
    bool flag = false;
    for(i=1;i<=n;i++){
        scanf("%d",&now);
        sum += (86400 - now);
        if(sum>=t && !flag){
            res = i;
            flag =true;
        }
    }
    printf("%d\n",res);


    return 0;
}

B. Japanese Crosswords Strike Back
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
input
2 4
1 3
output
NO
input
3 10
3 3 2
output
YES
input
2 10
1 3
output
NO

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int main(){
    int n,x,i,j;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        x -= a[i];
    }
    if(x == n-1){
        printf("YES\n");
    }else{
        printf("NO\n");
    }


    return 0;
}

C. Bertown Subway
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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

题目给一个有向图,求最长的两个有向图长度。因为题目的数据是保证每个点都有入度,因此环上某点出发必定能够搜到自身。瞎搞一下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
using namespace std;
const int maxn = 1e5+10;
vector<int> mp[maxn];
bool vis[maxn];
int save[maxn];
bool dfs(int root, int index, int sum){
    int i,j;
    if(vis[index]){
        return true;
    }
    if(sum!=0 && root == index){
        vis[index] = true;
        //save[++p] = sum;
        save[root] = max(save[root], sum);
        return true;
    }
    bool flag = false;
    for(i=0;i<mp[index].size();i++){
        if(dfs(root, mp[index][i], sum+1)){
            flag = true;
            vis[mp[index][i]] = true;
        }
    }
    return flag;
}
bool compare(int a, int b){
    return a>b;
}
int main(){
    int n,i,j,a;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&a);
        mp[i].push_back(a);
    }
    memset(save, 0, sizeof(save));
    memset(vis, 0, sizeof(vis));
    for(i=1;i<=n;i++){
        dfs(i, i, 0);
    }
    /*for(i=1;i<=n;i++){
        cout<<i<<" "<<save[i]<<endl;
    }*/
    sort(save+1, save+1+n, compare);
    long long res = 0;
    if(save[2]!=0){
        res += 1LL*(save[1] + save[2]) * 1LL*(save[1] + save[2]);
    }else{
        res += 1LL*save[1]*save[1];
    }
    int p = 3;
    while(save[p]!=0){
        res += 1LL*save[p]*save[p];
        ++p;
    }
    printf("%lld\n",res);

    return 0;
}

D. Boxes And Balls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan has n different boxes. The first of them contains some balls of n different colors.

Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th box will contain all balls with color i.

In order to do this, Ivan will make some turns. Each turn he does the following:

Ivan chooses any non-empty box and takes all balls from this box;
Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.

Help Ivan to determine the minimum possible penalty of the game!

Input
The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of boxes and colors.

The second line contains n integer numbers a1, a2, …, an (1 ≤ ai ≤ 109), where ai is the number of balls with color i.

Output
Print one number — the minimum possible penalty of the game.

Examples
input
3
1 2 3
output
6
input
4
2 3 4 5
output
19
Note
In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6.

In the second example you make two turns:

Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 — to the fourth box and the rest put back into the first box. Penalty is 14;
Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 — to the second box. Penalty is 5.
Total penalty is 19.

给n个盒子,以及n种颜色球的数量。一开始在第一个盒子里放着全部n种颜色的球,要求我们每次从一个盒子里把这个盒子里的全部的球取出来,然后分成2份或三份的球分别放到2/3个空盒子中。
现在设一个sum一开始等于0,每次从盒子里取一堆球sun += 这一堆球的数量。问最小的sum多少能够把n种不同颜色的球分别放到一个盒子里

首先,贪心。每次我们都把数量最多颜色的球扔到一个盒子里就不再动它了。这样反复。。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 2e5+10;
long long save[maxn];
priority_queue<long long, vector<long long>, greater<long long> > que;
int main(){
    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%lld",&save[i]);
        que.push(save[i]);
    }
    long long res = 0;
    if(n%2==0) que.push(0);
    while(que.size()>1){
        long long now = 0;
        for(j=1;j<=3 && !que.empty();j++){
            now += que.top();
            que.pop();
        }
        que.push(now);
        //cout<<now<<" "<<que.size()<<endl;
        res += now;
    }
    printf("%lld\n",res);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值