2021年度训练联盟热身训练赛第三场

传送门

A Circuit Math

题目描述
You are enrolled in the Computer Organization and Architecture course at your university. You decide to write a program to help check your work by computing the output value of a combinational digital circuit, given its inputs.

Consider the circuit shown in Figure A.1, which we use for illustration. This circuit has four inputs (letters A through D on the left), each of which is either true or false. There are four ‘gates’ each of which is one of three types: AND, OR, or NOT. Each gate produces either a true or false value, depending on its inputs. The last gate (the OR on the right) produces the output of the entire circuit. We can write these three types of gates in text by their equivalent logical operators: * for AND, + for OR, and - for NOT. In what follows, we’ll use the operators rather than gates to describe circuits.

Here is how these operators work. Given an assignment of true (T) or false (F) for each input, the operators produce the truth value indicated in the following tables:

Notice that AND and OR take two inputs, whereas NOT operates on only one input. Also, we use postfix notation to write expressions involving operators (like A B *), where the operator comes after its input(s) (just as how in Figure A.1, each gate in the circuit diagram comes after its inputs).

When we describe a valid circuit in postfix notation, we use the following syntax.

An uppercase letter (A through Z) is a valid circuit. In other words, an input alone (without any gates) is a valid circuit (which produces as output its own input value).
If and are valid circuits, then ’ *’ is a valid circuit that produces the AND of the outputs of the two subcircuits.
If and are valid circuits, then ’ +’ is a valid circuit that produces the OR of the outputs of the two subcircuits.
If is a valid circuit, then ’ -’ is a valid circuit that produces the NOT of 's output.

No other description is a valid circuit.

Thus, one of the ways the circuit in Figure A.1 could be described using postfix notation is as the string:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~A~ B~ * C D~ +~ -~ + A B ∗ C D + − +

Given a truth value (T or F) for each of the inputs (A, B, C, and D in this example), their values propagate through the gates of the circuit, and the truth value produced by the last gate is the output of the circuit. For example, when the above circuit is given inputs A = T, B = F, C = T, D = F, the output of the circuit is F.

Given an assignment to variables and a circuit description, your software should print the output of the circuit.

输入描述:
The first line of the input consists of a single integer nn, satisfying 1 \leq n \leq 261≤n≤26, denoting the number of input variables. Then follows a line with nn space-separated characters. Each character is either TT or FF, with the i-th such character indicating the truth value of the input that is labeled with the i-th letter of the alphabet.

The last line of input contains a circuit description, which obeys the syntax described above. Each circuit is valid, uses only the first nn letters of the alphabet as input labels, and contains at least 11 and at most 250250 total non-space characters.

Note that while each variable is provided only one truth value, a variable may appear multiple times in the circuit description and serve as input to more than one gate.

输出描述:
Print a single character, the output of the circuit (either TT or FF), when evaluated using the given input values.
示例1
输入
复制
4
T F T F
A B * C D + - +
输出
复制
F

思路:求后缀表达式的值

#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=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
string s[N];
map < char , bool > mp;
int n,m=1; 
bool exp()
{
	m--;
//	if(m<1)  return ;
	if(s[m][0]=='+')  return exp()|exp();
	else if(s[m][0]=='*')  return exp()&exp();
	else if(s[m][0]=='-')  return 1-exp();
	else  return mp[s[m][0]];
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		char c;
		cin>>c;
		if(c=='F')  mp[i-1+'A']=0;
		else  mp[i-1+'A']=1;
	}
	while(cin>>s[m])  m++;
//	for(int i=1;i<=m;i++)  cout<<i<<" "<<s[i]<<endl;
//	cout<<m<<endl;
	bool flag=exp();
	if(flag)  cout<<"T";
	else  cout<<"F";
	puts("");  
}
signed main()
{
	int T=1;
//	cin>>T; 
	solve();
    return 0;
}

B Diagonal Cut

Quido and Hugo are making a chocolate cake. The central ingredient of the cake is a large chocolate bar, lying unwrapped on the kitchen table. The bar is an M \times NM×N rectangular grid of chocolate blocks. All of the MNMN blocks are rectangles of identical shape and size. The chocolate bar is of top quality and the friends want to eat part of it, before the rest is used in the cake.

“OK,” says Quido, "let’s divide the whole bar into two triangular chunks by a straight diagonal cut from its upper-left corner to its lower-right corner. We will then eat all of the blocks which have been cut exactly in half, into two equal-area pieces. You will eat one half and I will eat the other half of each such block. All other blocks, that is, the blocks which are either uncut or cut into two parts of different sizes, will go directly into the cake. Of course, we will make sure the cut is perfectly precise.

Let’s see how much chocolate we get to eat!"

找规律的题目

#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=4*1e5+5;//?????????? 4e8
int n,m;
void solve()
{
	cin>>n>>m;
	int d=__gcd(n,m);
	int c1=n/d,c2=m/d;
	if((~c1&1)&&(c2&1))  d=0;
	if((~c2&1)&&(c1&1))  d=0;
	cout<<d<<endl;
}
signed main()
{
//	ios::sync_with_stdio(false);
	int T=1;
//	cin>>T;
	while(T--)  solve(); 
	return 0;
}

C Gerrymandering

Electoral systems across the world can vary widely. In some systems, such as winner-take-all, the winner is determined by the plurality of votes——the candidate that receives the most votes wins, and the loser(s) do not get a position.

Such elections can have “wasted votes.” Conceptually, a wasted vote is a vote that did not affect the election outcome. While the exact definition of a wasted vote varies, we’ll adopt the following definition: in an election with VV voters, every vote for a losing candidate is wasted (these are called lost votes), and every vote for a winning candidate beyond the strict majority of \lfloor V/2 \rfloor + 1⌊V/2⌋+1 votes the candidate needs to win is wasted (these are called excess votes). For this problem we’ll consider a two-party system (let’s call the parties AA and BB) with elections that always involve one candidate from each party.

Let’s illustrate wasted votes with a simple example between two candidates in a district. Suppose that the candidate for party AA receives 100100 votes and the candidate for party BB receives 200200 votes. All 100100 votes for party AA are wasted (lost votes for AA), and 4949 votes for party BB are wasted (excess votes for BB). This is because BB needs 151151 (\lfloor (100 + 200)/2\rfloor + 1⌊(100+200)/2⌋+1) votes to win (over AA), so the remaining 4949 are wasted.

A low efficiency gap indicates that the elections are competitive, and that the number of candidates elected from each party is representative of the total voting share for each party. When the efficiency gap is high, this can be an indication of gerrymandering. Gerrymandering refers to organizing voting districts in a way that favors a particular political outcome. Two common ways of doing this are to “pack” similar voters into districts, or “crack” them across multiple districts; both ways tend to diminish those voters’ influence in electing candidates they would like to win.

In an election, districts are made up of precincts. A precinct is an indivisible group of voters. The votes for all precincts in a district are added together to find the results for that district. In this problem you are given a description of a number of precincts: the party vote totals for each precinct, and how those precincts have been grouped into districts. For each district, determine the party that wins and the wasted votes for each party. Then determine the efficiency gap between the two parties over all the districts.

模拟题

#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=4*1e5+5;//?????????? 4e8
int n,m;
map < pair < int , int > , int > mp;//区域,人,票 
int sum,res1,res2;
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		mp[make_pair(x,1)]+=y;
		mp[make_pair(x,2)]+=z;
		sum+=y+z;
	}
	for(int i=1;i<=m;i++)
	{
		if(mp[make_pair(i,1)]>mp[make_pair(i,2)])
		{
			printf("A ");
			int cnt=mp[make_pair(i,1)]+mp[make_pair(i,2)];
			int ans=mp[make_pair(i,1)]-(cnt/2+1);
			printf("%lld %lld\n",ans,mp[make_pair(i,2)]);
			res1+=ans;
			res2+=mp[make_pair(i,2)];
		}
		else
		{
			printf("B ");
			int cnt=mp[make_pair(i,1)]+mp[make_pair(i,2)];
			int ans=mp[make_pair(i,2)]-(cnt/2+1);
			printf("%lld %lld\n",mp[make_pair(i,1)],ans);
			res1+=mp[make_pair(i,1)];
			res2+=ans;
		}
	}
	res1=abs(res1-res2);
	printf("%.10lf\n",(double)((double)res1/(double)sum));
}
signed main()
{
//	ios::sync_with_stdio(false);
	int T=1;
//	cin>>T;
	while(T--)  solve(); 
	return 0;
}

D Missing Numbers

You enjoy your new job as a teacher of young children. It’s fun to see them learning to count, recognize letters, draw, and interact with the world.

One common problem you’ve noticed is that children often forget numbers when counting. For example, early on they might count “one, two, three, five, six.” You have to remind them about that “four” that they didn’t say. And as they get more proficient and clever, they may use the “quick” way of counting: “one, two, skip a few, ninety-nine, one hundred!”

Please write a program that can help you (and your students) identify the missing numbers when they are counting.

签到题

#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=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
map < int , int > mp;
int n,m;
 
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		mp[x]=1;
		m=max(m,x);
	}
	int flag=0;
	for(int i=1;i<=m;i++)  if(!mp[i])  
	{
		cout<<i<<endl;
		flag=1;
	}
	if(!flag)  puts("good job");
}
signed main()
{
	int T=1;
//	cin>>T; 
	solve();
    return 0;
}

E NVWLS

NVWLS, or “No Vowels” puzzles are popular among puzzle enthusiasts. For example, consider the following no-vowels message:

BTWNSBTLSHDNGNDTHBSNCFLGHTLSTHNNCFQLSN

which is inscribed on the famous “Kryptos” statue located at the CIA’s headquarters in Virginia. This message is derived from the following sentence by removing all vowels and spaces:

BETWEEN SUBTLE SHADING AND THE ABSENCE OF LIGHT LIES THE NUANCE OF IQLUSION

Given a dictionary (a set of words that can be used to construct a sentence) and a message (which comes from a sentence which uses only those words, but with all vowels and spaces removed), reconstruct the original sentence using the dictionary words!

防AK的题,待补。。。。。

F Prospecting

题目描述
Prospectin’ Pete has a lead on a new titanium mine, and needs your help pitching a mining operation to investors. The mine can be represented as a tree: the mine entrance is the root of the tree, other tree nodes are pockets of underground titanium ore, and the tree edges are potential tunnels Pete can dig between two pockets (or between the mine entrance and one pocket, for edges adjacent to the root node). The tunnel connecting the ii-th ore pocket to its parent has a length of y_iy
i

feet. One of the tree leaves contains the motherlode, and each other ore pocket contains x_ix
i

dollars worth of ore.

Pete begins at the mine entrance, and his goal is to reach the motherlode. Obviously, Pete cannot travel to pocket ii until all y_iy
i

feet of dirt in the tunnel leading to it has been removed. But once a tunnel has been completely excavated, Pete can travel that tunnel, in both directions, as many times as he wants.

Pete explores the mines by repeatedly performing the following steps, in order:

Pete chooses a tunnel that he can reach from his current location, and that has not yet been fully excavated.
Pete digs through one foot of the chosen tunnel; this costs him one dollar.
If the tunnel is now completely clear, Pete travels through the tunnel to the newly opened pocket and mines the ore. If that pocket contains the motherlode, then he stops exploring the mine. Otherwise, he sells the ore in that pocket for x_ix
i

dollars and continues exploring.

Note that the tunnel Pete chooses to dig in the first step of each round of digging does not need to be adjacent to his current location, so long as Pete can reach the tunnel by traveling through the mine along a sequence of completely-excavated tunnels. He can also choose a different tunnel each round, even if the tunnel he was digging in the previous round is not yet completely excavated. If Pete ends a round of digging with no money, and without having reached the motherlode, the mining operation is a bust and Pete goes home bankrupt.

Pete has surveyed the geology of the area and knows the mine layout, as well as the amount of ore in each chamber, but hasn’t yet decided on a strategy for how to dig the tunnels. He knows that in addition to any riches he earns from the mine itself, he will need some amount of startup money in order to reach the motherlode, and so he is courting investors. He wants to present them with two pieces of information:

The minimum number of dollars aa that Pete must begin with in order for his venture to be successful even in the worst-case: for it to be guaranteed that he reaches the motherlode no matter how he chooses the tunnel to excavate during each round of digging.
The minimum number of dollars bb that Pete must begin with in order to reach the motherlode in the best-case scenario where Pete chooses tunnels optimally.

This information will allow the investors to decide how much to fund Pete, based on how much they trust him to dig optimally without making any mistakes.

Given the mine layout, compute aa and bb.

链接:https://ac.nowcoder.com/acm/contest/13168/F
来源:牛客网

Figure F.1: Illustrations of sample inputs 11 (on the left) and 22. Edges represent tunnels and nodes represent ore pockets. Ore values x_ix
i

in each pocket and length y_iy
i

of each tunnel are written in black text (“ML” stands for the motherlode). Nodes are labeled in red with their indices.

防AK的题

G Research Productivity Index

Angela is a new PhD student and she is nervous about the upcoming paper submission deadline of this year’s research conference. She has been working on multiple projects throughout the past year. Luckily most of the projects concluded successfully, and she came up with nn candidate papers. However not all of the papers were born equal——some have better results than others. Her advisor believes she should only submit the papers with “good enough” results so they have a high chance of getting accepted.

Angela’s research group has a unique way of evaluating the success of paper submissions. They use the research productivity index, defined as a^{a/s}a
a/s
, where ss is the total number of papers submitted, and aa is the number of papers that are accepted by the conference. When a = 0a=0, the index is defined to be zero. For example:

if one paper is submitted and it gets accepted, the index is 1^{1/1} = 11
1/1
=1;

if 4 papers are submitted and all get accepted, the index is 4^{4/4} = 44
4/4
=4;

if 10 papers are submitted and 3 get accepted, the index is 3^{3/10} ≈ 1.3903893
3/10
≈1.390389;

if 5 papers are submitted and 4 get accepted, the index is 4^{4/5} ≈ 3.0314334
4/5
≈3.031433;

if 3 papers are submitted and all get rejected (a = 0), the index is 0.

Intuitively, to get a high research productivity index one wants to get as many papers accepted as possible while keeping the acceptance rate high.

For each of her nn papers, Angela knows exactly how likely it is that the conference would accept the paper. If she chooses wisely which papers to submit, what is the maximum expected value of her research productivity index?

概率dp,因为还算简单就不解释了

#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=4*1e2+5;//?????????? 4e8
int n;
double f[N][N],a[N];
bool cmp(double x,double y)
{
	return x>y;
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  scanf("%lf",&a[i]),a[i]/=100.0;
	sort(a+1,a+n+1,cmp);
	f[0][0]=1.0;
	for(int i=1;i<=n;i++)
	{
		f[i][0]=f[i-1][0]*(1-a[i]);
		for(int j=1;j<=i;j++)  f[i][j]=f[i-1][j]*(1-a[i])+f[i-1][j-1]*a[i];
	}
	double sum1=0;
	for(int i=1;i<=n;i++)  
	{
		double ans=0;
		for(int j=1;j<=i;j++)  ans+=f[i][j]*pow(j,1.0*j/i);
		sum1=max(sum1,ans); 
	}
	printf("%.10lf\n",sum1);
}
signed main()
{
//	ios::sync_with_stdio(false);
	int T=1;
//	cin>>T;
	while(T--)  solve(); 
	return 0;
}

H Running Routes

题目描述
The administrators at Polygonal School want to increase enrollment, but they are unsure if their gym can support having more students. Unlike a normal, boring, rectangular gym, the gym floor at Polygonal is a regular nn-sided polygon! They affectionately refer to the polygon as PP .

The coach has drawn several running paths on the floor of the gym. Each running path is a straight line segment connecting two distinct vertices of PP . During gym class, the coach assigns each student a different running path, and the student then runs back and forth along their assigned path throughout the class period. The coach does not want students to collide, so each student’s path must not intersect any other student’s path. Two paths intersect if they share a common point (including an endpoint).

Given a description of the running paths in PP , compute the maximum number of students that can run in gym class simultaneously.

Figure H.1: Illustrations of the two sample inputs, with possible solutions highlighted in thick red lines. Solid black lines represent running paths that are not assigned to a student, and dashed black lines are used to show the boundary of PP in places where no running path exists.

没想到,居然是区间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
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e6+5;
const int N=1e3+5;//?????????? 4e8
int n;
int f[N][N],a[N][N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  for(int j=1;j<=n;j++)  scanf("%d",&a[i][j]);
	for(int len=2;len<=n;len++)  for(int l=1;l<=n;l++)
	{
		int r=len+l-1;
		if(r>n)  continue;
		f[l][r]=f[l+1][r-1]+a[l][r];
		for(int k=l;k<=r;k++)  f[l][r]=max(f[l][r],f[l][k]+f[k+1][r]);
	}
	cout<<f[1][n]<<endl;
}
signed main()
{
	int T=1;
//	cin>>T; 
	while(T--)  solve();
    return 0;
}

I Slow Leak

You are an avid cyclist and bike every day between your home and school. Usually, your ride is uneventful and you bike along the shortest path between home and school. After school this afternoon you realized you have a slow leak in your bike tire——the tire can hold some air, but not for long. Refilling the tire allows you to ride your bike for some distance, after which your tire goes flat again and it becomes impossible to ride any further (and you refuse to walk your bicycle).

Luckily for you, your city has installed several bike repair stations at intersections throughout town where you can refill your tire and bike again until the tire goes flat. There’s a repair station at your school too, so that you can fill up your tire before you start on your trip home.

You’ve calculated how far you can bike before your tire runs out of air and you know the layout of your town, including all the intersections, distances between them, and the locations of the repair stations. What is the shortest possible trip from school to your home that you can take without becoming stuck due to a flat tire? (You do not become stuck if you roll into a repair station, or your home, at the exact same time as your tire goes flat.)

floyd重新建图,使得每个新节点都是加油站,再跑SPFA

#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-12
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e6+5;
const int N=2*1e6+5;//?????????? 4e8
int n,m,dis,t;
struct node
{
	int ver,edge,next;
}e[N];
int tot,head[N];
int a[N],d[N],v[N];
int f[505][505];
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);
}
//bool dijkstra()
//{
//	priority_queue < pair < int , pair < int , int > > > q;
//	while(q.size()) q.pop();
//	q.push(make_pair(0,make_pair(dis,1)));
//	for(int i=0;i<=n+1;i++)  d[i]=1e18;
//	d[1]=0;
//	while(q.size())
//	{
//		int x=q.top().second.second;
//		int p=q.top().second.first;
//		q.pop();
//		int flag=0;
//		if(a[x])  flag=1;
//		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;
//			int w=p-z;
//			if(flag)  w=dis-z;
//			if(w<0)  continue;
//			if(d[y]>d[x]+z)
//			{
//				d[y]=d[x]+z;
//				q.push(make_pair(-d[y],make_pair(w,y)));
//			}
//		}
//	}
//	return d[n]!=1e18;
//}
bool spfa()
{
	queue < int > q;
	for(int i=0;i<=n;i++)  d[i]=1e18;
	d[1]=0;
	q.push(1);
	while(q.size())
	{
		int x=q.front();q.pop();
		v[x]=0;
		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;
				if(!v[y])
				{
					v[y]=1;
					q.push(y);
				}
			}
		}
	}
	return d[n]!=1e18;
}
void solve()
{
	cin>>n>>m>>t>>dis;
	for(int i=1;i<=t;i++)
	{
		int x;
		scanf("%lld",&x);
		a[x]=1;
	}
	a[1]=a[n]=1;
	for(int i=0;i<=501;i++)  for(int j=0;j<=501;j++)  f[i][j]=1e18;
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		f[x][y]=f[y][x]=min(f[x][y],z);
//		addedge(x,y,z);
	}
	for(int i=1;i<=n;i++)  f[i][i]=0;
	for(int k=1;k<=n;k++)  for(int i=1;i<=n;i++)  for(int j=1;j<=n;j++)  f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
	for(int i=1;i<=n;i++)  for(int j=i+1;j<=n;j++)  if(a[i]&&a[j]&&f[i][j]<=dis)  addedge(i,j,f[i][j]);
	if(!spfa())  puts("stuck");
	else  cout<<d[n]<<endl;
}
signed main()
{
	int T=1;
//	cin>>T; 
	while(T--)  solve();
    return 0;
}

J Stop Counting!

The Martingale casino is creating new games to lure in new gamblers who tire of the standard fare. Their latest invention is a fast-paced game of chance called Stop Counting!, where a single customer plays with a dealer who has a deck of cards. Each card has some integer value.

One by one, the dealer reveals the cards in the deck in order, and keeps track of the sum of the played cards and the number of cards shown. At some point before a card is dealt, the player can call “Stop Counting!” After this, the dealer continues displaying cards in order, but does not include them in the running sums. At some point after calling “Stop Counting!”, and just before another card is dealt, the player can also call “Start Counting!” and the dealer then includes subsequent cards in the totals. The player can only call “Stop Counting!” and “Start Counting!” at most once each, and they must call “Stop Counting!” before they can call “Start Counting!”. A card is “counted” if it is dealt before the player calls “Stop Counting!” or is dealt after the player calls “Start Counting!”

The payout of the game is then the average value of the counted cards. That is, it is the sum of the counted cards divided by the number of counted cards. If there are no counted cards, the payout is 0.

You have an ‘in’ with the dealer, and you know the full deck in order ahead of time. What is the maximum payout you can achieve?

求长度相等的前后两端,的最大平均数(长度可以是0)

#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-12
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e6+5;
const int N=1e6+5;//?????????? 4e8
int n;
double a[N];
double s1[N],s2[N],s3[N],s4[N];
double p1[N],p2[N],p3[N],p4[N];
bool check(double mid)
{
	int flag=0;
	for(int i=1;i<=n;i++)
	{
		s2[i]=s1[i]-(double)i*mid;
		if(s2[i]>eps)
		{
			flag++;
			break;
		}
	}
	for(int i=n;i;i--)
	{
		p2[i]=p1[i]-(double)i*mid;
		if(p2[i]>eps) 
		{
			flag++;
			break;
		}
	}
	if(flag==2)  return 1;
	return 0;
}
void solve()
{
	double l=1e9,r=-1e9;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf",&a[i]);
		l=min(a[i],l);r=max(r,a[i]);
	}
	for(int i=1;i<=n;i++)  s1[i]=s1[i-1]+a[i];
	for(int i=n;i;i--)  p1[i]=p1[i+1]+a[i];
	for(int i=1;i<=n;i++)  s1[i]/=(double)i;
	for(int i=n;i;i--)  p1[i]/=(double)(n-i+1);
	double ans=0.0;
	for(int i=1;i<=n;i++)  ans=max(ans,max(p1[n-i+1],s1[i]));
//	double ans=0;
//	l-=1.0,r+=1.0;
//	while(r-l>eps)
//	{
//		double mid=(r+l)/2;
//		if(check(mid))
//		{
//			ans=mid;
//			l=mid;
//		}
//		else r=mid;
//	}
//	ans=max(0.0,ans);
	printf("%.10lf\n",ans);
}
signed main()
{
	int T=1;
//	cin>>T; 
	while(T--)  solve();
    return 0;
}

K Summer Trip

Leo has started a job in a travel agency. His first task is to organize a summer trip to an exotic overseas city. During the summer season, events of various types take place in the city: sports matches, concerts, beach parties, and many others. At any given time, there is exactly one event taking place. Events of any particular type may take place more than once during the season. The itinerary of events that Leo offers to his clients cannot be chosen arbitrarily; the company requires them to form a so-called “good itinerary.” A good itinerary is a consecutive sequence of at least two events in the summer season, where the first and last events are of different types, and they are both unique among all event types during the sequence. For example, if the first event in a good itinerary is a beach party, none of the other events during the itinerary can also be a beach party. There are no other restrictions on the event types in the sequence of a good itinerary.

Before he starts organizing the trip, Leo wants to know the total number of good itineraries that are possible given a calendar of events that will take place over the summer season.

求一个子串,使得前后两端的字符没在串中出现。

#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=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
char s[N];
int n,ans;
unordered_map < char , int > mp2;
void solve()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<n;i++)  
	{
		mp2.clear();mp2[s[i]]=1;
		for(int j=i+1;j<=n;j++)
		{
			if(s[j]==s[i])  break;
			if(!mp2[s[j]])  ans++;
			mp2[s[j]]=1;
//			if(s[i]!=s[j])
//			{
				mp[make_pair(s[i],s[j])]=1;
//				ans++;
//			}
		}
	}
	cout<<ans<<endl;
}
signed main()
{
	int T=1;
//	cin>>T; 
	solve();
    return 0;
}

L Traveling Merchant

There is a long east-west road which has nn towns situated along it, numbered 11 to nn from west to east. All towns buy and sell the same kind of goodie. The value of a goodie fluctuates according to a weekly schedule. A town buys and sells a goodie at its value in that town on that particular day. At town ii, the value of a goodie changes by d_id
i

every day in the first half of a week, and changes by d_id
i

every day in the second half of a week. In other words, the value of a goodie at town ii is v_iv
i

on Mondays and Sundays, v_iv
i

  • d_id
    i

    on Tuesdays and Saturdays, v_i +2d_iv
    i

    +2∗d
    i

    on Wednesdays and Fridays, and v_i + 3
    d_iv
    i

    +3∗d
    i

    on Thursdays.

A merchant is making a business travel plan. His trip begins at a starting town ss and ends at a destination town tt, visiting each town from ss to tt (inclusive) exactly once. The merchant starts the trip on a Monday. It takes him exactly one day to travel between adjacent towns and every day he travels to the next town on the path to the destination. He may buy exactly one goodie at a town along the trip and sell that goodie at a town he visits later. He can only buy once and sell once. The merchant would like to know the maximum possible profit of qq travel plans with different choices of town ss and town tt.

开7个线段树,记得前两天才做,没想到又出现了

#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=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
struct tree
{
	int mx,mi,getmi,getmx;
}t[N][50];
int a[N],b[N],w[N][15];
int n,m;
tree push(tree a,tree b)
{
    tree res;
    res.mx=max(a.mx,b.mx);
    res.mi=min(a.mi,b.mi);
    res.getmx=max(max(a.getmx,b.getmx),b.mx-a.mi);
    res.getmi=min(min(a.getmi,b.getmi),b.mi-a.mx);
    return res;
}
int get(int l,int r)
{
    if(l<r)  return (l-1)%7;
    else  return l%7;
}
void build(int f,int p,int l,int r)
{
    if(l==r)
	{
        t[p][f].mi=w[l][f];
        t[p][f].mx=w[l][f];
        t[p][f].getmi=2e9;
        t[p][f].getmx=-2e9;
        return;
    }
    int mid=(l+r)>>1;
    build(f,p<<1,l,mid);
    build(f,p<<1|1,mid+1,r);
    t[p][f]=push(t[p<<1][f],t[p<<1|1][f]);
}
tree ask(int f,int p,int l,int r,int L,int R)
{
    if(l>=L&&r<=R)  return t[p][f];
   	int mid=(l+r)>>1;
    if(R<=mid)  return ask(f,p<<1,l,mid,L,R);
    if(L>mid)  return ask(f,p<<1|1,mid+1,r,L,R);
    tree las=ask(f,p<<1,l,mid,L,R);
    tree nex=ask(f,p<<1|1,mid+1,r,L,R);
    return push(las,nex);
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  scanf("%d%d",&a[i],&b[i]);
	for(int i=1;i<=n;i++)  for(int j=0;j<=6;j++)  w[i][j]=a[i];
	for(int j=0;j<=6;j++)  for(int i=1;i<=n;i++)
	{
		int op=((i-j)+7)%7;
        if(op==2||op==6)  w[i][j]+=b[i];
        else if(op==3||op==5)  w[i][j]+=2*b[i];
        else if(op==4)  w[i][j]+=3*b[i];
	}
	for(int i=0;i<=6;i++)  build(i,1,1,n);
	cin>>m;
	while(m--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		int num=get(l,r);
		tree ans=ask(num,1,1,n,min(l,r),max(l,r));
        if(l<r)  printf("%d\n",max(0,ans.getmx));
        else  printf("%d\n",max(0,-ans.getmi));
	}
}
signed main()
{
	int T=1;
//	cin>>T; 
	while(T--)  solve();
    return 0;
}

M Zipline

A zipline is a very fun and fast method of travel. It uses a very strong steel cable, connected to two poles. A rider (which could be a person or some cargo) attaches to a pulley which travels on the cable. Starting from a high point on the cable, gravity pulls the rider along the cable.

Your friend has started a company which designs and installs ziplines, both for fun and for utility. However, there’s one key problem: determining how long the cable should be between the two connection points. The cable must be long enough to reach between the two poles, but short enough that the rider is guaranteed to stay a safe distance above the ground. Help your friend determine these bounds on the length.

The cable connects to two vertical poles that are ww meters apart, at heights gg and hh meters, respectively. You may assume that the cable is inelastic and has negligible weight compared to the rider, so that there is no sag or slack in the cable. That is, at all times the cable forms two straight line segments connecting the rider to the two poles, with the sum of the segments lengths equal to the total length of the cable. The lowest part of the rider hangs rr meters below the cable; therefore the cable must stay at least rr meters above the ground at all times during the ride. The ground is flat between the two poles. Please refer to the diagram in Figure M.1 for more information.

在这里插入图片描述

#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=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
double w,g,r,h;
double qr1()
{
	return sqrt(w*w+(h-g)*(h-g));
}
double qr2()
{
	return sqrt((h+g-2*r)*(h+g-2*r)+w*w);
}
void solve()
{
	cin>>w>>g>>h>>r;
	printf("%.8lf %.8lf\n",qr1(),qr2());
}
signed main()
{
	int T=1;
	cin>>T; 
	while(T--)  solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值