2021年度训练联盟热身训练赛第一场 (除G,K外所有)

传送门

A Weird Flecks, But OK

An artist who wanted to create an installation where his works appeared to be floating in midair has cast a large cube of clear acrylic to serve as a base. Unfortunately, during the casting, some small flecks of dirt got into the mix, and now appear as a cluster of pinpoint flaws in the otherwise clear cube.

He wants to drill out the portion of the cube containing the flaws so that he can plug the removed volume with new, clear acrylic. He would prefer to do this in one drilling step. For stability’s sake, the drill must enter the cube only once, perpendicular to one of its faces. The cube’s faces are parallel to the coordinate axes.

Given the (x,y,z) positions of the flaws, and treating the size of the flaws as negligible, what is the smallest diameter drill bit that can be used to remove the flaws in one operation??

输入描述:
The first line of input contains an integer N denoting the number of flaws. 3≤N≤5000

This is followed by N lines of input, each containing three real numbers in the range −1000.0…1000.0, denoting the (x,y,z) coordinates of a single flaw. Each number contains at most 6 digits following a decimal point. The decimal point may be omitted if all succeeding digits are zero.

输出描述:
Print the diameter of the smallest drill bit that would remove all the flaws.

The answer is considered correct if the absolute or relative error is less than 10-4

示例1
输入
复制
3
1.0 0.0 1.4
-1.0 0.0 -1.4
0.0 1.0 -0.2
输出
复制
2.0000000000
示例2
输入
复制
5
1.4 1.0 0.0
-0.4 -1.0 0.0
-0.1 -0.25 -0.5
-1.2 0.0 0.9
0.2 0.5 0.5
输出
复制
2.0000000000
示例3
输入
复制
8
435.249 -494.71 -539.356
455.823 -507.454 -539.257
423.394 -520.682 -538.858
446.507 -501.953 -539.37
434.266 -503.664 -560.631
445.059 -549.71 -537.501
449.65 -506.637 -513.778
456.05 -499.715 -561.329
输出
复制
49.9998293198

感谢C大佬提供的思路,关键在于如何随机化,然后就是数学问题,随意选择两点求得当前直径及中点,然后与将中点作为一个新的点与其它点比对,若大于半径,则更新半径,时间复杂度O(n3

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define M 1223456789
#define pi acos(-1.0)
using namespace std;
mt19937 rnd(time(0));
const int mod=1e9+7;
//const int M=5e8;
const int N=2*1e4+5;//?????????? 4e8
double w[5][N];
int n;
double ans=1e15;
double qr(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double find(double x,double y,int u,int v)
{
	double cnt=0;
	for(int i=1;i<=n;i++)  cnt=max(cnt,qr(x,y,w[u][i],w[v][i]));
	return cnt;
}
void get(int s,int r)
{
	double tx=0,ty=0,ts=find(tx,ty,s,r);
	double ex,ey,es;
	double sz=4000.0;
	for(int i=1;i<=10000;i++)
	{
		unsigned d=rnd();
		double v=(double)d/(double)M*pi*2.0;
//		随机化
		ex=tx+cos(v)*sz;
        ey=ty+sin(v)*sz;
        es=find(ex,ey,s,r);
        if (ts>es)  {tx=ex;ty=ey;ts=es;}
        sz=sz*0.995;
	}
	ans=min(ans,ts);
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  for(int j=1;j<=3;j++)  scanf("%lf",&w[j][i]);
	get(1,3);get(1,2);get(2,3);
	printf("%.9lf\n",ans*2.0);
}
signed main()
{
    solve();
    return 0;
}

B Code Names

You are given W, a set of N words that are anagrams of each other. There are no duplicate letters in any word. A set of words S⊆W is called “swap-free” if there is no way to turn a word x∈S into another word y∈S by swapping only a single pair of (not necessarily adjacent) letters in x. Find the size of the largest swap-free set S chosen from the given set W.

The first line of input contains an integer N (1\le N\le5001≤N≤500). Following that are N lines each with a single word. Every word contains only lowercase English letters and no duplicate letters. All N words are unique, have at least one letter, and every word is an anagram of every other word.

输出描述:
Output the size of the largest swap-free set.
示例1
输入
复制
6
abc
acb
cab
cba
bac
bca
输出
复制
3
示例2
输入
复制
11
alerts
alters
artels
estral
laster
ratels
salter
slater
staler
stelar
talers
输出
复制
8
示例3
输入
复制
6
ates
east
eats
etas
sate
teas
输出
复制
4

吐了,最大独立点集的题,比赛的时候不是忘了建无向边,就是建了无向边没有除以二

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
//#define int long long
//#deine inf 0x7f
#define eps 1e-8
//#define double long double
using namespace std;
const int mod=1e9+7;
const int M=1e7+5;
const int N=1e5+5;
int n;
string s[N];
int ans;
struct node
{
    int ver,next;
}e[N];
int tot,head[N];
int match[N],v[N];
void add(int x,int y)
{
    e[++tot].ver=y;
    e[tot].next=head[x];
    head[x]=tot;
}
bool dfs(int x)
{
    for(int i=head[x];i;i=e[i].next)
    {
        int y=e[i].ver;
        if(v[y])  continue;
        v[y]=1;
        if(!match[y]||dfs(match[y]))
        {
            match[y]=x;
            return 1;
        }
    }
    return 0;
}
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)  cin>>s[i];
    int sz=s[1].size();
    for(int i=1;i<=n;i++)  for(int j=i+1;j<=n;j++)  
		{
			int cnt=0;
			for(int k=0;k<sz;k++)  if(s[i][k]!=s[j][k])  cnt++;
        	if(cnt==2)  add(i,j),add(j,i);
		}
    for(int j=1;j<=n;j++)
    {
        memset(v,0,sizeof(v));
        if(dfs(j))  ans++;
    }
    cout<<n-ans/2<<endl;
}
signed main()
{
    solve();
    return 0;
}

C New Maths

‘Dratl cursed Chares. This stupid cary bar is not working in my Enginel ljust tied to calculate the square of a number,butitswrong al of the caies are lot*"Hmm " mused Ada, "arithmetic without cariesll wonder if can figure out what your original input was, based on the resultl see on the Engine
Carryless adition,denoted by e, is the same as normal addition, except any carries are ignored (in base 10).Thus,37e48 is 75,not 85.
Caryless muiplic ton,denoted by 8, is performed using the schooboy algorithm for mutiplication, column by column, but the intermediate aditions are
calculated using caryless aditon.More fomally Let am4m-1…a ay be the digis of a,where ag is is least significant digit Similaly define b b。-1 …ybybe the digits of b.The digits of c=ab are given by the following equation:
= (aobxD a1bk—1D··ak-1b1agbo mod 10,
where any a; orb isconsidered zero firm or >n.For example, 981234 is 98769876,9081234 is 9876098760, and 9981234 is 9753697536.Given N, find the smallest positive integer aa such that aca=N.

输入描述:
The input consists of a single line with a positive integer N, with at most 25 digits and no leading zeros.
输出描述:
Print, on a single line, the least positive number aa such that a⊗a=N. If there is no such a, print ‘-1’ instead.
示例1
输入
复制
6
输出
复制
4
示例2
输入
复制
149
输出
复制
17
示例3
输入
复制
123476544
输出
复制
11112
示例4
输入
复制
15
输出
复制
-1
(排版有点问题,链接

定义新运算,无进位乘法,且数据量较小,dfs连剪枝都如此简单,就是高精度乘法的2.0版,可以比赛的时候看着道题的时间只剩下8分钟了

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e8;
const int N=2*1e5+5;//?????????? 4e8
string s;
int ans=1e18;
int a[N],po[N],d[N],c[N];
int m,n;
void get()
{
    for(int i=0;i<n;i++)  d[i]=0;
    for(int i=0;i<m;i++)  for(int j=0;j<m;j++)  d[i+j]+=c[i]*c[j];
    for(int i=0;i<n;i++)  if(d[i]%10!=a[i])  return;
    int res=0;
//    for(int i=0;i<m;i++)  cout<<c[i];
//    cout<<endl;
    for(int i=0;i<m;i++)  res+=po[i]*c[i];
    ans=min(res,ans);
//    cout<<res<<endl;
}
bool check(int pos)
{
    for(int i=0;i<pos;i++)  d[i]=0;
    for(int i=0;i<pos;i++)  for(int j=0;j<i+1;j++)  d[i]+=c[i-j]*c[j];
    for(int i=0;i<pos;i++)  if(d[i]%10!=a[i])  return 0;
    return 1;
}
void dfs(int pos)
{
    if(pos>=m)
    {
        get();
        return;
    }
    for(int i=0;i<=9;i++)
    {
        c[pos]=i;
        if(check(pos+1))  dfs(pos+1);
    }
}
void solve()
{
    cin>>s;
    int n=s.size();
    for(int i=0;i<n;i++)  a[i]=s[i]-'0';
    reverse(a,a+n);
    if(~n&1)
    {
        puts("-1");
        return;
    }
    m=(n+1)>>1;
    po[0]=1;
    for(int i=1;i<15;i++)  po[i]=po[i-1]*10;
    dfs(0);
    if(ans==1e18)  puts("-1");
    cout<<ans<<endl;
}
signed main()
{
    solve();
    return 0;
}

D Some Sum

Your friend has secretly picked N consecutive positive
integers between 1 and 100, and wants you to guess if their sum is
even or odd.
If the sum must be even, output ‘{\tt Even}Even’. If the sum must be odd, output ‘{\tt Odd}Odd’. If the sum could be even or could be odd,
output ‘{\tt Either}Either’.
输入描述:
The input is a single integer N with 1 \le N \le 101≤N≤10.
输出描述:
Output a single word. The word should be ‘{\tt Even}Even’, ‘{\tt Odd}Odd’, or
‘{\tt Either}Either’, according to the rules given earlier.
示例1
输入
复制
1
输出
复制
Either
示例2
输入
复制
2
输出
复制
Odd

思维题,找下规律就行了,就不解释了

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
//#define int long long
//#deine inf 0x7f
#define eps 1e-8
//#define double long double
using namespace std;
const int mod=1e9+7;
const int M=1e7+5;
const int N=1e5+5;
int n,m;
void solve()
{
    cin>>n;
    if(n&1)  puts("Either");
    else
    {
        n>>=1;
        if(~n&1)  puts("Even");
        else  puts("Odd");
    }
}
signed main()
{
    solve();
    return 0;
}

E Early Orders

You are given a list of integers w1, 2,. . . ,n and a number k.
lt is guaranteed that each i from 1 to k appears in the list at least once.
Find the lexicographically smallest subsequence of x that containseach integer from 1 to k exactly once.
输入描述:
The first line will contain two integers n and k, with1≤k <n ≤200 000.
The following n lines will each contain an integer i with1≤i ≤k .

输出描述:
Write out on one line, separated by spaces, the lexicographically smallest
subsequence of x that has each integer from 1 to k exactly once.
示例1
输入
复制
6 3
3
2
1
3
1
3
输出
复制
2 1 3
示例2
输入
复制
10 5
5
4
3
2
1
4
1
1
5
5
输出
复制
3 2 1 4 5

C大佬,给我提供了个单调栈的做法,tql

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e8;
const int N=2*1e5+5;//?????????? 4e8
int n,k,top;
int a[N],instack[N],v[N];
map < int , int > mp;
void solve()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)  scanf("%d",&a[i]),mp[a[i]]++;
	for(int i=1;i<=n;i++)
	{
		mp[a[i]]--;
		if(!v[a[i]])  while(top&&instack[top]>=a[i]&&mp[instack[top]])  v[instack[top--]]=0;
		if(!v[a[i]]) instack[++top]=a[i],v[a[i]]=1;
	}
	for(int i=1;i<=k;i++)  printf("%d ",instack[i]);
}
signed main()
{
    solve();
    return 0;
}

F Pulling Their Weight

To save money, Santa Claus has started hiring other animals besides reindeer to
pull his sleigh via short term ‘gig’ contracts. As a result, the actual
animals that show up to pull his sleigh for any given trip can vary greatly in
size.

Last week he had 2 buffalo, 37 voles and a schnauzer. Unfortunately, both
buffalo were hitched on the left side and the entire sleigh flipped over in
mid-flight due to the weight imbalance.

To prevent such accidents in the future, Santa needs to divide the animals for
a given trip into two groups such that the sum of the weights of all animals in
one group equals the sum of the weights of all animals in the other. To make
the hitching process efficient, Santa is seeking an integer target weight t
such that all animals that are lighter than t go in one group and those
heavier than t go into the other. If there are multiple such t, he wants
the smallest one. There’s one small wrinkle: what should be done if there some
animals have weight exactly equal to t? Santa solves the problem this way: if
there are an even number of such animals, he divides them equally among the two
groups (thus distributing the weight evenly). But if there are an odd number of
such animals, then one of those animals is sent to work with the elves to make
toys (it is not put in either group), and the remaining (now an even number)
are divided evenly among the two groups.
输入描述:

Input describes a list of animals ’ weights. The first line contains an integerm (2< m<10’), indicating the number of animals. The next m lines
each have a positive integer. These are the weights of the animals (in ounces) .Animals weighing more than 20 000 ounces are too big to pull the sleigh so
no given weight will exceed this maximum .

输出描述:
Output the smallest integer target weight t, as described above. It’s
guaranteed that it is possible to find such an integer.
示例1
输入
复制
4
3
6
1
2
输出
复制
4
示例2
输入
复制
4
11
8
3
10
输出
复制
10
示例3
输入
复制
2
99
99
输出
复制
99

这道题我先离散化了一下,然后求了个前缀和,后缀和,然后二分了一下,再分类讨论了一下,就过了

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
//#define int long long
//#deine inf 0x7f
#define eps 1e-8
//#define double long double
using namespace std;
const int mod=1e9+7;
const int M=1e7+5;
const int N=1e6+5;
int n,t,m,t1;
int a[N],sum[N],pre[N];
map < int , int > mp;
int cnt,ans,ret,res;
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)  scanf("%d",&a[i]),mp[a[i]]++;
    sort(a+1,a+n+1);
    n=unique(a+1,a+n+1)-(a+1);
    for(int i=n;i>=1;i--)  pre[i]=pre[i+1]+a[i]*mp[a[i]];
    for(int i=1;i<=n;i++)  sum[i]=sum[i-1]+a[i]*mp[a[i]];
    cnt=sum[n]/2;
    t=lower_bound(sum+1,sum+n+1,cnt)-sum;
    int ll=sum[t-1],rr=pre[t+1];
    if(ll==rr)
    {
        ans=a[t];
    }
    else if(ll<rr)
    {
        ans=a[t]+1;
    }
    else
    {
        if(ll>=rr+a[t]*mp[a[t]])    ans=a[t-1]+1;
        else  ans=a[t]+1;
    }
    cout<<ans<<endl;
}
signed main()
{
    solve();
    return 0;
}

G Birthday Paradox

The probability of n unique birthdays among n people.
The Birthday Paradox is the name given to the surprising fact that if there arejust 23 people in a group, there is a greater than50% chance that a pair of them share the same birthday. The underlying assumptions for this are that all birthdays are equallylikely (which isn’t quite true), the year has exactly 365 days (which also isnt true), and the people in the group are uniformlyrandomly selected (which is a somewhat strange premise).For this problem, we’ll accept these assumptions.
Consider what we might observe if we randomly select groups of P=10 people. Once we have chosen a group, we break themup into subgroups based on shared birthdays. Among many other possibilities, we might observe the following distributions ofshared birthdays:
. all 10 have different birthdays, or. all 10 have the same birthday, or
3 people have the same birthday, 2 other people have the same birthday (on a different day), and the remaining 5 al havedifferent birthdays.
Of course, these distributions have different probabilities of occurring.
Your job is to calculate this probability for a given distribution of people sharing birthdays. That is, if there are P people in agroup, how probable is the given distribution of shared birthdays (among all possible distributions for P people chosen uniformlyat random)?
输入描述:
The first line gives a number n where 1<n <365. The second line contain integers c1
through Cn, where 1sc<100 for all c;. The value C; represents the number of people whoshare a certain birthday (and whose birthday is distinct from the birthdays of everyone elsein the group) .
输出描述:
Compute the probability b of observing a group of people with the given distribution ofshared birthdays. Since b may be quite small,output instead log1o(b). Your submission ’ s
answer is considered correct if it has an absolute or relative error of at most10from thejudge ’ s answer.

示例1
输入
复制
2
1 1
输出
复制
-0.001191480807419
示例2
输入
复制
7
1 1 2 1 3 1 1
输出
复制
-4.310614508857128

The first sample case shows P=2 people with distinct birthdays. Theprobability of this occurring is b =364/365 ~ 0.9972602740,
and log10(b)~—0.001191480807419.
The second sample case represents the third example in the list given earlierwith P=10 people. In this case, the probability is b e 0.0000489086,
and log10(b)—4.310614508857128.

H On Average They’re Purple

Alice and Bob are playing a game on a simple connected graph with N nodes and M edges.

Alice colors each edge in the graph red or blue.

A path is a sequence of edges where each pair of consecutive edges have a node in common. If the first edge in the pair is of a different color than the second edge, then that is a ‘‘color change.’’

After Alice colors the graph, Bob chooses a path that begins at node 1 and ends at node N. He can choose any path on the graph, but he wants to minimize the number of color changes in the path. Alice wants to choose an edge coloring to maximize the number of color changes Bob must make. What is the maximum number of color changes she can force Bob to make, regardless of which path he chooses? changes she can force Bob to make, regardless of which path he chooses?
输入描述:
The first line contains two integer values N and M with 2<N <100000 and 1< M ≤<100000.The next M lines contain two integers ; and b; indicating an undirected edge between nodes .iand bi (1 ≤ ai,bi ≤ N,ai≠ bi ) .
All edges in the graph are unique .

输出描述:
Output the maximum number of color changes Alice can force Bob to make on his route from node 1 to node N.
示例1
输入
复制
3 3
1 3
1 2
2 3
输出
复制
0
示例2
输入
复制
7 8
1 2
1 3
2 4
3 4
4 5
4 6
5 7
6 7
输出
复制
3

这道题看似复杂,实则就是,边权为一的最短路

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
//#define int long long
//#deine inf 0x7f
#define eps 1e-8
//#define double long double
using namespace std;
const int mod=1e9+7;
const int M=1e7+5;
const int N=1e6+5;
int n,m;
struct node
{
    int ver,edge,next;
}e[N];
int tot=1,head[N];
int v[N],d[N];
priority_queue < pair < int, int > > q;
void add(int x,int y,int z)
{
    e[++tot].ver=y;
    e[tot].edge=z;
    e[tot].next=head[x];
    head[x]=tot;
}
void addedge(int x,int y,int z)
{
    add(x,y,z),add(y,x,z);
}
void dijkstra(int s)
{
    q.push(make_pair(0,s));
    memset(d,0x3f,sizeof(d));
    d[s]=0;
    while(q.size())
    {
        int x=q.top().second;
        q.pop();
        if(v[x])  continue;
        v[x]=1;
        for(int i=head[x];i;i=e[i].next)
        {
            int y=e[i].ver;
            int z=e[i].edge;
            if(d[y]>d[x]+z)
            {
                d[y]=d[x]+z;
                q.push(make_pair(-d[y],y));
            }
        }
    }
}
void solve()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d",&x,&y);
        addedge(x,y,1);
    }
    dijkstra(1);
    cout<<d[n]-1<<endl;
}
signed main()
{
    solve();
    return 0;
}

I Full Depth Morning Show

All boring tree-shaped lands are alike, while all exciting tree-shaped lands are exciting in their own special ways. What makesTreeland more exciting than the other tree-shaped lands are the raddest radio hosts in the local area: Root and Leaf.Everymorning on FM 32.33 (repeating of course), Root and Leaf of The Full epth Morning Show serve up the hottest celebrity gossipand traffic updates.
The region of Treeland is made of n cCities, connected by n- 1 roads such that between every pair of cities there is exactly onesimple path. The i-th road connects cities u; and v; , and has a toll of w ; .
To reward their loyal listeners, The Full Depth Morning Show is giving away a number of travel packagesl Root and Leaf willchoose n- 1 lucky residents from the city that sends them the most fan mail. Each of those residents then gets a distinct ticket toa different city in Treeland.
Each city in Treeland has its own tax on prizes:t; . Let du,t be the sum of the tolls on each road on the only simple path fromcity u to v. For a trip from city u to city v, the cost of that trip is then (tu + ty)du,v-

The map of Treeland corresponding to the first sample input.

The shock jocks haven’t quite thought through how much their prize is worth. They need to prepare a report to the radio executives, to summarize the expected costs. For each city that could win the prize, what is the total cost of purchasing all the tickets?
输入描述:

The first line of input is a single integer n (1<n < 100 000). The next line has n space-separated integers t; (1< t; <1000), the tax in each city. The following n - 1 lines eachhave 3 integers,Li,0i;, ui, meaning the i-th road connects cities u; and v; (1 ≤u;,0n),with a toll of w;(1 ≤w;≤1000) .

输出描述:
Output n lines. On the i-th line, output a single integer: the cost of purchasing tickets if city i wins the contest.
示例1
输入
复制
5
2 5 3 4 1
1 2 2
2 4 5
4 3 3
5 2 6
输出
复制
130
159
191
163
171
示例2
输入
复制
6
4 3 3 4 3 3
1 3 2
2 1 1
1 4 6
4 5 6
6 4 2
输出
复制
209
206
232
209
336
232

这道题因为比赛时没什么思路就放弃了,后来C大佬告诉我这是换根dp

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
using namespace std;
const int mod=1e9+7;
const int M=2e3+5;
const int N=2*1e7+5;//?????????? 4e8
int n;
struct node
{
	int ver,edge,next;
}e[N];
int tot,head[N];
int d[N],sz[N];
int ans[N];
int a[N]; 
int f1[N],f2[N];
void add(int x,int y,int z)
{
	e[++tot].ver=y;
	e[tot].edge=z;
	e[tot].next=head[x];
	head[x]=tot;
}
void addedge(int x,int y,int z)
{
	add(x,y,z);add(y,x,z);
}
void dfs1(int x,int pre)
{
	sz[x]=1,d[x]=a[x];
	for(int i=head[x];i;i=e[i].next)
	{
		int y=e[i].ver;
		int z=e[i].edge;
		if(y==pre)  continue;
		dfs1(y,x);
		f1[x]+=f1[y]+z*d[y];
		f2[x]+=f2[y]+z*sz[y];
		sz[x]+=sz[y];
		d[x]+=d[y];
	}
}
void dfs2(int x,int pre)
{
	ans[x]=f1[x]+a[x]*f2[x];
	for(int i=head[x];i;i=e[i].next)
	{
		int y=e[i].ver;
		int z=e[i].edge;
		if(y==pre)  continue;
		int ysz=sz[y],yd=d[y],yf1=f1[y],yf2=f2[y];
		f1[y]+=(d[x]-d[y])*z+f1[x]-d[y]*z-f1[y];
		f2[y]+=(sz[x]-sz[y])*z+f2[x]-sz[y]*z-f2[y];
		sz[y]=sz[x],d[y]=d[x];
		dfs2(y,x);
		sz[y]=ysz,d[y]=yd,f1[y]=yf1,f2[y]=yf2;
	}
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  scanf("%lld",&a[i]);
	for(int i=1;i<n;i++)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		addedge(x,y,z);
	}
	dfs1(1,1);
	dfs2(1,1);
	for(int i=1;i<=n;i++)  printf("%lld\n",ans[i]);
}
signed main()
{
    solve();
    return 0;
}

J This Ain’t Your Grandpa’s Checkerboard

You are given an n−by−n grid where each square is colored either black or white. A grid is correct if all of the following conditions are satisfied:

Every row has the same number of black squares as it has white squares.

Every column has the same number of black squares as it has white squares.

No row or column has 3 or more consecutive squares of the same color.

Given a grid, determine whether it is correct.

输入描述:
The first line contains an integer n(2≤n≤24; n is even ) . Each of the next n lines contains a string of length n consisting solely of the characters ‘B’ and ‘W’, representing the colors of the grid squares.
输出描述:
If the grid iscorrect, print the number 1 on a single line. Otherwise, print the number 0 on a single line.
示例1
输入
复制
4
WBBW
WBWB
BWWB
BWBW
输出
复制
1
示例2
输入
复制
4
BWWB
BWBB
WBBW
WBWW
输出
复制
0
示例3
输入
复制
6
BWBWWB
WBWBWB
WBBWBW
BBWBWW
BWWBBW
WWBWBB
输出
复制
0
示例4
输入
复制
6
WWBBWB
BBWWBW
WBWBWB
BWBWBW
BWBBWW
WBWWBB
输出
复制
1

简单模拟题,就不解释了

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
//#define int long long
//#deine inf 0x7f
#define eps 1e-8
//#define double long double
using namespace std;
const int mod=1e9+7;
const int M=1e7+5;
const int N=1e4+5;
int n;
int a[N][N];
bool check()
{
    for(int i=1;i<=n;i++)
    {
        int cnt=0,ans=0;
        for(int j=1;j<=n;j++)
            if(a[i][j])  cnt++;
            else ans++;
        if(ans!=cnt)  return 0;
        cnt=0,ans=0;
        for(int j=1;j<=n;j++)
            if(a[j][i])  cnt++;
            else ans++;
        if(ans!=cnt)  return 0;
    }
    for(int i=1;i<=n;i++)
    {
        int cnt=-1,ans=0;
        for(int j=1;j<=n;j++)
            if(a[i][j]==cnt)
            {
                ans++;
                if(ans>=3)  return 0;
            }
            else  cnt=a[i][j],ans=1;
        cnt=-1,ans=0;
        for(int j=1;j<=n;j++)
            if(a[j][i]==cnt)
            {
                ans++;
                if(ans>=3)  return 0;
            }
            else  cnt=a[j][i],ans=1;

    }
    return 1;
}
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)  for(int j=1;j<=n;j++)
    {
        char c;
        cin>>c;
        if(c=='W')  a[i][j]=1;
    }
    if(check())  puts("1");
    else  puts("0");
}
signed main()
{
    solve();
    return 0;
}

K Solar Energy

You are planning to travel in interstellar space in the hope of finding habitable planets. You have already identfied N stars thatcan recharge your spaceship via its solar panels. The only work left is to decide the orientation of the
spaceship that maximizes the distance it can travel.
Space is modeled as a 2D plane, with the Earth at the origin. The spaceship can be launched fromthe Earth in a straight line, in any direction. Star i can provide enough energy to travel T distance ifthe spaceship is launched at an angle of a; with the x-axis. lf the angle is not perfectly aligned, thenthe spaceship gets less energy.Specifically, if the launch direction makes an angle of a with the x-axis, then it gets enough energy to travel distance of
max(0,T-sjdist(a,a))
from star i, where dist(a,b) is the minimum radians needed to go from angle a to b. The distance that
the spaceship can travel is simply the sum of the distances that each star contributes. Find the maximum distance T that thestarship can travel.
输入描述:
The first line contains the value N 1SNS105. Following this are N lines each containing threereal numbers Ti, si, and ai, with 0<T,S1000,0Ss,S100,and 0Saj<2n.All real numbers in theinput have at most 6 digits after the decimal point.
输出描述:
on a single line output the maximum distance the spacecraft can travel. Your answer isconsidered correct if it has an absolute or relative error of at most 10-6.

示例1
输入
复制
2
100 1 1
100 1 1.5
输出
复制
199.500000
示例2
输入
复制
4
100 1 0.5
200 1 1
100 0.5 1.5
10 2 3
输出
复制
405.500000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值