Yandex Algorithm 2017 Qualication Round (数组练习 + 拓扑排序练习)

Problem A. Task Management
Input le: standard input Output le: standard output Time limit: 2 seconds Memory limit: 256 megabytes
Sergey is Yandex junior software engineer for only several months, but he already completed n tasks. In order to manage tasks Yandex utilizes a special task manager system (the so called task tracker), tasks in Sergey's tracker are numbered with integers from 1 to n. As it often happens with developers, Sergey always forgets to close the completed tasks. It is almost the time of his performance review, that's why he nally managed to close all tasks. Initially all n tasks of Sergey are open. Right now tasks in his tracker follow in the order of last change time, but Sergey wants to close them strictly in the order from 1 to n. Sergey acts as follows. He looks through the task list from top to bottom and closes some tasks. After he reaches the end of the list, he starts once again from the beginning. Help Sergey determine how many times he has to start looking through the list from the very beginning until the moment all n tasks become closed. The task can be closed only once.
Input In the rst line of input there follows the single integer n (1 ≤ n ≤ 200000), the number of tasks completed by Sergey. In the second line there follows the order of tasks in the tracker.
Output
Output the only integer, the number of times he has to start looking through the list from the very beginning until he closes all the tasks in the tracker.
Examples
standard input

2

1 2

3

3 2 1

5

1 3 2 5 4

standard output

1
3
3
Note
In the rst sample test Sergey closes all the tasks in the system right after the rst time he looks through it. In the second sample test during the rst run through the list he closes only the task number 1, during the second run he closes only the task number 2, and during the third run he nally closes the last task number 3.

题意: 给定一个打乱的包含1 – n 的序列,要求每次只能从开头找到结尾,依次取出 1 - n的数字,问最少需要从头找到结尾这样找几次。

耍小聪明的题目,开始构出一个标记第i个数在哪个位置的数组,然后遍历一边数组,如果下一个数的位置在当前数所在位置的前面,那么次数就要加一

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;

int link[200010];
int task[200010];
int main()
{
    memset(link, 0, sizeof(link));
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &task[i]);
        link[task[i] - 1] = i;
    }
    int cnt = 0, pos = link[0];
    for(int i = 0; i < n; i++)
    {
        pos = link[i];
        if(link[i+1] < pos)
            cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}

Problem B. Cross-City Communication
Input le: standard input Output le: standard output Time limit: 2 seconds Memory limit: 256 megabytes
Yandex has oces in 16 cities across 8 countries. It often happens that employees located in dierent cities have to meet and have discussion about tasks and plans on future development. In order to make this possible each oce provides several conference rooms with cross-oce video-chat facilities. Conference rooms are used very intensively, so the meeting organizer has to book conference rooms in all cities involved in advance so that each meeting participant may connect with everybody at the right moment of time. You are given the information about conference rooms availability in all oces by the day of meeting, and also m queries of concluding an hour-long meeting for employees from dierent cities. For each query you have choose the set of conference rooms in the given cities (in each city you must choose exactly one conference room and those conference rooms must all be free during some hour-long time interval), or determine that there is no satisfying set of conference rooms. Note that the queries are independent from each other, i.e. the answer to each query doesn't actually change the availability of conference rooms.

Input

First line of the input contains an integer c (2 ≤ c ≤ 16), the number of oces. After that there follow c blocks with oce description. Each block starts with the name of the city the oce is located in, and then follows the number of conference rooms ni (1 ≤ ni ≤ 100). After that there follow ni lines, each of them contains the availability schedule tij and the name of the conference room sij. The schedule tij is a string consisting of exactly 24 characters, k-th of them equals to 'X' if the conference room is unavailable for booking during the k-th hour of a day, and to '.' if it is available. In the next line there follows an integer m (1 ≤ m ≤ 1000), the number of queries. Each of the following m lines rst contains an integer l (2 ≤ l ≤ c), the number of cities that should have a booked conference room, after that l city names follow. City names are space-separated. No two conference room share the same name. No two cities share the same name. Names of conference rooms and cities are non-empty strings consisting of no more than 10 English characters.

Output
For each of the m queries output the line containing either Yes (without the quotes) and the names of conference rooms, or the No (without the quotes) if it is impossible to book the suitable set of conference rooms. You may output conference rooms in each answer in any order. If there are several possible answers to the query, you may output any correct one.


Yandex Algorithm 2017 Qualication Round, April 29, 2017


Examples


standard input

3

Moscow 2

XXXXXXXX.X.X.X.X.X.XXXXX Kvartal

XXXXXXXXX.X.X.X.X.X.XXXX Kvartet

Minsk 1

XX.XXXXX........XXXXXXXX Toloka

Berlin 2

XX..XXXXXXXXXXXXXXXXXXXX Mitte

XXXXXXXXXXXXXXXX.....XXX Lustgarten

4

3 Moscow Minsk Berlin

2 Moscow Minsk

2 Minsk Berlin

2 Moscow Berlin

3

Moscow 1

XXXXXXXX...........XXXXX Kvartal

Minsk 1

XXXXXXX...........XXXXXX Toloka

Berlin 1

XXXXXX...........XXXXXXX Mitte

1

3 Moscow Minsk Berlin

standard output

No

Yes Kvartal Toloka

Yes Toloka Mitte

Yes Kvartal Lustgarten


Yes Kvartal Toloka Mitte

题目不难,就是数据储存比较麻烦,mark上

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int n, q;
char city[20][50];
int cinum[20];
string tim[20][200];
string name[20][200];
int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%s %d", city[i], &cinum[i]);
        for(int j = 0; j < cinum[i]; j++)
        {
            cin>>tim[i][j]>>name[i][j];
            //cout<<tim[i][j]<<" "<<name[i][j]<<endl;
        }
    }
    scanf("%d", &q);
    while(q--)
    {
        int num;
        scanf("%d", &num);
        string nam;
        vector<int> cit;
        for(int i = 0; i < num; i++)
        {
            cin>>nam;
            for(int j = 0; j < n; j++)
                if(nam == city[j])
                {
                    cit.push_back(j);
                    break;
                }
        }
        /*for(int i = 0; i < cit.size(); i++)

            cout<< cit[i]<< " ";*/
        string ans[20];
        int cnt;
        int flag;
        for(int i = 0; i < 24; i++)
        {
            cnt = 0;
            flag = 1;
            for(int j = 0; j < cit.size(); j++)
            {
                int k = cit[j];
                int ok = 0;
                for(int o = 0; o < cinum[k]; o++)
                {
                    if(tim[k][o][i] == '.')
                    {
                        ok = 1;
                        ans[cnt++] = name[k][o];
                        break;
                    }
                }
                if(ok == 0)
                {
                    flag = 0;
                    break;
                }
            }
            if(flag == 1)
            {
                printf("Yes");
                for(int i = 0; i < cnt; i++)
                    cout<<" "<<ans[i];
                printf("
");
                break;
            }
        }
        if(!flag)
            printf("No
");
    }

    return 0;

}

Problem C. Test Invocation Input le: standard input Output le: standard output Time limit: 1 second Memory limit: 256 megabytes
In order to check the correctness of all the changes performed by developers, Yandex uses several automated continuous testing and build system. Vladimir's project is being tested on n tests conveniently numbered from 1 to n. Testing is done in a very tricky manner. System starts by invoking the root test that has the number 1. Each test including the root one rst invokes all the tests it depends from, and then runs a certain check of their results, the required time to run this check for the i-th test is ai seconds. It is known that tests form a tree-like structure, i.e. each test except the root one belongs to a dependency list of exactly one other test. Vladimir decided to increase the stability of the whole testing process by rewriting it in such manner that each test now invokes all the tests it depends from twice, and then runs their result check once. The root test is still invoked exactly once. After his changes some tests are being run several times, and the total running time of all tests increased. By knowing the description of all tests determine the total running time of a whole testing process on all the tests before Vladimir's changes and after.
Input

The first line of the input contains the only single integer n (2 ≤ n ≤ 50), the number of tests in Vladimir's system. The second line contains n integers ai (1 ≤ ai ≤ 50), the time of checking the results in each of the tests. The following n lines contain the description of dependencies between tests. The i-th line contains the number ki (0 ≤ ki ≤ n−1), the number of tests that the i-th test is dependent from. After that this line contains ki integers bij (2 ≤ bij ≤ n), the indices of the tests that i-th test is dependent from. It is guaranteeed that each of the tests from 2-nd to n-th appears in a dependency list of exactly one other test.
Output
In the only line output two integers: total running time of the original and new testing systems

PX)Z)`I{AZ5)6T%MB~ZIZC8

输出两个数,一个数所有时间的和,第二个是要求为了完成最根处的一个测试,要把他所依赖的测试做两遍(对于每一个要做的测试,他所依赖的测试都要做两遍),题目开始理解有点错误

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
typedef long long LL;
int n;
int se[100];
LL pre[100];
int ind[100];
int topo[100];
int mp[100][100];
int sum;


void topsort()
{
    int vis[100];
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++)
    {
        int j;
        for(j = 1; j <=n; j++)
        {
            if(ind[j] == 0 && !vis[j])
            {
                vis[j] = 1;
                break;
            }
        }
        topo[i] = j;
        for(int k = 1; k <=n; k++)
        {
            if(mp[j][k] != 0)
                ind[k]--;
        }
    }
}

int main()
{
    scanf("%d", &n);
    sum = 0;
    memset(ind, 0, sizeof(ind));
    memset(mp, 0, sizeof(mp));
    memset(se, 0, sizeof(se));
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &se[i]);
        sum += se[i];
    }
    for(int i = 1; i <= n; i++)
    {
        int m;
        scanf("%d", &m);
        for(int j = 1; j <= m; j++)
        {
            int v;
            scanf("%d", &v);
            mp[v][i] = 1;
            ind[i]++;
        }
    }

    topsort();

    memset(pre, 0, sizeof(pre));
    for(int i = 1; i<= n; i++)
        pre[i] =  se[i];
    for(int i = 1; i <= n; i++)
    {
        int k = topo[i];
        for(int j = 1; j <= n; j++)
        {
            if(mp[j][k] != 0)
            {
                pre[k] += 2 * pre[j];
            }
        }
    }
    LL ans = 0;
    ans = pre[topo[n]];
    printf("%d %lld
", sum,ans);
    return 0;
}
如果有错误,请指出,谢谢
原文地址:https://www.cnblogs.com/Alruddy/p/7148145.html