tzcacm去年训练的好题的AC代码及题解
A - Tree
You are to determine the value of the leaf node in a given binary tree that is the terminal node of a
path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values
of nodes along that path.
Input
The input file will contain a description of the binary tree given as the inorder and postorder traversal
sequences of that tree. Your program will read two line (until end of file) from the input file. The first
line will contain the sequence of values associated with an inorder traversal of the tree and the second
line will contain the sequence of values associated with a postorder traversal of the tree. All values
will be different, greater than zero and less than 10000. You may assume that no binary tree will have
more than 10000 nodes or less than 1 node.
Output
For each tree description you should output the value of the leaf node of a path of least value. In the
case of multiple paths of least value you should pick the one with the least value on the terminal node.
Sample Input
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
Sample Output
1
3
255
给你一棵二叉树的中序和后序遍历,那么你的这棵树就是确定了的,让你找到最小的一半枝
其实就是每一层深度都统计下,完全可以弄进这个二叉树
#include<bits/stdc++.h>
using namespace std;
const int N=;
int in[N],post[N],ans,mi;
void solve(int l,int r,int f,int s)
{
if(l==r)return;
if(r-l<=)
{
if(in[l]+s<mi)mi=in[l]+s,ans=in[l];
return ;
}
for(int i=l;i<r;i++)
if(in[i]==post[f])solve(i+,r,f-,s+post[f]),solve(l,i,f+i-r,s+post[f]);
}
int main()
{
ios::sync_with_stdio(false);
string s,c;
while(getline(cin,s))
{
getline(cin,c);
mi=<<;
stringstream ss(s),sc(c);
int n=;
while(ss>>in[n])sc>>post[n++];
solve(,n,n-,);
cout<<ans<<"\n";
}
return ;
}
只有给你前序和后序是没有唯一的中序遍历的,是2^n,因为可以是左子树,也可以是右子树
#include <stdio.h>
#include <string.h>
int pre[];
int post[];
int cc;
void calc(int a1,int b1,int a2,int b2)
{
int i;
if(a1>=b1) return;
for(i=a2; i<=b2-; i++)
{
if(pre[a1+] == post[i]) break;
}
if(i == b2-) cc++;
calc(a1+,a1++(i-a2),a2,i);
calc(a1++(i-a2)+,b1,i+,b2-);
}
int a[];
int main()
{
int n;
scanf("%d",&n);
for(int i=; i<n; i++)
scanf("%d",&pre[i]);
for(int i=; i<n; i++)
scanf("%d",&post[i]);
cc=;
calc(,n-,,n-);
n=cc;
int sum=,i,k;
for(i=; i<; i++)
a[i]=;
a[]=;
for(k=; k<=n; k++)
{
for(i=; i<sum; i++)
a[i]=a[i]*;
for(i=; i<sum; i++)
if(a[i]>=)
{
a[i+]=a[i+]+a[i]/;
if(i+==sum)sum++;
a[i]=a[i]%;
}
}
for(i=sum-; i>=; i--)
printf("%d",a[i]);
printf("\n");
return ;
}
B - Til the Cows Come Home
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
用我常用的垃圾模板,只要不卡直接这个模板稳过啊
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N=,INF=0x3f3f3f3f;
vector<pair<int,int> >G[N];
priority_queue<int>Q;
int dis[N];
int main()
{
int n,m;
cin>>m>>n;
for(int i=,u,v,w;i<m;i++)
cin>>u>>v>>w,G[u].push_back(make_pair(w,v)),G[v].push_back(make_pair(w,u));
vector<pair<int,int> >::iterator it;
fill(dis,dis+n+,INF);
dis[]=,Q.push();
while(!Q.empty())
{
int u=Q.top();
Q.pop();
for(it=G[u].begin();it!=G[u].end();it++)
if(dis[u]+it->first<dis[it->second])dis[it->second]=dis[u]+it->first,Q.push(it->second);
}
cout<<dis[n];
}
C - Frogger
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
Output
Sample Input
2
0 0
3 4 3
17 4
19 4
18 5 0
Sample Output
Scenario #1
Frog Distance = 5.000 Scenario #2
Frog Distance = 1.414
FLOYD最短路
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define fi first
#define se second
const int MAXN=;
pair<int,int>p[MAXN];
double dis(pair<int,int>p1,pair<int,int>p2) {
return (p1.fi-p2.fi)*(p1.fi-p2.fi)+(p2.se-p1.se)*(p2.se-p1.se);
}
int dist[MAXN][MAXN];
int main() {
int n,cas=;
while(~scanf("%d",&n)) {
if(!n)break;
for(int i=; i<n; i++) {
int x,y;
scanf("%d%d",&x,&y);
p[i]=make_pair(x,y);
}
for(int i=; i<n; i++)
for(int j=i; j<n; j++) {
dist[j][i]=dist[i][j]=dis(p[i],p[j]);
}
for(int k=; k<n; k++)
for(int i=; i<n; i++)
for(int j=; j<n; j++)
if(dist[i][j]>max(dist[i][k],dist[k][j]))
dist[i][j]=max(dist[i][k],dist[k][j]);
printf("Scenario #%d\n",cas++);
printf("Frog Distance = %.3f\n\n",sqrt(dist[][]*1.0));
}
return ;
}
D - 0 or 1
Besides,X ij meets the following conditions:
1.X 12+X 13+...X 1n=1
2.X 1n+X 2n+...X n-1n=1
3.for each i (1<i<n), satisfies ∑X ki (1<=k<=n)=∑X ij (1<=j<=n).
For example, if n=4,we can get the following equality:
X 12+X 13+X 14=1
X 14+X 24+X 34=1
X 12+X 22+X 32+X 42=X 21+X 22+X 23+X 24
X 13+X 23+X 33+X 43=X 31+X 32+X 33+X 34
Now ,we want to know the minimum of ∑C ij*X ij(1<=i,j<=n) you can get.
For sample, X 12=X 24=1,all other X ij is 0.
InputThe input consists of multiple test cases (less than 35 case).
For each test case ,the first line contains one integer n (1<n<=300).
The next n lines, for each lines, each of which contains n integers, illustrating the matrix C, The j-th integer on i-th line is C ij(0<=C ij<=100000).OutputFor each case, output the minimum of ∑C ij*X ij you can get.
Sample Input
4
1 2 4 10
2 0 1 1
2 2 0 5
6 3 1 2
Sample Output
3
题目也不容易让你想到最短路,最后其实那个式子就是求一下1到n的最短路,或者结点1的最小闭环(不包括自环)+节点n的最小闭环(不包括自环)的值
那个模板处理不了自环的,但是其实就是1-a,然后a-1,求一下正着的最短路还有反着的最短路就好了吧
#include <stdio.h>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=;
int n,g[N][N],dis[N],dis1[N];
int dij(int s)
{
fill(dis,dis+N,INF);
priority_queue<int> pq;
dis[s]=,pq.push(s);
while(!pq.empty())
{
int u=pq.top();
pq.pop();
for(int i=;i<=n;i++)
if(dis[i]>dis[u]+g[u][i])dis[i]=dis[u]+g[u][i],pq.push(i);
}
}
int dij1(int s)
{
fill(dis1,dis1+N,INF);
priority_queue<int> pq;
dis1[s]=,pq.push(s);
while(!pq.empty())
{
int u=pq.top();
pq.pop();
for(int i=;i<=n;i++)
if(dis1[i]>dis1[u]+g[i][u])dis1[i]=dis1[u]+g[i][u],pq.push(i);
}
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
scanf("%d",&g[i][j]);
g[i][i]=INF;
}
dij(),dij1();
int mi1=INF,mi=dis[n],mi2=INF;
for(int i=;i<=n;i++)
mi1=min(dis[i]+dis1[i],mi1);
dij(n),dij1(n);
for(int i=;i<n;i++)
mi2=min(dis[i]+dis1[i],mi2);
printf("%d\n",min(mi,mi1+mi2));
}
return ;
}
还有一份逆天点的代码,dij经典写法,找到没有访问的点的最小的边
#include <stdio.h>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=;
int n,g[N][N],dis[N];
bool did[N];
int dij(int s,int t)
{
for(int i=; i<=n; i++)
{
if(i!=s)dis[i]=g[s][i];
else dis[i]=INF;
did[i]=;
}
int c=;
while(c<n)
{
int mi,m=INF;
for(int i=; i<=n; i++)
if(!did[i]&&dis[i]<m)
{
m=dis[i];
mi=i;
}
did[mi]=;
for(int i=; i<=n; i++)
if(!did[i]&&dis[i]>dis[mi]+g[mi][i])
{
dis[i]=dis[mi]+g[mi][i];
}
c++;
}
return dis[t];
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
scanf("%d",&g[i][j]);
}
printf("%d\n",min(dij(,n),dij(,)+dij(n,n)));
}
return ;
E - Layout
Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
Input
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample Output
27
Hint
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
InputThere are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
OutputThe output consists of one integer representing the largest number of islands that all lie on one line.
Sample Input
8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q
Sample Output
1
2
3
Hint
Xiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).
题目还是挺简单易懂的,就是有insert还有query操作
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
int n,k,i;
while(cin>>n>>k)
{
multiset<int>v;
while(n--)
{
string s;
cin>>s;
if(s=="I")
{
int x;
cin>>x;
v.insert(x);
if(v.size()>k)v.erase(v.begin());
}
else if(s=="Q")
{
cout<<*v.begin()<<endl;
}
}
}
return ;
}
tzcacm去年训练的好题的AC代码及题解的更多相关文章
- 【南阳OJ分类之语言入门】80题题目+AC代码汇总
小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位. 本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载. 声明: 题目部分皆为南阳OJ题目. 代 ...
- 2018天梯赛第一次训练题解和ac代码
随着评讲的进行代码和题解会逐步放上来 2018天梯赛第一次训练 1001 : 进制转换 Time Limit(Common/Java):1000MS/10000MS Memory Limit: ...
- Educational Codeforces Round 50 (Rated for Div. 2)的A、B、C三题AC代码
A题链接:https://codeforces.com/contest/1036/problem/A A题AC代码: #include <stdio.h> #include <std ...
- HDU2449 Gauss Elimination 高斯消元 高精度 (C++ AC代码)
原文链接https://www.cnblogs.com/zhouzhendong/p/HDU2449.html 题目传送门 - HDU2449 题意 高精度高斯消元. 输入 $n$ 个 $n$ 元方程 ...
- python爬虫学习(7) —— 爬取你的AC代码
上一篇文章中,我们介绍了python爬虫利器--requests,并且拿HDU做了小测试. 这篇文章,我们来爬取一下自己AC的代码. 1 确定ac代码对应的页面 如下图所示,我们一般情况可以通过该顺序 ...
- 【原创】用Python爬取LeetCode的AC代码到Github
在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地 ...
- C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - P ...
- HDU1007 TLE代码和AC代码对比
这题卡了一天,上午开始看算法导论,然后实现了,一开始是wa,后来TLE,由于我开始的实现方式比较笨,而且在递归调用的时候很是混乱,用了好多数组.导致我的代码不断的出问题.具体是算法导论33-4. 后来 ...
- ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码
这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂. 题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度.所 ...
随机推荐
- (四)我的JavaScript系列:原型链
夜深风竹敲秋韵,万叶千声皆是恨. 原型链对于JavaScript来说是个很核心的概念.JavaScript不是基于类模板的面向对象语言:反而,它的面向对象机制是基于原型的.我们不可能说某个对象属于什么 ...
- Windows基础环境_安装配置教程(Windows7 64、JDK1.8、Android SDK23.0、TortoiseSVN 1.9.5)
Windows基础环境_安装配置教程(Windows7 64.JDK1.8.Android SDK23.0.TortoiseSVN 1.9.5) 安装包版本 1) JDK版本包 地址: htt ...
- 使用tensorflow object_detection API训练自己的数据遇到的问题及解决方法
1.Windows下出现找不到object_detection包的问题. 解决方法 在Anaconda3\soft\Lib\site-packages新建一个pth文件,将PedestrianDete ...
- 使用nginx搭建一个简单的负载均衡
在windows系统上使用IIS模拟出两个不同服务器的站点: 然后再NGINX使用轮询机制配置两个服务器以及虚拟服务器的端口: 需要注意的是,配置虚拟代理域名的话需要找到windowsC盘下的host ...
- Ubuntu18.04如何从英文界面更改为中文界面
本文介绍如何将Ubuntu18.04安装后的英文界面,更改为中文界面,即系统语言由英文改为简体中文.注意,与安装中文输入法不同,两者也没有冲突. 首先进入设置(Setting),选择区域和语言(Reg ...
- 标签中的name属性和ID属性的区别
标签中的name属性和ID属性的区别 2018年05月13日 10:17:44 tssit 阅读数:760 编程这么久,细想了一下,发现这个问题还不是很清楚,汗!看了几篇文章,整理了一下,分享下! ...
- 《剑指offer》51:数组中的逆序对
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- Mybatis学习记录(1)
1.Mybatis介绍 Mybatis是apache的一个开源项目iBatis,Mybatis是一个优秀的持久层框架,他对jdbc的操作数据库的过程进行封装,使开发者只需要关注sql本身,不需 ...
- iOS监听电话来电、挂断、拨号等
以下,来讲解在app内如何调用打电话功能和监听电话来电.挂断.拨号等功能. 简单的UI布局: 首先,先实现拨打电话的功能,以便于后续测试: // 拨打电话 - (IBAction)dialingBut ...
- 协议(Protocol)与委托代理(Delegate)
协议(Protocol)的作用: 1. 规范接口,用来定义一套公用的接口: 2. 约束或筛选对象. 代理(Delegate): 它本身是一种设计模式,委托一个对象<遵守协议>去做某件事情, ...