Simulation test

Tourism Planning 7-1

With a road map for travel by car, you will know the length of the highway between the city and the road tolls to be charged. Now you need to write a program to help the tourists to come to counseling to find the shortest path between a departure and destination. If there are several paths are the shortest, you need to output the least expensive path.

Input format:
Input Description: first input data line 4 is given positive integers N, M, S, D, where N (2≤N≤500) is the number of the city, the city assumed way numbered from 0 to ( N-1); M is the number of the highway; S number From the city; D is numbered destination city. Subsequent M rows, each row is given an information highway, are: 1 urban, urban 2, the length of the highway, toll revenue, separated by a space intermediate, figures are an integer of not more than 500. Enter exist to ensure solution.

Output format:
length and total charges output path, separated by spaces between the numbers in a row, the end of the output can not have extra space.

Sample input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample output:
3 40

Experiment Code

#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;

struct Trip
{
    int next;
    int cost; 
    int dis;
};

vector<Trip> edge[501];
bool mark[501];  //标记
int dis[501];
int cost[501];

int main()
{
    int n, m, S, T;  //城市个数,高速公路条数,起点,终点
    int i, j;
    Trip temp;
    int newP;

    while (scanf("%d%d%d%d", &n, &m, &S, &T) != EOF)
    {
        if (n == 0 && m == 0)
            break;

        for (i = 0; i <= n; i++)
        {
            edge[i].clear();  //初始化邻接链表
            dis[i] = -1;
            mark[i] = false;
            cost[i] = 0;
        }

        while (m--) //高速公路条数
        {
            int city1, city2, len, money;
            scanf("%d%d%d%d", &city1, &city2, &len, &money);
            temp.cost = money;
            temp.next = city2;
            temp.dis = len;
            edge[city1].push_back(temp);
            temp.next = city1;
            edge[city2].push_back(temp);  //无向图,故每条边信息都要添加到两个顶点的单链表中
        }

        dis[S] = 0;
        mark[S] = 1;
        newP = S;  //起点为S
        for (i = 1; i < n; i++)
        {
            for (j = 0; j < edge[newP].size(); j++)
            {
                //更新一个顶点对它的边表内结点的距离
                int next = edge[newP][j].next;
                int money = edge[newP][j].cost;
                int len = edge[newP][j].dis;

                if (mark[next] == true)
                    continue;
                if (dis[next] == -1 || dis[next] > dis[newP] + len || ((dis[next] == dis[newP] + len) && (cost[next] > cost[newP] + money)))
                {
                    dis[next] = dis[newP] + len;
                    cost[next] = cost[newP] + money;
                }
            }

            int minx = 66666666;
            for (j = 0; j <= n; j++)
            {
                //寻找这个顶点出发的最小值
                if (mark[j] == true)
                    continue;
                if (dis[j] == -1)
                    continue;
                if (dis[j] < minx)
                {
                    minx = dis[j];
                    newP = j;
                }
            }
            mark[newP] = true;
        }

        printf("%d %d\n", dis[T], cost[T]);
    }
    return 0;
}

7-3 rainbow bottle

Production process Rainbow bottle (do not) like this: first a large number of bottles laid on the loading site, and then follow a certain order of each color will be evenly spread the ball to these bottles.
Suppose rainbow bottle package according to the order N colors pellets (on the order might be numbered from 1 to N). Now the factory have each color of each ball a box, a box of a box of workers need to be moved the ball from the factory filled venue. If you moved the ball just this little box is filled with color, direct unpacking filling; if not, put the first box code on a temporary shelf, is a method of stacking crates pile up. When a color dress filled out, take a look at the top of a shelf next to the box is not filled with color, they took it down if it is loaded, otherwise go to the factory and then move a box over.
If the order of factory shipments better, workers can be completed successfully loaded. 7, for example, according to the order of colors packed, shipped 7,6,1,3,2,5,4 plant according to this order, the worker to get two immiscible 7,6 loaded color to the next in accordance with 7, 6 in order on the stack on the shelf; can be loaded directly to get 1; 3 time and have to get on the provisional code number of the color box 6; 2 can be loaded directly get; subsequently removed from the top shelf 3 is loaded ; then get 5, 6 placed above provisional code; finally take the color No. 4 was loaded directly; remains is sequentially removed from the shelves 5,6,7 sequentially loaded.
However, if the factory shipment in accordance with this order 3,1,5,4,2,6,7, the workers must be angry toss shelves, because the color package 2 filled out after a plurality of the boxes do not move down on the shelf you'll get the No. 3 box, it is impossible to successfully complete the task. In addition, the shelves of limited capacity, if the accumulation of goods exceeds the capacity of workers is no way to complete the task successfully. Such as factory shipments in accordance with 7,6,5,4,3,2,1 this order, if the shelf is high enough, the boxes can be stacked 6, it can still be successfully completed; but only if the shelf boxes stacked five workers but also angry ...... on this topic please you determine what, shipments of factory order could allow workers to successfully complete the task.
Sample input:

7 5 3 
7 6 1 3 2 5 4
3 1 5 4 2 6 7 
7 6 5 4 3 2 1 

Sample output:

YES 
NO 
NO

Experiment Code

#include<iostream>
#include<stack>
using namespace std;

int main()
{
    int n, m, k;
    int num;

    scanf("%d%d%d", &n, &m, &k);

    while (k--)
    {
        stack <int> stk;  //申请一个栈
        int t = 1;  //第一个编号
        int flag = 0;  //判断是否超出栈容量
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &num);
            if (num == t)  //刚来的正好是需要的
            {
                t++;
                while (stk.size())  //如果不为空的话
                {
                    if (stk.top() == t)  //判断栈顶元素与需要的那个是否一致
                    {
                        stk.pop();  //是就出栈
                        t++;  //需要的下一个+1;
                    }
                    else
                        break;
                }
            }

            else  //不是需要的
            {
                stk.push(num);  //进栈
                if (stk.size() > m)
                    flag = 1;    ////如果栈的容量超出了给定范围,就标记一下。
                //注意不能break,不然栈里面就有上一个的残留。
            }
        }

        if (flag == 1 || t < n)
        {
            printf("NO\n");
        }
        else
            printf("YES\n");
    }

    return 0;
}

On the moral 7-4

Song Dynasty historian Sima Guang, there is a famous "moral theory" in the "Mirror": "The actual occurrence of that of the saints to do the whole virtuous, virtuous and death that of a fool, Ruby was that of a gentleman, just Shengde that of the villain. people who take the surgery, Gou not saints, and with the gentleman, was its villain, was not as fools. "

Now gives moral and scores a number of candidates, please give admission ranked according to Sima Guang theory.

Input format:
input of the first line gives the three positive integers, respectively: N (≦ 10
. 5
), i.e., the total number of candidates; L (≥60), the lowest score for candidate, i.e., not de points and Caifen below L candidates eligible to be considered for admission; H (<100), priority admission line - not Caifen de points and below this line is defined as "the whole virtuous best" candidates by such descending sort out moral; Caifen but not de class assigned to a line candidates are "Ruby only", also sorted by score, but behind the first category candidates; scores were low moral candidates H, but not less than Caifen de points are "dead and virtuous" but there are "Ruby only" person, sorted by score, but behind the second category candidates; other low line L candidates also sorted out according to, but behind the third category candidates.

Then N rows, each row gives information of an examinee, comprising: a ticket number, de points, Caifen, wherein the ticket number is the 8-bit integer, integer divided moral interval [0, 100] within. Between numbers separated by a space.

Output format:
first line is output first given number of candidates to reach the minimum score M, then M rows, each row in accordance with the output of an input format candidates, the candidates according to the rules listed in the order of the input instructions. When there is more than certain candidates out of the same, in descending order according to their de points; if de points also tied in ascending order of the output ticket number.

Sample input:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

Sample output:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

Experiment Code

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
    int num;  //考号
    int de;   //德分
    int cai;  //才分
};

int cmp(struct node a, struct node b) 
{
    if ((a.de + a.cai) != (b.de + b.cai))
        return (a.de + a.cai) > (b.de + b.cai);  //从大到小排序

    else if (a.de != b.de)
        return a.de > b.de;  //从大到小排序

    else
        return a.num < b.num;  //从小到大排序
}


int main()
{
    int n, Low, High;
    scanf("%d%d%d", &n, &Low, &High);  //人数,最低线,最优线

    vector<node>v[4];  //定义一个容器为v,可以放node实体
    node temp;

    int total = n;  //达到最低人数
    for (int i = 0; i < n; i++)
    {
        scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
        if (temp.de < Low || temp.cai < Low)  //德分和才分都 < 最低线
        {
            total--;
        }
        else if (temp.de >= High && temp.cai >= High)  //德分才分不低于最低线,才德全尽
        {
            v[0].push_back(temp);
        }
        else if (temp.de >= High && temp.cai < High)  //德分达到最优线,才分低于最优线,德胜才
        {
            v[1].push_back(temp);
        }
        else if (temp.de < High && temp.cai < High && temp.de >= temp.cai)  //德才分均低于H,德分高于才分,才德兼亡
        {
            v[2].push_back(temp);
        }
        else  //其他达到最低线的考生
        {
            v[3].push_back(temp);
        }
    }

    printf("%d\n", total);  //达线人数

    for (int i = 0; i < 4; i++)  //四类考生
    {
        sort(v[i].begin(), v[i].end(), cmp);  //排序,首地址;尾地址;比较函数,自定义,返回值bool型,小于(降序)
        for (int j = 0; j < v[i].size(); j++)
        {
            printf("%d %d %d\n", v[i][j].num, v[i][j].de, v[i][j].cai);
        }
    }

    return 0;
}

Median 7-5 two ordered sequences

There are two known non-equal length descending sequence S1, S2, S1 and S2 function evaluation design and set median. Ordered sequence A0, A1, ⋯, AN- 1 refers to the median value of A (N-1) / 2, i.e., the first ⌊ (N + 1) / 2⌋ number (A0 is the number 1).
Input formats:
Enter the three lines. The first line gives the sequence of a common length N (0 <N≤100000), followed by a sequence of information per line, i.e., in descending order of N non-integer. Digital intervals by a space.

Output format:
output bits of the input sequence, and two sets of sequences in a row.

Sample Input 1:

5
1 3 5 7 9
2 3 4 5 6

Output Sample 1:
4
Input Sample 2:

6
-100 -10 1 1 1 1
-50 0 2 3 4 5

Output Sample 2:
1

Experiment Code

#include <iostream>  //将两个合并,输出中间那个
#include<stdio.h>
#include <algorithm>
using namespace std;

int main()
{
    int N;

    scanf("%d", &N);
    int num[200002];
        
    for (int i = 0; i < N * 2; i++)
    {
        scanf("%d", &num[i]);
    }

    sort(num, num + (N * 2));

    printf("%d\n", num[N-1]);

    return 0;
}

Guess you like

Origin www.cnblogs.com/lqx0123/p/11938721.html