Analysis of 2022 TIANTI competition

Time:2022-5-22

L1-1 I want to win today (5 points)

Title Description

在这里插入图片描述

code

#include 
using namespace std;

int main()
{
	cout << "I'm gonna win! Today!" << endl;
    cout << "2022-04-23" << endl;
	return 0;
}

L1-2 kinds of diamonds (5 points)

Title Description

请添加图片描述
在这里插入图片描述

code

#include 

using namespace std;

int main()
{
    int n , v;
    cin >> n >> v;
    cout << n / v;
	return 0;
}

L1-3 who can enter the library (10 points)

Title Description

在这里插入图片描述
在这里插入图片描述

code

#include 

using namespace std;

int  main()
{
	int j , p , x1 , x2;
	cin >> j >> p >> x1 >> x2;
	if(x1 >= j || (x1 < j && x2 >= p)) cout << x1 << "-Y ";
	else       cout << x1 << "-N ";
	if(x2 >= j|| (x2 < j && x1 >= p)) cout << x2 << "-Y\n";
	else       cout << x2 << "-N\n";
	if(x1 >= j && x2 >= j) {
		cout << "huan ying ru guan\n";
	} else if(x1 < j && x2 < j) {
		cout << "zhang da zai lai ba\n";
	} else if(x1 < j && x2 >= p) {
		printf("qing 2 zhao gu hao 1\n");
	} else if(x2 < j && x1 >= p) {
		printf("qing 1 zhao gu hao 2\n");
	}Else if (x1 > = J & & x2 < J & & X1 < p) {// I didn't think about it clearly at the beginning, but I found an error at the last minute~
		printf("1: huan ying ru guan\n");
	} else if(x2 >= j && x1 < j && x2 < p) {
		printf("2: huan ying ru guan\n");
	}
    return 0;
}

Summary:

  1. To do this kind of classified discussion, people need to consider all the situations clearly. Generally, there are many classified situations. You can make a simple thinking map on paper, so that the idea is clear and there will be no mistakes of incomplete consideration and details (such mistakes in the exam are difficult to find ~ ~)
  2. Mind map:
    请添加图片描述

L1-4 saving Aliens (10 points)

Title Description

请添加图片描述
在这里插入图片描述

code

#include 
using namespace std;

Long long f (int n) // it's better to write long long to prevent data overflow 
{
    long long t = 1;
    for(int i = 1 ; i <= n ; ++i)
        t *= i;
    return t;
}
int main()
{
    int a , b;
    cin >> a >> b;
    cout << f(a + b);
	return 0;
}

L1-5 try your luck (15 points)

Title Description

请添加图片描述
在这里插入图片描述

code

#include 
#include 
#include 
#include 
#include 
using namespace std;

int num[10];
bool vis[10][10];
//Vis [the ith face] [the number of points on the ith face]: judge whether it has occurred
int main()
{
    for(int i = 0 ; i < 6 ; ++i) cin >> num[i];
    int n;
    cin >> n;
    while(n--) {
        for(int i = 0 ; i < 6 ; ++i) {
            vis[i][num[i]] = true;
            //From large to small, ensure the maximum points per time
            for(int j = 6 ; j >= 1 ; --j)
                if(!vis[i][j]) {
                    num[i] = j;
                    break;
                }
        }
    }
    for(int i = 0 ; i < 6 ; ++i) cout << num[i] << " \n"[i == 5];
	return 0;
}

L1-6 questions on Stockholm train (15 points)

Title Description

请添加图片描述

在这里插入图片描述

code

#include 

using namespace std;

string f(string s)
{
    string s0;
    for (int i = 1 ; i < s.size(); i++)
        if (s[i] % 2 == s[i-1] % 2)
            s0 += max(s[i], s[i-1]);
    return s0;
}
int main()
{
    string s1 , s2;
    cin >> s1 >> s2;
    if(f(s1) == f(s2)) cout << f(s1);
    else cout << f(s1) << endl << f(s2) << endl;
    return 0;
}

L1-7 mechanic mstadio (20 points)

Title Description

在这里插入图片描述

code

Test 19 point code

#include 

//Test 19 points
using namespace std;
const int N = 1e4 + 9999;
bool vis[N][N];
int  main()
{
	int n , m , q , ans = 0;
	cin >> n >> m >> q;
	while(q--) {
		int t , c;
		cin >> t >> c;
		if(t == 1) {
			for(int i = 0 ; i < n ; ++i)
				vis[i][c - 1] = true;
		} else {
			for(int i = 0 ; i < m ; ++i)
				vis[c - 1][i] = true;
		}
	}
 	for(int i = 0 ; i < n ; ++i)
		for(int j = 0 ; j < m ; ++j) {
			if(vis[i][j]) continue;
			++ans;
		}
	cout << ans;
    return 0;
}
Summary:
  1. The space of two-dimensional array should not be too large, and it is easy to explode the stack (such as this problem)\(N = 10^5\))

AC code

#include 

using namespace std;

set>st;
int  main()
{
	int n , m , q , ans = 0;
	cin >> n >> m >> q;
	while(q--) {
		int t , c;
		cin >> t >> c;
		if(t == 1) {
			for(int i = 1 ; i <= n ; ++i) {
				if(!st.count({i , c})) ++ans;
				st.insert({i , c});
			}
		} else {
			for(int i = 1 ; i <= m ; ++i) {
				if(!st.count({c , i})) ++ans;
				st.insert({c , i});
			}
		}
	}
	cout << n * m - ans;
    return 0;
}
summary
  1. This problem only needs to record the squares of the released skills, and there is no need to traverse the whole square
  2. Therefore, containers (sets, etc.) can be used to store the squares that have released skills, so as to avoid the problem of excessive overhead of two-dimensional arrays
  3. Use pair to take the row number and column number as a whole for easy processing(Often used)

L1-8 quiet recommendation (20 points)

Title Description

在这里插入图片描述
在这里插入图片描述

code

1. Test 1 point code

#include 
#define x first
#define y second
using namespace std;

typedef pair pii;
multimap m;
bool v1[300][105];

int  main()
{

	int n , k , s;
	cin >> n >> k >> s;
	while(n--) {
		int t , p;
		scanf("%d %d" , &t , &p);
		if(t < 175) continue;
  		m.insert(make_pair(t , p));
	}
	int ans = 0 , ans1 = 0 , ans2 = 0;
	for(int l = 0 ; l < k ; ++l) {
		bool v2[300] = {false};
		for(auto i = m.begin() ; i != m.end() ; ++i) {
			if(i->y >= s && !v1[i->x][i->y] && m.count(i->x) > 1 && l != 0) {
				++ans;
				v1[i->x][i->y] = true;
				continue;
			}
			if(!v2[i->x] && !v1[i->x][i->y]) {
				++ans;
				v2[i->x] = true;
				v1[i->x][i->y] = true;
			}
		}
		
	}
	cout << ans<
Summary:
  1. Without considering the situation that both scores are the same, marking with a two-dimensional array cannot deal with this situation
  2. I was confused during the exam and didn’t do it for a long time~~
  3. Not familiar with map storage(You cannot directly store pairs as key values), debugging took a long time~

2. 14 sub codes

#include 
#include 
#include 
#include 
#define x first
#define y second
using namespace std;

typedef pair pii;
vector v;

int main()
{
	int n , k , s;
	cin >> n >> k >> s;
	while(n--) {
		int t , p;
		scanf("%d %d" , &t , &p);
		if(t < 175) continue;
		v.push_back({t , p});
	}
	sort(v.begin() , v.end());
	int ans = 0;
	while(k--) {
		bool vis[300] = {false};
		for(int i = 0 ; i < v.size() ; ++i) {
			if(vis[v[i].x] && v[i].y < s) continue;
			vis[v[i].x] = true;
			++ans;
			v.erase(v.begin() + i);
			i = 0;
		}
	}
	cout << ans;
	return 0;
}
Summary:
  1. This is a popular practice, which directly enumerates all batches (k) and all>175 pointsInterviewers, that is, they need two cycles to operate
  2. Use the erase() function to delete the selected interviewer
    be careful:
    (1) Erase (iterator). The usage of deleting a single element in a vector is:v.erase(v.begin() + i)
    (2) Among themiFrom 0 toiOne, andAfter deleting an element, the subscript of the element in the container changesSo every time you deleteSet I to 0
    (3) Since erase () needs to be called, the running time is increased, and\(max (k * n) = P * 5 * 10 ^ 8 (P is determined by erase() function) \), and the given time is200ms,thereforeTLE

3. AC code

#include 
#include 
#include 
#define x first
#define y second
using namespace std;
unordered_map m;
int main()
{
	int n , k , s , ans = 0;
	cin >> n >> k >> s;
	while(n--) {
		int t , p;
		scanf("%d %d" , &t , &p);
		if(t < 175) continue;
		if(p >= s) {
			++ ans;
			continue;
		}
		++m[t];
	}
 	for(auto i : m)
		ans += i.y >= k ? k : i.y;
	cout << ans;
	return 0;
}
Summary:

1. According to the previous idea, double cycle TLE, so we have to think in the direction of single cycle
The title means(very important):

  • All ladder scores>=175Every point has a chance to be admitted
  • All PTA scores>=S (interview score line of the enterprise), directly admitted (TIANTI competition score)>=175)
  • Interviewers with the same ladder score (except TA score) cannot be admitted in the same batch>=S (interview score line of the enterprise)I.e. only one candidate can be admitted
  • The same batch of interviewers regardless of the PTA score (PTA score)>=S (interview score line of the enterprise)(except)

3. Clear thinking:

  • Get rid of the ladder score first>=175Interviewers (+ + ANS) and ladder scores < 175 (Operation while inputting)
  • The rest will use hash counting (array and container can be used) to calculate the number of interviewers with the same score
  • IfBatch k > = number of interviewers with a certain score, indicating that all interviewers with this score have been accepted
  • IfNumber of interviewers with batch K < a certain score, indicating that some of the interviewers with this score were admitted and K were admitted
  • Core code:ans += i.y >= k ? k : i.y;, emmmm, a littleGreedy thoughts

L2-2 boss’s schedule (25 points) [sort]

在这里插入图片描述

code

#include 
#include 
#include 
using namespace std;
setst;
unordered_map vis;
void f(string s) {
   	if(vis[s] == 2 || s == "00:00:00" || s == "23:59:59")
		st.erase(s);
	else
		st.insert(s);
}
int main()
{
	//Card I / O, TLE (21) points if cache is not released
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n , k , j = 0;
    cin >> n;
    while(n-- ) {
    	char ch;
        string s1 , s2;
        cin >> s1 >> ch >> s2;
        ++vis[s1] , ++vis[s2];
		f(s1) , f(s2);
    }
	if(!(vis["00:00:00"])) st.insert("00:00:00");
	if(!(vis["23:59:59"])) st.insert("23:59:59");
	for(auto i : st) {
		++j;
		cout << i;
		j & 1 ? cout << " - " : cout << endl;
	}
	return 0;
}

Summary:

  1. The idea is to see whether there is repetition time. If not, output it(except00:00:00and23:59:59)
  2. You can regard time as a string and output a strings1, character'-', and strings2
  3. Use set to store the answers to be output:
    (1) Store all the time first
    (2) Then use hash to judge whether the time is repeated
    (3) If it is repeated, it will be deleted, otherwise it will not be deleted
    (4) Special judgment00:00:00and23:59:59, these two will be deleted as soon as they appear
    (5) If not00:00:00and23:59:59, add
  4. Emmmm, if you follow the above ideas, you can directlyTLE(21)Points, becauseEach interval shall be at least 1 second,max(n) = 86400 / 2
  5. This question card is input and output. It is found that sometimes it can be passed and sometimes it can not be passed. You can use:
//Uncached template
ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

How can C / C + + speed up the efficiency of input and output

Data volume under [1E5] windwos:
Use unbound cout: 30.719000 seconds, 29.518000 seconds, 29.446000 seconds
Do not use unbind cout: 51.749000 seconds, 49.383000 seconds, 47.605000 seconds
Printf of C + + files: 84.962000 seconds, 76.131000 seconds, 77.639000 seconds
Printf of C language file: 29.776000 seconds, 29.327000 seconds, 29.862000 seconds