B. New Year Permutation
用floyd算出每个点能够到达的所有点
然后再在没有用过的点中找一个最小的放过去
// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1; char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
//-----------------------------------
int n;
int dis[310][310];
int vis[310];
int a[310];
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(dis[i][k]&&dis[k][j])
{
dis[i][j]=1;
}
}
}
}
}
int main()
{
read(n);
for(int i=1;i<=n;i++)
read(a[i]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%1d",&dis[i][j]);
dis[i][i]=1;
}
floyd();
for(int i=1;i<=n;i++)
{
int minn=a[i],po=i;
for(int j=1;j<=n;j++)
{
if(dis[i][j] && !vis[a[j]] && minn>a[j])
{
minn=a[j];
po=j;
}
}
vis[minn]=1;
swap(a[i],a[po]);
}
for(int i=1;i<=n;i++)
write(a[i]),putchar(' ');
putchar('\n');
return 0;
}
C. New Year Book Reading
贪心
每次书第一次出现就将它放在stack的最底下,然后向上移动
<pre name="code" class="cpp">// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
//-----------------------------------
bool used[510];
int sta[510], w[510], nowpos[510];
int pop(int pos)
{
int ans = 0;
int tmp = sta[pos];
for(int i = pos-1; i >= 0; i--)
{
ans += w[sta[i]];
sta[i+1] = sta[i];
nowpos[sta[i+1]] = i+1;
}
sta[0] = tmp;
nowpos[tmp] = 0;
return ans;
}
int main()
{
int n, m;
read(n), read(m);
for(int i = 0; i < n; i++)
read(w[i]);
int ans = 0;
for(int i = 0, tmp, len = 0; i < m; i++)
{
read(tmp);
--tmp;
if(!used[tmp])
{
sta[len++] = tmp, used[tmp] = true;
ans += pop(len-1);
}
else
ans += pop(nowpos[tmp]);
}
printf("%d\n", ans);
return 0;
}
D. New Year Santa Network
dfs维护每个节点共有多少个子孙
然后计算出一条边两侧的节点,v 和 n-v ,第三个节点便可以随便选取
一条边被利用的次数是v*(n-v)*(n-2)
再乘上其长度,再除以C(n,3),则是每条边对期望值的贡献率
// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
//-----------------------------------
const int MAXN=100010;
int n, m;
int a[MAXN], b[MAXN], len[MAXN], cnt[MAXN];
vector<int> g[MAXN];
int dfs(int u, int fa)
{
cnt[u] = 1;
for(int i = 0; i < g[u].size(); i++ )
{
if(g[u][i] == fa)
continue;
dfs(g[u][i], u);
cnt[u] += cnt[g[u][i]];
}
return cnt[u];
}
int main()
{
while(read(n))
{
for(int i = 0; i < n; ++ i)
g[i].clear();
for(int i = 0; i < n-1; i++ )
{
read(a[i]),read(b[i]), read(len[i]);
a[i] --;
b[i] --;
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
}
dfs(0, -1);
double C[MAXN];
for(int i = 0; i < n-1; i++ )
{
long long v = min(cnt[a[i]], cnt[b[i]]);
C[i] = (double)v*(n-v)*(n-2);
}
double res = 0;
for(int i = 0; i < n-1; i++ )
{
res += C[i]*len[i];
}
double x = (double)n*(n-1)*(n-2)/6;
read(m);
for(int i = 0; i < m; i++ )
{
int r, w;
read(r),read(w);
r --;
res -= C[r]*(len[r]-w);
len[r] = w;
printf("%.10lf\n", res/x);
}
}
return 0;
}