2024/2/7北化寒假集训题解(部分)

问题 A: 7.2.2.1 丛林之路

题目描述

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

输入

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

输出

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

样例输入 
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
样例输出 
216
30
题解 
#include <bits/stdc++.h>
using namespace std;
struct Graph
{
    int vexnumber;
    int adjmatrix[27][27];
};
void fun(Graph G)
{
    int U[G.vexnumber]={0};
    int cost[G.vexnumber],adj[G.vexnumber];
    for(int i=0;i<G.vexnumber;i++)
    {
        cost[i]=-1;
        adj[i]=-1;
    }
    U[0]=1;
    int k=1;
    int sum=0;
    int j=0;
    while(k<G.vexnumber)
    {
        
        for(int i=0;i<G.vexnumber;i++)
        {
            
                if(U[i]==0)
                {
                    if(G.adjmatrix[i][j]!=0)
                    {
                        if(cost[i]==-1||cost[i]>G.adjmatrix[i][j])
                        {
                            cost[i]=G.adjmatrix[i][j];
                            adj[i]=j;
                        }
                    }
                }
            
        }
        int minindex=-1;
        int min=-1;
        for(int i=0;i<G.vexnumber;i++)
        {
            if(cost[i]!=-1)
            {
                if((minindex==-1||min>cost[i])&&U[i]==0)
                {
                    minindex=i;
                    min=cost[i];
                }
            }
        }
        U[minindex]=1;
        adj[minindex]=j;
        j=minindex;
        sum+=cost[minindex];
        k++;
    }
    for(int i=0;i<G.vexnumber;i++)
    {
        if(U[i]==0)
        {
            cout<<"-1";
            return ;
        }
    }
    cout<<sum;
}
int main() 
{
    int n;
    while(cin>>n&&n!=0)
    {
        Graph G;
        G.vexnumber=n;
        G.adjmatrix[27][27]={0};
        for(int i=0;i<n-1;i++)
        {
            char source;
            int k;
            cin>>source>>k;
            for(int j=0;j<k;j++)
            {
                char dest;
                int cost;
                cin>>dest>>cost;
                G.adjmatrix[source-'A'][dest-'A']=cost;
                G.adjmatrix[dest-'A'][source-'A']=cost;
            }
        }
        fun(G);
        cout<<endl;
    }
    return 0;
}

问题 B: 7.3.1 家族树

题目描述

火星人的血缘关系系统已经够混乱了。事实上,火星人在他们想发芽的时间和地点发芽。他们以不同的群体聚在一起,所以火星人既可以有一个父母,也可以有十个父母。没有人会对一百个孩子感到惊讶。火星人已经习惯了这一点,他们的生活方式对他们来说似乎很自然。
在行星理事会中,令人困惑的系谱系统导致了一些尴尬。在那里遇到了最有价值的火星人,因此为了在所有的讨论中不冒犯任何人,就先让年老的火星人发言,而不是年轻的,也只是年轻的没有子女的评议者发言。
然而,维护这种秩序确实不是一项琐碎的任务。火星人并不总是知道他所有的父母(而且他的祖父母也没什么可说的!)
但是,如果由于一个错误,首先讲的是一个孙子,而只是比他年轻的曾祖父,这是一个真正的丑闻。
你的任务是编写一个程序,它将一古脑地,定义一个命令,以保证理事会的每一个成员都比他的后代更早地发言。

输入

标准输入的第一行只包含一个数字 N, 1 <= N <= 100 火星行星委员会的数量。
根据悠久的传统,安理会成员是用从1到N的自然数来枚举的。
此外,正好有N行,而且第i行包含第i个成员的子成员的列表。
子元素列表是一组以空格分隔的任意顺序的子元素序号的序列。
子列表可以为空。这个列表(即使它是空的)以0结束。

输出

标准输出应该在它唯一的一行中包含一个由空格隔开的扬声器号码序列。
如果有几个序列满足问题的条件,则将其中任何一个序列写入标准输出。至少存在一个这样的序列。

样例输入 
5
0
4 5 1 0
1 0
5 3 0
3 0
样例输出 
2 4 5 3 1
题解 
#include<bits/stdc++.h>
using namespace std;
struct Graph
{
    int vexnumber;
    int adjmatrix[101][101];
};
void topo(const Graph &G)
{
    stack<int> S;
    int inDegree[G.vexnumber]={0};
    for(int i=0;i<G.vexnumber;i++)
    {
        for(int j=0;j<G.vexnumber;j++)
        {
            if(G.adjmatrix[i][j]==1)
            {
                inDegree[j]++;
            }
        }
    }
    for(int i=0;i<G.vexnumber;i++)
    {
        if(inDegree[i]==0)
        {
            S.push(i);
        }
    }
    while(!S.empty())
    {
        int p=S.top();
        S.pop();
        cout<<p+1<<" ";
        for(int i=0;i<G.vexnumber;i++)
        {
            if(G.adjmatrix[p][i]==1)
            {
                inDegree[i]--;
                if(inDegree[i]==0)
                {
                    S.push(i);
                }
            }
        }
    }
}
int main()
{
    int n;
    cin>>n;
    Graph G;
    G.vexnumber=n;
    G.adjmatrix[n+1][n+1]={0};
    for(int i=0;i<G.vexnumber;i++)
    {
        int child;
        while(cin>>child&&child!=0)
        {
            G.adjmatrix[i][child-1]=1;
        }
    }
    topo(G);
    return 0;
}

问题 D: 8.1.3.1雪花

题目描述

有N片雪花,每片雪花由六个角组成,每个角都有长度。
第i片雪花六个角的长度从某个角开始顺时针依次记为ai1,ai2,…,ai6。
因为雪花的形状是封闭的环形,所以从任何一个角开始顺时针或逆时针往后记录长度,得到的六元组都代表形状相同的雪花。
例如 ai1,ai2,…,ai6 和 ai2,ai3,…,ai6,ai1 就是形状相同的雪花。 ai1,ai2,…,ai6 和 ai6,ai5,…,ai1 也是形状相同的雪花。
我们称两片雪花形状相同,当且仅当它们各自从某一角开始顺时针或逆时针记录长度,能得到两个相同的六元组。求这N片雪花中是否存在两片形状相同的雪花。

输入

第一行输入一个整数N,代表雪花的数量。 接下来N行,每行描述一片雪花。
每行包含6个整数,分别代表雪花的六个角的长度(这六个数即为从雪花的随机一个角顺时针或逆时针记录长度得到)。
同行数值之间,用空格隔开。
1≤N≤100000,
0≤ai,j<10000000

输出

如果不存在两片形状相同的雪花,则输出:
No two snowflakes are alike.
如果存在两片形状相同的雪花,则输出:
Twin snowflakes found.

样例输入 
2
1 2 3 4 5 6
4 3 2 1 6 5
样例输出 
Twin snowflakes found.
题解 
#include<bits/stdc++.h>
using namespace std;
bool compare_snowflakes(const vector<int>& snowflake1, const vector<int>& snowflake2) 
{
    for (int i = 0; i < 6; ++i)
    {
        bool match = true;
        for (int j = 0; j < 6; ++j) 
        {
            if (snowflake1[j] != snowflake2[(i + j) % 6]) 
            {
                match = false;
                break;
            }
        }
        if (match) return true;
        match = true;
        for (int j = 0; j < 6; ++j) 
        {
            if (snowflake1[j] != snowflake2[(i - j + 6) % 6]) 
            {
                match = false;
                break;
            }
        }
        if (match) return true;
    }
    return false;
}

int main() 
{
    int N;
    cin >> N;
    vector<vector<int>> snowflakes(N, vector<int>(6));
    for (int i = 0; i < N; ++i) 
    {
        for (int j = 0; j < 6; ++j) 
        {
            cin >> snowflakes[i][j];
        }
        sort(snowflakes[i].begin(), snowflakes[i].end()); 
    }

    for (int i = 0; i < N; ++i) 
    {
        for (int j = i + 1; j < N; ++j) 
        {
            if (compare_snowflakes(snowflakes[i], snowflakes[j])) 
            {
                cout << "Twin snowflakes found." << endl;
                return 0;
            }
        }
    }
    cout << "No two snowflakes are alike." << endl;
    return 0;
}

问题 E: 18-2 后缀表达式

题目描述

给定n个加号,m个减号,以及n+m+1个整数a1、a2、…… 、a(n+m+1),卡密想知道在所有由n个加号,m个减号,以及n+m+1个整数凑出的合法后缀表达式中,最大的结果是什么?
  例如,使用1 2 3 + -,则“2 3 + 1 -”(2+3-1=4)这个后缀表达式的结果是4, 为最大。
  对于所有样例, 0<=n, m <= 1e5;          -10^9<=  ai  <=10^9

输入

第一行包含一个整数n和m。
第二行包含n+m+1个整数a1、a2、…… 、a(n+m+1).

输出

输出一个整数表示最大值

样例输入 
1 1
1 2 3
样例输出 
4
题解 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m;
    cin>>n>>m;
    long long*arr=new long long[n+m+2];
    for(int i=0;i<n+m+1;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n+m+1);
    long long sum=0;
    for(int i=0;i<n+m+1;i++)
    {
        sum+=arr[i];
    }
    if(m==0)
    {
        cout<<sum<<endl;
    }
    else
    {
        sum=arr[n+m]-arr[0];
        for(int i=1;i<n+m;i++)
        {
            sum+=abs(arr[i]);
        }
        cout<<sum<<endl;
    }
    delete[] arr;
    return 0;
}

问题 F: [蓝桥杯2019初赛]扫地机器人 

题目描述

输入 

第一行包含两个整数N 和K。
接下来K 行,每行一个整数Ai。1<=K<N<=100000,1<=Ai<=N。 

输出 

 输出一个整数表示答案。

样例输入 
10 3
5
2
10
样例输出 
6
题解 
#include<bits/stdc++.h>
using namespace std;
int a[1000000];
int n,m;
int fun(int x)
{
    int sum=0;
    for(int i=0;i<m;i++)
    {
        if(a[i]-x<=sum)
        {
            if(a[i]<=sum)
            {
                sum=a[i]+x-1;
            }
            else
            {
                sum+=x;
            }
        }
        else
        {
            return 0;
        }
    }
    return sum>=n;
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>a[i];
    }
    sort(a,a+m);
    int mid,ans,l=0,r=n;
    while(l<=r)
    {
        mid=l+r>>1;
        if(fun(mid))
        {
            r=mid-1;
            ans=mid;
        }
        else
        {
            l=mid+1;
        }
    }
    cout<<(ans-1)*2<<endl;
    return 0;
}

问题 G: 2.4.9.3 水果

题目描述

Joe经营着一家水果店,他想要一份水果销售情况明细表,这样就可以很容易掌握所有水果的销售情况了。

输入

第1行输入正整数N(0<N≤10),表示有N组测试数据。
每组测试数据的第1行都是一个整数M(0<M≤100),表示共有M次成功的交易。
其后有M行数据,每行都表示一次交易,由水果名称(小写字母组成,长度不超过80)、水果产地(由小写字母组成,长度不超过80)和交易的水果数量(正整数,不超过100)组成。

输出

对每组测试数据,都按照输出样例输出水果销售情况明细表。
这份明细表包括所有水果的产地、名称和销售数量的信息。水果先按产地分类,产地按照字母顺序排列;
同一产地的水果按照名称排序,名称按照字母顺序排序。
每两组测试数据之间都有一个空行。最后一组测试数据之后没有空行。

样例输入 
1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1
样例输出 
guangdong
  |----pineapple(5)
  |----sugarcane(1)
shandong
  |----apple(3)
 题解
#include<bits/stdc++.h>
using namespace std;
int main() 
{
    int N;
    cin>>N;
    for (int t=0;t<N;++t) 
    {
        int M;
        cin>>M;
        map<string,map<string,int>> sales;
        for (int i=0;i<M;++i) 
        {
            string fruit,origin;
            int quantity;
            cin>>fruit>>origin>>quantity;
            sales[origin][fruit] += quantity;
        }
        for (auto it=sales.begin();it!=sales.end();++it) 
        {
            cout << it->first << endl;
            for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) 
            {
                cout << "  |----" << it2->first << "(" << it2->second << ")" << endl;
            }
        }
        if (t<N-1) 
        {
            cout<<endl;
        }
    }
    return 0;
}

问题 H: Oil Deposits

题目描述

GeoSurvComp地质调查公司负择探测地下石油矿床。该公司一次处理一个大的矩形区域,并创建一个网格,将土地划分为许多正方形地块。然后,它分别分析每个地块,使用传感设备来确定地块中是否含有石油。一块含有石油的地块叫作油田。如果两块油田相邻,则它们属于同一个油田区。石油储量可能相当大,并可能包含许多油田。你的工作是确定个网格中有多少不同的油田区。

输入

输入包含一个或多个网格。每个网格开始一行是空格分隔的 m和n,它们分别是网格的行 数和列数。 如果m=0,则表示输入结束;否则1≤m≤100, 1≤n≤100。 接下来是m行,每行有n个字符(不包括行尾字符)。每个字符对应一个地块,“*” 表示没有油:“@”表示油田。

输出

对于每个网格,输出不同的油田区数量并换行。如果水平、垂直或对角相邻,则两块不同的油田是于一-个油田区。油田区数量不超过100个。

样例输入 
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
样例输出 
0
1
2
2
题解 
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<vector<char>>& grid, int i, int j) 
{
    if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] != '@')
        return;
    
    grid[i][j] = '*'; 
    dfs(grid, i - 1, j);
    dfs(grid, i + 1, j);
    dfs(grid, i, j - 1);
    dfs(grid, i, j + 1);
    dfs(grid, i - 1, j - 1);
    dfs(grid, i - 1, j + 1);
    dfs(grid, i + 1, j - 1);
    dfs(grid, i + 1, j + 1);
}
int countOilFields(vector<vector<char>>& grid) 
{
    int m = grid.size();
    int n = grid[0].size();
    int count = 0;
    for (int i = 0; i < m; ++i) 
    {
        for (int j = 0; j < n; ++j) 
        {
            if (grid[i][j] == '@') 
            {
                dfs(grid, i, j);
                ++count;
            }
        }
    }
    return count;
}
int main() 
{
    int m, n;
    while (true) 
    {
        cin >> m >> n;
        if (m == 0)
            break;
        vector<vector<char>> grid(m, vector<char>(n));
        for (int i = 0; i < m; ++i) 
        {
            for (int j = 0; j < n; ++j) 
            {
                cin >> grid[i][j];
            }
        }
        cout << countOilFields(grid) << endl;
    }
    return 0;
}

问题 J: 打怪兽version-3

题目描述

有N只怪兽的血量为Hi
你现在有2个技能
技能1:选择一个怪兽i,使其血量降低1点
技能2:选择一个怪兽i,使其血量变为0
问你需要使用多少个1技能可以杀死所有怪兽(其血量小于等于0即为死亡)
特别的,技能2只能使用K次

输入

N K
H1 H2 .......Hn
1 <= k <= 2e5
1<= N <=2e5
1 <= Hi <= 1e9

输出

需要使用多少个1技能可以杀死所有怪兽

样例输入 
3 1
4 1 5
样例输出 
5
题解 
#include<bits/stdc++.h>
using namespace std;
int main() 
{
    long long N,K;
    cin>>N>>K;
    long long arr[N];
    for(int i=0;i<N;i++)
    {
        cin>>arr[i];
    }
    long long sum=0;
    sort(arr,arr+N);
    for(int i=0;i<N-K;i++)
    {
        sum+=arr[i];
    }
    cout<<sum<<endl;
}

问题 L: 2.4.2 Web导航

题目描述

标准的 Web 浏览器包含在最近访问过的页面中向后和向前移动的功能。实现这些特性的一种方法是使用两个栈来跟踪前后移动可以到达的页面。支持以下命令。
BACK:将当前页面推到前向栈的顶部。从后向栈的顶部弹出页面,使其成为新的当前页面。如果后向栈为空,则忽略该命令。
FORWARD:将当前页面推到后向栈的顶部。从前向栈顶部弹出页面,使其成为新的当前页面。如果前向栈为空,则忽略该命令。
VISIT:将当前页面推到后向栈的顶部,使得 URL 成为新的当前页面,前向栈清空。
QUIT:退出浏览器。
假设浏览器的最初页面为 URL ***###.acm.org/ (对 "http://" 用 "***" 代替,对 "www" 用 "###" 代替)。

输入

输入是一系列 BACK、FORWARD、VISIT、QUIT 命令。

输出

对于除了 QUIT 外的每个命令,如果不忽略该命令,则在执行该命令后单行输出当前页的 URL,否则输出 "Ignored"。
QUIT 命令没有输出。

样例输入 
VISIT ***acm.ashland.edu/
VISIT ***acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT ***###.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
样例输出 
***acm.ashland.edu/
***acm.baylor.edu/acmicpc/
***acm.ashland.edu/
***###.acm.org/
Ignored
***acm.ashland.edu/
***###.ibm.com/
***acm.ashland.edu/
***###.acm.org/
***acm.ashland.edu/
***###.ibm.com/
Ignored
提示

URL 没有空白,最多有 70 个字符。
任何时候,在每个栈中都不会超过 100 个元素。
QUIT 命令表示输入结束。

题解 
#include<bits/stdc++.h>
using namespace std;
int main() 
{
    stack<string> backwardStack;
    stack<string> forwardStack;
    string currentURL = "***###.acm.org/";
    string command;
    while (cin >> command && command != "QUIT") 
    {
        if (command == "BACK") 
        {
            if (!backwardStack.empty()) 
            {
                forwardStack.push(currentURL);
                currentURL = backwardStack.top();
                backwardStack.pop();
                cout << currentURL << endl;
            } 
            else 
            {
                cout << "Ignored" << endl;
            }
        } 
        else if (command == "FORWARD") 
        {
            if (!forwardStack.empty()) 
            {
                backwardStack.push(currentURL);
                currentURL = forwardStack.top();
                forwardStack.pop();
                cout << currentURL << endl;
            } 
            else 
            {
                cout << "Ignored" << endl;
            }
        } 
        else if (command == "VISIT") 
        {
            backwardStack.push(currentURL);
            cin >> currentURL;
            while (!forwardStack.empty()) 
            {
                forwardStack.pop();
            }
            cout << currentURL << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_74367776

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值