几何问题
Description
如果在一个二维坐标系中,已知三角形三个点的坐标,那么对于坐标系中的任意一点,如何判断该点是否在三角形内(点在三角形边线上也认为在三角形内)?
Input
本问题有多组测试数据,每组数据一行,共8个实数,每两个实数表示一个点,前三个点表示三角形的三个点,后一个点表示需要你判断是不是在内部的点。
Output
如果再三角形内(边上也算内),输出“Yes”,否则输出“No”。
Sample Input
0.0 0.0 4.0 4.0 6.0 0.0 3.0 2.5
Sample Output
Yes
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
using namespace std;
typedef bool nsjzf;
struct jg
{
double a,b;
};
#define E404 1E-6
jg nsj1,nsj2,nsj3,nsj4;
double qEdge(jg x,jg y);
double qS(jg x,jg y,jg z);double nsj[8];
nsjzf qJudge(jg n,jg s,jg j,jg nsj);
void Counting();
int main()
{
int i;
while(cin>>nsj[0]>>nsj[1]>>nsj[2]>>nsj[3]>>nsj[4]>>nsj[5]>>nsj[6]>>nsj[7])
{
Counting();
if(qJudge(nsj1,nsj2,nsj3,nsj4))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
double qEdge(jg x,jg y)
{
return sqrt((x.a-y.a)*(x.a-y.a)+(x.b-y.b)*(x.b-y.b));
}
double qS(jg x,jg y,jg z)
{
double h,nsj,pp,oo;
nsj=qEdge(x,y);
pp=qEdge(y,z);
oo=qEdge(x,z);
h=(nsj+pp+oo)/2.0;
return sqrt(h*(h-nsj)*(h-pp)*(h-oo));
}
nsjzf qJudge(jg n,jg s,jg j,jg nsj)
{
return fabs(qS(s,j,nsj)+qS(n,s,nsj)+qS(n,j,nsj)-qS(n,s,j))<E404;
}
void Counting()
{
nsj1.a=nsj[0];
nsj1.b=nsj[1];
nsj2.a=nsj[2];
nsj2.b=nsj[3];
nsj3.a=nsj[4];
nsj3.b=nsj[5];
nsj4.a=nsj[6];
nsj4.b=nsj[7];
}
Anniversary party
Description
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests’ conviviality ratings.
Input
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go N – 1 lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Output
Output should contain the maximal sum of guests’ ratings.
Sample Input
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
Sample Output
5
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
int n,dp[6001][2],vi[6001],pre[6001];
vector<int> eg[6005];
void dfs(int u)
{
dp[u][1] = vi[u];
dp[u][0] = 0;
int lim = eg[u].size();
for(int i = 0;i < lim;i++)
{
int v=eg[u][i];
dfs(v);
dp[u][1] +=dp[v][0];
dp[u][0] += max(dp[v][0],dp[v][1]);
}
}
int main()
{
while(scanf("%d",&n)&&n)
{
for(int i = 1;i <=n;i++)
{
eg[i].clear();
pre[i]=-1;
scanf("%d",&vi[i]);
}
for(int i=1,a,b;i<n;i++)
{
scanf("%d%d",&a,&b);
eg[b].push_back(a);
pre[a]=b;
}
int root = 1;
while(pre[root] != -1)
root = pre[root];
dfs(root);
cout<<max(dp[root][0],dp[root][1])<<endl;
}
return 0;
}
摘苹果
Description
果园里一棵苹果树,树上有N个节点,每个节点对应一个质量为wi的苹果。老板要求摘了某个苹果后,不能摘他父节点处的苹果,摘错苹果不给钱,最后还要按苹果总质量给工钱。
比如,a是b的父节点,b是c的父节点,c被摘之后b就不能被摘了,但是a可以被摘。
Input
第一行为N,表示节点数量,1≤N≤100000。
接下来N行表示苹果的质量Wi,1≤Wi≤200 。
接下来N-1行代表子节点v和父节点u。
Output
输出一个整数 代表所选苹果质量和的最大值。
Sample Input
7
1
1
1
1
1
1
1
1 3
7 4
2 3
4 5
6 4
3 5
Sample Output
5
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
int n,dp[100001][2],vi[100001],pre[100001];
vector<int> eg[100001];
void dfs(int u)
{
dp[u][1] = vi[u];
dp[u][0] = 0;
int lim = eg[u].size();
for(int i = 0;i < lim;i++)
{
int v=eg[u][i];
dfs(v);
dp[u][1] +=dp[v][0];
dp[u][0] += max(dp[v][0],dp[v][1]);
}
}
int main()
{
while(1==scanf("%d",&n))
{
for(int i = 1;i <=n;i++)
{
eg[i].clear();
pre[i]=-1;
scanf("%d",&vi[i]);
}
for(int i=1,a,b;i<n;i++)
{
scanf("%d%d",&a,&b);
eg[b].push_back(a);
pre[a]=b;
}
int root = 1;
while(pre[root] != -1)
root = pre[root];
dfs(root);
cout<<max(dp[root][0],dp[root][1])<<endl;
}
return 0;
}