暑假集训日记--8.9--搜索、图论专题总结

来源:互联网 发布:表示喜欢的网络用语 编辑:程序博客网 时间:2024/06/05 09:50

今天搜索和图论的专题就结束了。

总结一下在这一周半的学习:

首先通过做题巩固了搜索的知识。深搜和广搜也有了较大的提高。但是图论花的时间相对低。

专题只做了18个题,剩下的题会补上。

在过去的一周半里,前面还好一点,后面每写一个代码都要调试好久。有的时候一晚上都在调试一个代码。感觉时间全都用来调代码了。我觉得这也反映了一个问题,就是基础知识的不牢固以及理解的还不够深刻。

这一套专题有很多人AK了。感觉自己好菜……唉还是功夫不到家吧。

明天好像就要开始新的专题了,单调队列和二分。

今天复习了一下二分的知识。印象当中好像二分学的还可以。。。

然后看了看单调队列。

下面对单调队列做一个简单的理解总结:

单调队列是队首和队尾都可以进行出队操作,在队尾进行入队操作。

若新插入的元素从队尾插入后会破坏单调性,删除队尾元素,直到不破坏单调性为止,将其插入单调队列。

既然是单调队列,那么获取最大值或者最小值就可以访问队首或者队尾元素。

自己考虑了一下:

基本上代码就这样:

int head = 1,tail = 0;

for (在输入的数的个数里)

{

输入元素。

while(head <= tail && 新插入的元素和tail位置元素进行比较)

若不满足条件则tail--;

直到满足条件,将元素插入。

}

今天看了几道例题,还有记录路径的问题。有很多,在接下来的专题训练中可以去加深巩固,然后继续学习。

努力的补知识点:新的一周还是要努力看书。这一周看书有点少说实话。要多看课件,例题,然后书。

同时在学算法学累了的时候,空闲时间可以看看《c++ primer》。争取这个假期看完一遍。

上一周的表现确实让人不太满意。

下一个专题,继续努力。

争取可以AK。

附今天做的一道最小生成树的题:

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on. 

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
1/Σ(to,td)d(to,td)

where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types. 
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan. 
 

Input
The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types. 
 

Output
For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan. 
 

Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0
 

Sample Output
The highest possible quality is 1/3.

一道很基础的最小生成树的问题。

附源代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <string>
using namespace std;
int map[10000][10000];
int dis[10000],visit[10000];
int n,i,j;
struct Str
{
    string a;
}str[10000];
int get(int i,int j)
{
    int sum = 0;
    for (int t = 0; t < 7; t++)
    {
        if (str[i].a[t] != str[j].a[t])
            sum++;
    }
    return sum;
}
int main()
{
    while (scanf("%d",&n) != EOF)
    {
        if (n == 0)
            break;
        for (i = 1; i <= n; i++)
            cin >> str[i].a;
        for (i = 1; i <= n - 1; i++)
        {
            for (j = i + 1; j <= n; j++)
            {
                map[i][j] = get(i,j);
                map[j][i] = get(j,i);
            }
        }
        memset(visit,0,sizeof(visit));
        for (int t = 1; t <= n; t++)
            dis[t] = map[1][t];
        visit[1] = 1;
        int flag,sum = 0;
        for (int t = 1; t <= n - 1; t++)
        {
            int minn = 100000;
            for (int p = 1; p <= n; p++)
            {
                if (!visit[p] && dis[p] < minn)
                {
                    minn = dis[p];
                    flag = p;
                }
            }
            visit[flag] = 1;
            sum += dis[flag];
            for (int p = 1; p <= n; p++)
            {
                if (!visit[p] && dis[p] > map[flag][p])
                    dis[p] = map[p][flag];
            }
        }
        cout << "The highest possible quality is 1/" << sum <<"."<< endl;
    }
    return 0;
}