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("\n");
                break;
            }
        }
        if(!flag)
            printf("No\n");
    }

    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

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

#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\n", sum,ans);
    return 0;
}
时间: 2024-10-12 05:30:20

Yandex Algorithm 2017 Qualication Round (数组练习 + 拓扑排序练习)的相关文章

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

[ACM] POJ 1094 Sorting It All Out (拓扑排序)

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 9248 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from sm

POJ3249 Test for Job(拓扑排序+dp)

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10137   Accepted: 2348 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a jo

HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9267    Accepted Submission(s): 2668 Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想

Ordering Tasks 拓扑排序

John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task isonly possible if other tasks have already been executed.InputThe input will consist of several instances of the problem. Each instance begins with a

拓扑排序(Topological Order)UVa10305 Ordering Tasks

2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意一对顶点u,v满足如下条件: 若边(u,v)∈E(G),则在最终的线性序列中出现在v的前面 好了,说人话:拓扑排序的应用常常和AOV网相联系,在一个大型的工程中,某些项目不是独立于其他项目的,这意味着这种非独立的项目的完成必须依赖与其它项目的完成而完成,不妨记为u,v,则若边(u,v)∈E(G),代

ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TshingHua OJ-旅行商TSP)

做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(TSP) Description Shrek is a postman working in the mountain, whose routine work is sending mail to n villages. Unfortunately, road between villages is

CodeForces 86D(Yandex.Algorithm 2011 Round 2)

思路:莫队算法,离线操作,将所有询问的左端点进行分块(分成sqrt(n) 块每块sqrt(n)个),用左端点的块号进行排序小的在前,块号相等的,右端点小的在前面. 这样要是两个相邻的查询在同一块内左端点每次最多移动sqrt(n) n次的话效率为nsqrt(n) ,对于同一块内右端点为有序的那么最多移动 n次  .总的效率为nsqrt(n) . 要是相邻的查询不同块 最坏效率为O(n) 因为块最多为sqrt(n)个那么坏效率也为nsqrt(n).   总的效率就为nsqrt(n) #include

hiho一下 第四十八周 拓扑排序&#183;二【拓扑排序的应用 + 静态数组 + 拓扑排序算法的时间优化】

题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当然小Hi和小Ho也参与到了其中.从大家各自了解的情况中,小Hi和小Ho整理得到了以下的信息: 校园网主干是由N个节点(编号1..N)组成,这些节点之间有一些单向的网路连接.若存在一条网路连接(u,v)链接了节点u和节点v,则节点u可以向节点v发送信息,但是节点v不能通过该链接向节点u发送信息. 在刚