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

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

A-Circuit Math
链接:https://ac.nowcoder.com/acm/contest/13168/A
来源:牛客网

题目描述
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

思路:使用栈模拟电路的与、或、非运算,与、或运算需要两个运算数,则从栈里弹出两个数,运算结果压入栈;非运算弹出一个数,处理后压入栈。
代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
stack<char>s1,s2;
map<char,char>mp;
char answer(char a,char b,char op){
	if(op=='*'){
		if(a=='T'&&b=='T'){
			return 'T';
		}
		return 'F';
	}else if(op=='+'){
		if(a=='T'||b=='T'){
			return 'T';
		}else{
			return 'F';
		}
	}
	return 'F';
}
char a[28];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i]; 
	}
	char c;
	int cnt=0,flag=0;
	getchar();
	while(cin.get(c)&&c!='\n'){
		if(c<='Z'&&c>='A'){
			if(!mp.count(c)){
				cnt++;
				mp[c]=a[cnt];
			}
			s1.push(mp[c]);
		}
		char t1,t2,ans;
		if(c=='*'||c=='+'){
			t1=s1.top();
			s1.pop();
			t2=s1.top();
			s1.pop();
			ans=answer(t1,t2,c);
			s1.push(ans);
		}else if(c=='-'){
			t1=s1.top();
			s1.pop();
			if(t1=='T'){
				ans='F';
			}else{
				ans='T';
			}
			s1.push(ans);
		}
	}
	cout<<s1.top()<<endl;
	return 0;
} 

B-Diagonal Cut
链接:https://ac.nowcoder.com/acm/contest/13168/B
来源:牛客网

题目描述

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!"

输入描述:
The input consists of two space-separated integers MM and NN given on a single line, (where 1 ≤ M, N ≤ 10^{18}1≤M,N≤10
18
). The numbers MM and NN denote the number of blocks in one column and in one row, respectively, in the chocolate bar.
输出描述:
Print the number of blocks of the chocolate bar which are cut into exactly two pieces of equal area.
示例1
输入
6 10
输出
2
示例2
输入
75206452536745713 10322579177493903
输出
40318322589

思路:对N×M 格子的长方形画对角线,求线划分出的两半面积相等的小格子的个数;
先求最大公因数,这样一个大的长方形所分的格子就是由gcd(a,b)个小长方形画对角线组成的,只有小矩形的长宽都是奇数的时候,才能找到一对中心对称的格子。
代码:

#include<iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long int gcd(long long int x,long long int y){
	long long int z = y;
	while(x%y!=0){
		z = x%y;
		x = y;
		y = z;	
	}
	return z;
}
int main(){
	long long int n,m,k;
	cin>>n>>m;
	k=gcd(n,m);
	n=n/k;
	m=m/k;
	if(n%2&&m%2){
		cout<<k<<endl;
	}
	else{
		cout<<"0"<<endl;
	}
	return 0;
}

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

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 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 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 and ) 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 receives votes and the candidate for party receives votes. All votes for party are wasted (lost votes for ), and votes for party are wasted (excess votes for ). This is because needs () votes to win (over ), so the remaining are wasted.

Political scientists use wasted votes to compute the efficiency gap, a single number that summarizes wasted votes. Suppose we have a number of races in different districts, where each district elects one person. Across all districts there are total votes cast, with total wasted votes for party and total wasted votes for party . Then the efficiency gap is:
.

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.

输入描述:
The input describes one election. The first line contains two integers and , where and . These indicate, respectively, the number of voting precincts and districts. Following this are lines describing the precincts. Line contains numbers: the district that precinct is assigned to , the number of votes for the candidate from party , and the number of votes for the candidate from party . It is guaranteed that:

  1. for each precinct ,

  2. each district is assigned at least one precinct, and

  3. there are no ties within any district.

输出描述:
For each of the districts from to , print which party wins (a single character, either or ). Then print the number of wasted votes for party and for party , in order. Finally, after reporting on all the districts, print the efficiency gap as measured over all the districts. The efficiency gap should be accurate to within an absolute error of .
示例1
输入
5 3
1 100 200
2 100 99
3 100 50
3 100 50
2 100 98
输出
B 100 49
A 1 197
A 49 100
0.1965897693
示例2
输入
4 4
3 100 99
2 100 99
1 100 99
4 100 99
输出
复制
A 0 99
A 0 99
A 0 99
A 0 99
0.4974874372
示例3
输入
4 4
4 99 100
1 100 99
3 100 99
2 99 100
输出
A 0 99
B 99 0
A 0 99
B 99 0
0.0000000000

思路:题目要求找出每个地区A和B的无效票,以及所有地区的 efficiency gap 之和,枚举即可。
代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
struct node{
	ll x;
	ll y;
}area[10010];
int main(){
	int p,d;
	cin>>p>>d;
	ll a,b,c;
	ll sum=0,sum1=0,sum2=0,t=0;
	for(int i=1;i<=p;i++){
		cin>>a>>b>>c;
		area[a].x+=b;
		area[a].y+=c;
		sum+=b+c;
	}
	for(int i=1;i<=d;i++){
		if(area[i].x>area[i].y){
			t=area[i].x-((area[i].x+area[i].y)/2+1);
			sum1+=t;
			sum2+=area[i].y; 
			cout<<"A "<<t<<" "<<area[i].y<<endl;
		}else{
			t=area[i].y-((area[i].x+area[i].y)/2+1);
			sum1+=area[i].x;
			sum2+=t;
			cout<<"B "<<area[i].x<<" "<<t<<endl;
		}
	} 
	double ans=0; 
	ans=abs(sum1-sum2)*1.0/sum*1.0;
	printf("%.10f",ans);
	return 0;
} 

K-Summer Trip
链接:https://ac.nowcoder.com/acm/contest/13168/K
来源:牛客网

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.

输入描述:

The input consists of one line with a string describing the sequence of event types in the summer season. All characters are lowercase English letters (a - z)(a−z), with different letters represent different types of events. Character ii of the string encodes the ii-th event of the summer. There are no blanks or spaces in the string.

The length of the input string is at least 22 and at most 100 000100000 characters.

输出描述:
Print the number of good itineraries that exist for the given summer season.
示例1
输入
abbcccddddeeeee
输出
10
示例2
输入
thenumberofgoodstringsis
输出
143

思路:给出以一个只含小写字母的序列,然后让你选长度至少为2且第一字符没有重复,最后一个字符也没有重复的子序列.那么固定最后一个字符,找前面一次的字符开头即可。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
using namespace std;
int a[26];
int main(){
	string s;
	cin>>s;
	int sum=0,cnt=0;
	for(int i=0;i<s.length();i++){
		cnt=1;
		memset(a,0,sizeof(a));
		for(int j=i+1;j<s.length();j++){
			 if(s[i]==s[j]||cnt>26){
			 	break;
			 }
		 	if(!a[s[j]-'a']){
		 		a[s[j]-'a']=1;
		 		sum++;
		 		cnt++;
			 }
			 
		}
	}
	cout<<sum<<endl;
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值