1. 程式人生 > 其它 >阿里巴巴20220422筆試程式設計題

阿里巴巴20220422筆試程式設計題

備忘錄

新增記錄
輸入1 x y z s, 代表x年y月z日添加了一條記錄s
2.查詢資訊數量
輸入2 x y z, 代表查詢x年y月z日的資訊數量
3.查詢資訊日期
輸入3 s, 查詢資訊s的所在日期
日期可能非法,x[2022, 9999];
注意1, 日期非法輸出"error", s已存在輸出"existed", 新增成功輸出"done"
注意2, 日期非法輸出"error", 輸出整數
注意3, 資訊不存在輸出"not existed", 否則輸出日期,注意格式2022/04/22
樣例輸入
6
1 2022 3 32 ranko
1 2022 3 31 ranko
1 2022 1 1 ranko
2 2022 3 31
3 ranko
3 kotori
樣例輸出
error
done
existed
1
2022/03/31
not existed

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int n, x, y, z, op;
map<string, vector<int> > mark; //標記資訊日期
map<string, int> vis;
map<vector<int>, int> number; // 統計某天資訊個數
vector<int> v;
string s;
int dir[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool judge(int x){
	if((x % 400) == 0 || ((x % 4) == 0 && ( x % 100) != 0)) return true;
	return false;
}
int f(int x, int y, int z){
	if(y == 2 && z == 29 && judge(x)) return true;
	if(x < 2022 || x > 9999) return false;
	if(y < 1 || y > 13) return false;
	if(z < 1 || z > dir[y]) return false;
	return true;
}
void add(int x, int y, int z){
	v.clear(); v.push_back(x);
	v.push_back(y); v.push_back(z);
}
int main(){
	cin >> n;
	while(n--){
		cin >> op;
		if(op == 1){
			cin >> x >> y >> z >> s;
			if(!f(x, y, z)) {
				cout << "error" << endl;
			}else if(vis[s] == 1){
				cout << "existed" << endl;
			}else{
				vis[s] = 1;
				add(x, y, z);
				number[v] ++; mark[s] = v;
				cout << "done" << endl;
			}
		}else if(op == 2){
			cin >> x >> y >> z;
			if(!f(x, y, z)){
				cout << "error" << endl;
			}else{
				add(x, y, z);
				cout << number[v] << endl;
			}
		}else{
			cin >> s;
			if(vis[s] == 0){
				cout << "not existed" << endl;
			}else{
				v = mark[s];
				cout << v[0] << '/';
				v[1] < 10 ? cout << '0' << v[1] << '/' : cout << v[1] << '/';
				v[2] < 10 ? cout << '0' << v[2] << endl : cout << v[2] << endl;
			}
		}
	}
    return 0;
}

嚴格上升子序列

給定一個數組,刪除連續的一個子陣列,使得剩餘部分嚴格遞增,最少需要刪掉幾個數
輸入樣例
6
1 3 1 2 4 5
輸出樣例
2
輸入樣例
6
1 2 3 3 4 5
輸出樣例
1
輸入樣例
7
4 5 2 3 4 1 2
輸出樣例
5
預處理前後綴,暴力更新

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
    int n, ans;
    cin >> n;
    vector<vector<int> > dp(3, vector<int>(n + 1, 0));
    dp[1][0] = dp[2][n - 1] = 1;
    ans = n - 1;
    for(int i = 0; i < n; i++){
        cin >> dp[0][i];
        if(i == 0) continue;
        if(i && dp[0][i] > dp[0][i - 1] && dp[1][i - 1] != 0) dp[1][i] = dp[1][i - 1] + 1;
        else dp[1][i] = 0;
        ans = min(ans, n - dp[1][i]);
    }
    for(int i = n - 2; i >= 0; i--){
        if(dp[0][i] < dp[0][i + 1] && dp[2][i + 1] != 0) dp[2][i] = dp[2][i + 1] + 1;
        else dp[2][i] = 0;
        ans = min(ans, n - dp[2][i]);
    }
    int l = -1, r = 0;
    while(r < n && dp[2][r] == 0) r++;
    while(l < n){
        l++;
        if(dp[1][l] == 0) continue;
        while(r < n && dp[0][l] >= dp[0][r]) r++;
        if(r < n && dp[0][l] < dp[0][r]) ans = min(ans, n - dp[1][l] - dp[2][r]);
    }
    cout << ans << endl;
    return 0;
}