020-2021 Winter holiday Training Round 1

A - AD 2020
2020 is the current year and is a leap year starting on Wednesday of the Gregorian calendar, the 2020th year of the Common Era (CE) and Anno Domini (AD) designations, the 20th year of the 3rd millennium, the 20th year of the 21st century, and the 1st year of the 2020s decade.

2020 has been designated as Year of the Nurse and Midwife by the World Health Organization. The United Nations has declared 2020 as the International Year of Plant Health. 2020 has been designated as International Year of Sound by the International Commission for Acoustics.

Because there are so many unforgettable things happening in 2020, somebody has now proposed that all dates with a substring containing “202” to be designated as the Day for Disaster Reduction. We represent a date in the format YYYYMMDD (for example, 21110202), then if 202 is a substring of this date, this day is the Day for Disaster Reduction.

Please write a program to compute how many Days for Disaster Reduction are there in all the dates between Y1M1D1 and Y2M2D2 (both inclusive)? Note that you should take leap years into consideration. A leap year is a year that can be divided by 400 or can be divided by 4 but can’t be divided by 100. A leap year has 29 days in February, instead of the normal 28 days.

Input
The input contains multiple cases. The first line of the input contains a single positive integer T (1≤T≤105), the number of cases.

The first and only line of each case contains six integers Y1,M1,D1,Y2,M2,D2, which are described in the problem statement above.

It’s guaranteed that Y1M1D1 is not larger than Y2M2D2. Both Y1M1D1 and Y2M2D2 are between 20000101 and 99991231, and both dates are valid.

We kindly remind you that the size of the input and output of this problem can be large, so it’s recommended to use a faster I/O method. For example, in C++, you can use scanf/printf instead of cin/cout.

Output
For each case, print a single line containing a single integer, the answer.

Example
Input
3
2111 02 01 2111 02 03
2202 01 01 2202 12 31
2000 01 01 9999 12 31
Output
1
365
44294
Note
In the first sample case, 21110202 is the only Day for Disaster Reduction.

In the second sample case, 202 is a substring of 2202, so every day in the year 2202 is the Day for Disaster Reduction!

如果涉及到需要查询很多次的,需要提前预处理打表,执行次数很多的操作需要写成函数处理

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

//int dx[4] = {0, 1, 0, -1};
//int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 110;
const int M = 2e5 + 10;
const int mod = 77797;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int a[N], b[N];
int pre[10000][12][40];

int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline bool is_run(int year){
	if (year % 400 == 0)   return true;
	if (year % 100 != 0 && year % 4 == 0)    return true;
	return false;
}

inline bool is_202(int num){
	string str = "";
	bool flag1 = false;
	bool flag2 = false; 
	while(num){
		int temp = num % 10;
		if (temp == 2)   flag1 = true;
		if (temp == 0)   flag2 = true;
		num /= 10;
		str += (temp + '0');
	}
	
	if (!flag1 || !flag2)   return false;
	
	for (int i = 0; i < str.size(); i ++){
	  if (i + 3 - 1 <= str.size() - 1){
		if (str.substr(i, 3) == "202")   return true;
     	}
	}
	
	return false;
}

signed main(){
	int T;
	
	
		int yea = 2000, mon = 1, da = 1;
		
		int ans = 0;
		while(true){
			//cout << yea << "---" << mon << "---" << da << endl;
			int num = yea * 10000 + mon * 100 + da;
			if (is_202(num))   ans ++;
			pre[yea][mon][da] = ans;
			//cout << ans << ",";
			if (yea == 9999 && mon == 12 && da == 31)    break;
			
			if (is_run(yea))     month[2] = 29;
			else   month[2] = 28;
			
			if (da + 1 <= month[mon]){
				da += 1;
			}
			else if (mon + 1 <= 12){
				da = 1;
				mon += 1;
			}
			else{
				da = 1;
				mon = 1;
				yea ++;
			}
		}
	
//	cout << pre[9999][12][31] << endl;
     scanf("%lld", &T);
	while(T --){
		int year1, month1, day1, year2, month2, day2;
		scanf("%lld%lld%lld%lld%lld%lld", &year1, &month1, &day1, &year2, &month2, &day2);
		
	
		int ans = pre[year2][month2][day2] - pre[year1][month1][day1];
		if (is_202(year1 * 10000 + month1 * 100 + day1))   ans ++;
		printf("%lld\n", ans);
	}
	
	return 0;
}

I - Invoking the Magic
BaoBao is a lazy boy. He has n pairs of socks in different colours and he washes them once a month. In the washing machine, these socks will be mixed.

Because there are too many socks that need to be paired, BaoBao divides the whole sock pairing process into two stages.

In the first stage, BaoBao randomly distributes the socks in pairs. Then in the second stage, BaoBao repeats the following operation until all the socks are paired: BaoBao chooses some of the pairs of socks, puts them into his magic washing basin, and uses his magic. If the socks in the magic washing basin can be paired perfectly when he uses his magic, the magic washing basin will pair them for BaoBao automatically. However, if they can’t (which means there is at least one sock whose colour is unique in the basin), the magic basin will explode and BaoBao must not let this happen.

BaoBao’s magic is limited, after the first stage, he needs to know the minimum capacity of the magic washing basin that he needs to pair all the socks successfully. The capacity of the basin is the maximum number of pairs of socks BaoBao can put in the magic washing basin at one time.

Input
The input contains multiple cases. The first line of the input contains a single positive integer T (1≤T≤10), the number of cases.

For each case, the first line of the input contains a single integer n (1≤n≤105), the number of pairs of socks.

For the following n lines, the i-th (1≤i≤n) line contains two integers ai and bi (1≤ai,bi≤230), denoting the colours of the two socks in the i-th pair after the first stage. It is guaranteed that for each sock, there is exactly one other sock of the same colour.

Output
For each case, print a single line containing a single integer, the minimum capacity of the magic washing basin.

Example
Input
1
5
1 2
2 3
1 3
4 5
4 5
Output
3
Note
In the sample, BaoBao can first put these three pairs of socks: {1,2}{2,3}{1,3} in the magic washing basin and then they will be paired into {1,1}{2,2}{3,3}. Then, he can put {4,5}{4,5} in the magic washing basin and they will be paired into {4,4}{5,5}. Therefore, with a capacity of 3 it is possible to pair all the socks successfully. It can be shown by brute-force that this is impossible with a capacity of 2.

题目各种绕,其实就是问一次最多可以匹配k双袜子,需要最小把k设置成多少,就可以匹配成功,并查集很明显,但是数据范围有点大,所以再离散化一次

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

//int dx[4] = {0, 1, 0, -1};
//int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e6 + 10;
const int M = 2e5 + 10;
const int mod = 77797;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int a[N], b[N];
int p[N], cnt[N];
int n;
vector<int> alls;

int find(int x){
	if (x != p[x])   p[x] = find(p[x]);
	return p[x];
}

int find1(int x){
	return lower_bound(alls.begin(), alls.end(), x) - alls.begin();
}

signed main(){
	int T;
	scanf("%lld", &T);
	
	while(T --){
	
		alls.clear();
		int ans = -1;
		scanf("%lld", &n);
		for (int i = 1; i <= n; i ++){
			scanf("%lld%lld", &a[i], &b[i]);
			alls.push_back(a[i]);
			alls.push_back(b[i]);
		}
		
		sort(alls.begin(), alls.end());
		alls.erase(unique(alls.begin(), alls.end()), alls.end());
		
		for (int i = 0; i < alls.size(); i ++){
			p[i] = i;
			cnt[i] = 1;
		}	
		
		for (int i = 1; i <= n; i ++){
			int u = find1(a[i]);
			int v = find1(b[i]);
			
			int pu = find(u), pv = find(v);
			if (pu != pv){
				p[pu] = pv;
				cnt[pv] += cnt[pu];
				ans = max(ans, cnt[pv]);
			}
		}
		
		cout << ans << endl;
	}
	
	return 0;
}

K - Killing the Brute-force
Chenjb is the task author of the 71-st Zhejiang Provincial Collegiate Programming Contest. He created an NP-Hard problem on a graph with n vertices, the intended solution of which is DFS with branching and pruning. Chenjb is an experienced task author, he knows that the constraints can’t be too tight, so he will set the time limit t to be 3x, where x is the running time of the intended solution on the slowest test case.

Chenjb heard that the contest would be unfortunately held on Potato OJ, whose hardware is not so good. To protect Potato OJ from being overloaded and the contest being a failure, Chenjb plans to cut down the input size of his task. He has tested the running time of the intended solution and the brute-force solution for various values of n. Note that for simplicity, we assume the running time of the solutions on a test case only depends on the value of n.

Please help Chenjb find the smallest value of n such that 1≤n≤m, and the brute-force solution won’t pass the problem. Note that if the time limit is t, we assume that a solution will pass if and only if its running time on each test case is smaller than or equal to t.

Input
The input contains multiple cases. The first line of the input contains a single integer T (1≤T≤50), the number of cases.

For each case, the first line of the input contains a single integer m (1≤m≤50), denoting the upper bound of n.

The second line contains m integers a1,a2,…,am (1≤ai≤100,ai≤ai+1), the i-th of which denotes the running time of the intended solution when n=i.

The third line contains m integers b1,b2,…,bm (1≤bi≤100,bi≤bi+1), the i-th of which denotes the running time of the brute-force solution when n=i.

Output
For each case, print a single line containing a single integer denoting the minimum possible value of n. If there is no solution, print −1 instead.

Example
Input
2
4
1 2 7 20
2 5 30 40
3
2 3 3
5 7 8
Output
3
-1

给每个题设置一个时间,看暴力过到多少个样例的时候就过不去了,当发现有过不去的时候break掉即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

//int dx[4] = {0, 1, 0, -1};
//int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 110;
const int M = 2e5 + 10;
const int mod = 77797;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int a[N], b[N];

signed main(){
	int T;
	scanf("%lld", &T);
	
	while(T --){
		int m;
		scanf("%lld", &m);
		
		for (int i = 1; i <= m; i ++){
			scanf("%lld", &a[i]);
			a[i] *= 3;
		}
		
		for (int i = 1; i <= m; i ++){
			scanf("%lld", &b[i]);
		}
		
		int ans = -1;
		
		for (int i = 1; i <= m; i ++){
			if (a[i] < b[i]){
				ans = i;
				break;
			}
		}
		
		cout << ans << endl;
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值