Codeforces Good Bye 2014 A.B.C.D.

今天终于升到1900了,2014年的一个好的结束!虽然现在是偶尔打打codeforces,虽然没有了功利的诱惑,但是还是一如既往的喜欢,像蜗牛一样慢慢的前进吧,2015继续加油!!!

话不多说,说说这次的题目.

A题比较简单,就是建图,然后dfs一遍即可得到答案。

B题还是稍微花了点时间的。题意是给一个排列,求能得到的‘’较美丽“的排列,其定义类似于字典序。然后给一个nxn矩阵说明哪些元素可以交换位置。用了一个并查集将可交换的元素都放在一个集合里面,然后在这个集合里面排序,最后得到的排列就是所求排列。

C题也不难。题意有点麻烦,就简单说说,就是”拿书问题“求最小耗费。首先我们可以根据将要拿书的顺序贪心的得到书排放最好的顺序:从上到下就是将要拿书的顺序。然后就可以按照拿书的顺序模拟拿书的过程并计算耗费。

D题是组合数学题。一开始其实就想到了过程,但是调试了很久,还是太久没有比赛啊。。。

题意是给出一个树,每条边上有边权,随机选取三个不同的点,求三点中两两距离之和的和的期望,并且每次修改一个边权,再求出其期望。给出我的思路:一开始我想到的是用树形dp来推一推,但是始终建立不了方程,后来换了一个思维:从边的角度来考虑问题。实际上就是考虑每条边被计算的概率。考虑每条边,一个图取三个点,取法是C(n,3),然后这条边被计算的情况数目是C(a,1)xC(b,2)+C(a,2)xC(b,1)其中a+b=n,并且每种情况该边被计算两次(由于三角形的三边之中肯定有两条边包含该边)。最终将所有边权乘上概率得到期望。这里面遇到的一个问题是如何得到a和b?一开始还在纠结,后来想了想其实一遍dfs就行了。。。


A. New Year Transportation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.

So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell(i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.

Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.

Input

The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.

The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

Output

If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".

Sample test(s)
input
8 4
1 2 1 2 1 2 1
output
YES
input
8 5
1 2 1 2 1 1 1
output
NO
Note

In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.

In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.


#include<queue>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;  
#define ll  long long
int n,t,x;
vector<int>v[1000000];
bool b[1000000];
void dfs(int u){
	for(int i=0;i<v[u].size();i++){
		int vv=v[u][i];
		if(!b[vv]){
			b[vv]=1;
			dfs(vv);
		}
	}
}
int main()
{
	cin>>n>>t;
	for(int i=1;i<n;i++)
	{
		cin>>x;
		v[i].push_back(i+x);
	}
	b[1]=1;
	dfs(1);
	if(b[t])cout<<"YES\n";
	else cout<<"NO\n";
}


B. New Year Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.

Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.

As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ ni ≠ j) if and only if Ai, j = 1.

Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

Input

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p.

The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation.

Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th lineAi, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ nAi, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ nAi, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Sample test(s)
input
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000
output
1 2 4 3 6 7 5
input
5
4 2 1 5 3
00100
00011
10010
01101
01010
output
1 2 3 4 5
#include<queue>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;  
#define ll  long long
int n;
int a[301],fa[301];
int temp[301],p,ans[301];
bool vis[301];
int b[301][301];
char c;
vector<int>v[301];
int Find(int u){
	if(u==fa[u])return u;
	return fa[u]=Find(fa[u]);
}
void Union(int x,int y){
	int fx=Find(x),fy=Find(y);
	if(fx==fy)return;
	fa[fx]=fy;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i],fa[i]=i;
	for(int i=1;i<=n;i++){
		getchar();
		for(int j=1;j<=n;j++)
			cin>>c,b[i][j]=c-'0';
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)if(b[i][j]){
			Union(i,j);
		}
	for(int i=1;i<=n;i++){
		int f=Find(i);
		v[f].push_back(i);
		vis[f]=1;
	}
	for(int i=1;i<=n;i++)
		if(vis[i]){
			p=0;
			for(int j=0;j<v[i].size();j++)
				temp[p++]=a[v[i][j]];
			sort(temp,temp+p);
			for(int j=0;j<v[i].size();j++)
				ans[v[i][j]]=temp[j];
		}
	for(int i=1;i<=n;i++)
		cout<<ans[i]<<" ";
	cout<<endl;
}


C. New Year Book Reading
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.

As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.

  1. He lifts all the books above book x.
  2. He pushes book x out of the stack.
  3. He puts down the lifted books without changing their order.
  4. After reading book x, he puts book x on the top of the stack.

He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.

After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?

Input

The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books.

The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.

Output

Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.

Sample test(s)
input
3 5
1 2 3
1 3 2 3 1
output
12
Note

Here's a picture depicting the example. Each vertical column presents the stacked books.


#include<queue>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;  
#define ll  long long
int n,m;
int a[501],b[1001];
int p[501];
bool f[501];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	int id=0,ans=0;
	for(int j=1;j<=m;j++){
		cin>>b[j];
		if(!f[b[j]])f[b[j]]=1,p[++id]=b[j];
	}
	for(int i=1;i<=m;i++){
		int j;
		for(j=1;j<=id;j++){
			if(p[j]==b[i])break;
			ans+=a[p[j]];
		}
		for(int k=j;k>=2;k--)
			p[k]=p[k-1];
		p[1]=b[i];
	}
	cout<<ans<<endl;
}


D. New Year Santa Network
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by integers from 1 to n - 1. Let's define d(u, v) as total length of roads on the path between city u and city v.

As an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the i-th year, the length of the ri-th road is going to become wi, which is shorter than its length before. Assume that the current year is year 1.

Three Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities c1c2c3 and make exactly one warehouse in each city. The k-th (1 ≤ k ≤ 3) Santa will take charge of the warehouse in city ck.

It is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to d(c1, c2) + d(c2, c3) + d(c3, c1) dollars. Santas are too busy to find the best place, so they decided to choose c1, c2, c3randomly uniformly over all triples of distinct numbers from 1 to n. Santas would like to know the expected value of the cost needed to build the network.

However, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value.

Input

The first line contains an integer n (3 ≤ n ≤ 105) — the number of cities in Tree World.

Next n - 1 lines describe the roads. The i-th line of them (1 ≤ i ≤ n - 1) contains three space-separated integers aibili (1 ≤ ai, bi ≤ nai ≠ bi,1 ≤ li ≤ 103), denoting that the i-th road connects cities ai and bi, and the length of i-th road is li.

The next line contains an integer q (1 ≤ q ≤ 105) — the number of road length changes.

Next q lines describe the length changes. The j-th line of them (1 ≤ j ≤ q) contains two space-separated integers rjwj (1 ≤ rj ≤ n - 11 ≤ wj ≤ 103). It means that in the j-th repair, the length of the rj-th road becomes wj. It is guaranteed that wj is smaller than the current length of the rj-th road. The same road can be repaired several times.

Output

Output q numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10 - 6.

Sample test(s)
input
3
2 3 5
1 3 3
5
1 4
2 2
1 2
2 1
1 1
output
14.0000000000
12.0000000000
8.0000000000
6.0000000000
4.0000000000
input
6
1 5 3
5 3 2
6 1 7
1 4 4
5 2 3
5
1 2
2 1
3 5
4 1
5 2
output
19.6000000000
18.6000000000
16.6000000000
13.6000000000
12.6000000000
Note

Consider the first sample. There are 6 triples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because n = 3, the cost needed to build the network is always d(1, 2) + d(2, 3) + d(3, 1) for all the triples. So, the expected cost equals to d(1, 2) + d(2, 3) + d(3, 1).


#include<queue>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;  
#define ll  long long
struct edge{
	int x,y,w;
}nd[100009];
vector<int>ed[100009];
int n,m;
int num[100009];
int dfs(int u,int fa){
	int ans=1;
	for(int i=0;i<ed[u].size();i++){
		edge e=nd[ed[u][i]];
		int v;
		if(e.x==u)v=e.y;else v=e.x;
		if(v==fa)continue;
		int f=dfs(v,u);
		num[ed[u][i]]=f;
		ans+=f;
	}
	return ans;
}
double cnt(int x){
	if(x==1||x==n-1)return 6.0/n;
	return 6.0*x*(n-x)/n/(n-1);
}
int main()
{
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>nd[i].x>>nd[i].y>>nd[i].w;
		ed[nd[i].x].push_back(i);
		ed[nd[i].y].push_back(i);
	}
	dfs(1,-1);
	double an=0;
	for(int i=1;i<n;i++){
		an+=cnt(num[i])*nd[i].w;
	}
	cin>>m;
	int p,q;
	for(int i=1;i<=m;i++){
		cin>>p>>q;
		an=an+cnt(num[p])*(q-nd[p].w);
		nd[p].w=q;
		printf("%.10lf\n",an);
	}
}




资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间和笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度和加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置和姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数和工具箱: 优化工具箱:用于解决运动学逆解和路径规划中的优化问题。 Simulink:可视化建模环境,适合构建和仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程和轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间和笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值