第六届“中国高校计算机大赛-团体程序设计天梯赛”

L1

L1-1 人与神

#include
using namespace std;
int main(){
         
    printf("To iterate is human, to recurse divine.\n");
    return 0;
}

L1-2 两小时学完C语言

#include
using namespace std;
int main(){
     
    int N, K, M;
    scanf("%d %d %d", &N, &M, &K);
    printf("%d\n", N-M*K);
    return 0;
}

L1-3 强迫症

#include
using namespace std;
int y, m;
int main(){
     
    string s, res;
    cin >> s;
    if(s.length() == 6){
     
        res = s.substr(0, 4); // 子串
        res += "-";
        res += s.substr(4, 2);        
    }
    else{
             
        int y = 0;
        y += s[1] - '0';
        y += (s[0] - '0')*10;
        if(y >= 22) res += "19";
        else res += "20";
        res += s.substr(0, 2);
        res += "-";
        res += s.substr(2, 2);
    }
    cout << res << endl;
    return 0;
}
#include
using namespace std;
int y, m;
int main(){
     
    string s;
    cin >> s;
    if(s.length() == 6){
     
		cout << s.substr(0, 4) << "-" << s.substr(4, 2) << endl;        
    }
    else{
             
        int y = stoi(s.substr(0,2)); // s to i:字符串转int,此外还有stol、stoll
        cout << (y >= 22 ? "19" : "20") << s.substr(0,2) <<  "-" << s.substr(2, 2) << endl; 
    }
    return 0;
}

L1-4 降价提醒机器人

#include
using namespace std;
int n, m;
int main(){
     
    double p;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
     
        cin >> p;
        if(p < m) printf("On Sale! %.1f\n", p);
    }
    return 0;
}

L1-5 大笨钟的心情

#include
using namespace std;
int n[24], m;
int main(){
     
    for(int i = 0; i < 24; i++) cin >> n[i];
    cin >> m;
    while(m <= 23 && m >= 0){
     
        printf("%d %s\n", n[m], n[m] > 50 ? "Yes" : "No");
        cin >> m;
    }
    return 0;
}

L1-6 吉老师的回归

繁琐的思路

#include
using namespace std;
string s;
int n, m, cnt1 = 0, cnt2 = 0;
int main(){
     
    cin >> n >> m;
    getline(cin, s);
    for(int i = 0; i < n; i++){
     
        getline(cin, s);
        if(s.find("qiandao") != s.npos || s.find("easy") != s.npos) cnt1++;
        else cnt2++;
        if(cnt2 > m) break;
    }
    if(cnt2 <= m) cout << "Wo AK le" << endl;
    else cout << s << endl;
    return 0;
}

简洁的思路【wzt提供】

#include
using namespace std;
string s;
int n, m;
int main(){
     
    cin >> n >> m;
    getline(cin, s); // 吃掉第一个的换行符
    for(int i = 0; i < n; i++){
     
        getline(cin, s); // 读题
        if(s.find("qiandao") == s.npos && s.find("easy") == s.npos) m--;
        if(m == -1)break;
    }
    if(m > -1) cout << "Wo AK le" << endl;
    else cout << s << endl;
    return 0;
}

L1-7 天梯赛的善良

排序1(桶排)

#include
using namespace std;
int r[1000001], n, b;
int main(){
     
    cin >> n;
    memset(r, 0, sizeof r);
    for(int i = 0; i < n; i++){
     
        scanf("%d", &b);
        r[b]++;
    }
    b = 1;
    while(r[b] == 0) b++;
    cout << b << " " << r[b] << endl;
    b = 1000001;
    while(r[b]== 0) b--;
    cout << b << " " << r[b] << endl;
    return 0;
}

排序2

最大、最小同时计数

L1-8 乘法口诀数列

#include
using namespace std;
int r[1010], n, m, k = 3;
int main(){
     
    cin >> r[1] >> r[2] >> n;
    for(int i = 3; i <= n; i++){
     
        m = r[i-1]*r[i-2];
        if(m < 10) r[k] = m, k++;
        else r[k+1] = m%10, r[k] = m/10, k+=2;
    }
    printf("%d", r[1]);
    for(int i = 2; i <= n; i++) printf(" %d", r[i]);
    printf("\n");
    return 0;
}

L2

L2-1 包装机

队列+堆栈

#include
using namespace std;
deque<char> q[1001];
stack<char> s;
deque<char> r;
int N, M, S;
int main(){
     
    char ch;
    cin >> N >> M >> S;
    scanf("%c", &ch);
    for(int i = 1; i <= N; i++){
     
        scanf("%c", &ch);
        while(ch != '\n'){
     
            q[i].push_back(ch);
            scanf("%c", &ch);
        }
    }
    int d;
    while(cin >> d){
     
        if(d == -1) break;
        if(d == 0) {
     
            if(s.size() > 0)
                r.push_back(s.top()), s.pop();
        }
        else{
     
            if(q[d].size() > 0){
     
                if(s.size() == S){
     
                    r.push_back(s.top());
                    s.pop();
                }
                s.push(q[d].front());
                q[d].pop_front();
            }
        }
    }
    while(r.size() > 0){
     
        cout << r.front();
        r.pop_front();
    }
    cout << endl;
    return 0;
}

L2-2 病毒溯源

dfs

在这里插入代码片

bfs

#include
using namespace std;
#define MXN 10010
vector<int> v[MXN];
int h[MXN], fa[MXN], cnt[MXN];
deque<int> r;
int N;
int main(){
     
    int a, M;
    memset(fa, -1, sizeof fa);
    cin >> N;
    for(int i = 0; i < N; i++){
     
        cin >> M;
        cnt[i] = M;
        if(M == 0) r.push_back(i), h[i] = 1;
        for(int j = 0; j < M; j++){
     
            scanf("%d", &a);
            v[i].push_back(a);
            fa[a] = i;
        }
    }
    while(r.size()){
     
        a = r.front();
        if(h[a]+1 > h[fa[a]]) h[fa[a]] = h[a]+1;
        cnt[fa[a]]--;
        if(cnt[fa[a]] == 0) r.push_back(fa[a]);
        r.pop_front();
    }
    for(int i = 0; i < N; i++){
     
        if(fa[i] == -1){
     
            a = i;
            break;
         }
    }
    cout << h[a] << endl;
    while(h[a] > 1){
     
        printf("%d ", a);
        sort(v[a].begin(), v[a].end(), [](int x, int y){
     
            if(h[x] == h[y]) return x < y;
            else if(h[x] > h[y]) return true;
            else return false;
        });
        a = v[a][0];
    }
    printf("%d\n", a);
    return 0;
}

L2-3 清点代码库

多关键字排序

#include
using namespace std;
#define MXN 10005
#define MXM 105
int N, M, a[MXN], ans[MXN][MXM], cnt =0;
vector<int> res[MXN];
void dfs(int m, int s, int t){
     
    if(s == t || m == M){
     
        cnt++;
        res[t-s+1].push_back(a[s]);
        return;
    }
    sort(a+s, a+t+1, [m](int x, int y){
     
        return ans[x][m] < ans[y][m];
    });
    int ts = s, tt = s;
    while(ts <= t){
     
        while(tt < t){
     
            if(ans[a[tt+1]][m] == ans[a[ts]][m])
                tt++;
            else break;
        }
        dfs(m+1, ts, tt);
        tt++;
        ts = tt;
    }
}
int main(){
     
    cin >> N >> M;
    for(int i = 0; i < N; i++){
     
        a[i] = i;
        for(int j = 0; j < M; j++){
     
            scanf("%d", ans[i]+j);
        }
    }
    dfs(0, 0, N-1);
    printf("%d\n", cnt);
    for(int i = N; i > 0; i--){
     
        if(res[i].size() == 0) continue;        
        for(int j = 0; j < res[i].size(); j++){
     
            printf("%d ", i);
            printf("%d", ans[res[i][j]][0]);
            for(int k = 1; k < M; k++){
     
                printf(" %d",ans[res[i][j]][k]);
            }
            printf("\n");
        }
    }
    return 0;
}

使用upper_bound

在这里插入代码片

L2-4 哲哲打游戏

vector存储

#include
using namespace std;
#define MXN 100005
#define MXS 105
int N, M, a[MXS];
vector<int> vs[MXN];

int main(){
     
    cin >> N >> M;
    int k, tmp;
    for(int i = 1; i <= N; i++){
             
        scanf("%d", &k);
        for(int j = 1; j <= k; j++){
     
            scanf("%d", &tmp);
            vs[i].push_back(tmp);
        }
    }
    int type, op, cs = 1;
    for(int i = 1; i <= M; i++){
     
        scanf("%d %d", &type, &op);
        if(type == 1) a[op] = cs, printf("%d\n", cs);
        else if(type == 2) cs = a[op];
        else cs = vs[cs][op-1];
    }
    printf("%d\n", cs);
    return 0;
}

数组存储

在这里插入代码片

L3

L3-1 森森旅游

线段树【HC提供】

#include 
using namespace std;
using ll = long long;
const int maxn = 2e5 + 5;
struct edge{
     
	int next, to;
	ll w[2];
}p[maxn][2];
int tot[2], head[maxn][2], vis[maxn];
ll a[maxn], dis1[maxn], dis2[maxn], ans[maxn];
void add(int u, int v, ll w1, ll w2) {
     
	p[++tot[0]][0].next = head[u][0];
	p[tot[0]][0].to = v;
	p[tot[0]][0].w[0] = w1;
	p[tot[0]][0].w[1] = w2;
	head[u][0] = tot[0];
	
	p[++tot[1]][1].next = head[v][1];
	p[tot[1]][1].to = u;
	p[tot[1]][1].w[0] = w1;
	p[tot[1]][1].w[1] = w2;
	head[v][1] = tot[1];
}
int n, m, q;
struct node{
     
	int id;
	ll val;
	node(int x, ll y):id(x), val(y){
     }
	bool operator < (const node & rhs) const {
     
		return val > rhs.val; 
	} 
};
void solve(int st, ll dis[]) {
     
	for (int i = 1; i <= n; i++) {
     
		dis[i] = 2e18;
		vis[i] = 0;
	}
	dis[st] = 0;
	int op = (st == 1? 0: 1);
	priority_queue<node> Q;
	Q.push(node(st, 0ll));
	while (!Q.empty()) {
     
		node now = Q.top();
		Q.pop();
		int u = now.id;
		if (vis[u]) {
     
			continue;
		}
		vis[u] = 1;
		for (int i = head[u][op]; i; i = p[i][op].next) {
     
			int v = p[i][op].to;
			if (dis[v] > dis[u] + p[i][op].w[op]) {
     
				dis[v] = dis[u] + p[i][op].w[op];
				Q.push(node(v, dis[v]));
			}
		}
	}
}
struct seg{
     
	int l, r;
	ll ans;
}tree[maxn << 1];
void pushup(int rt) {
     
	tree[rt].ans = min(tree[rt << 1].ans, tree[rt << 1 | 1].ans);
}
void build(int l, int r, int rt) {
     
	tree[rt].l = l;
	tree[rt].r = r;
	if (l == r) {
     
		tree[rt].ans = ans[l];
		return;
	}
	int m = (l + r) >> 1;
	build(l, m, rt << 1);
	build(m + 1, r, rt << 1 | 1);
	pushup(rt);
}
void update(int rt, int x) {
     
	int l = tree[rt].l, r = tree[rt].r;
	if (l == r) {
     
		tree[rt].ans = ans[x];
		return;
	}
	int m = (l + r) >> 1;
	if (x <= m) {
     
		update(rt << 1, x);
	} else {
     
		update(rt << 1 | 1, x);
	}
	pushup(rt);
}
int main(){
     
	cin >> n >> m >> q;
	int u, v;
	ll w1, w2;
	for (int i = 1; i <= m; i++) {
     
		scanf("%d%d%lld%lld", &u, &v, &w1, &w2);
		if (u == v) {
     
			continue;
		}
		add(u, v, w1, w2);
	}
	for (int i = 1; i <= n; i++) {
     
		scanf("%lld", a + i);
	}
	solve(1, dis1);
	solve(n, dis2);
	for (int i = 1; i <= n; i++) {
     
		if (dis1[i] >= 2e18 || dis2[i] >= 2e18) {
     
			ans[i] = 2e18;
			continue;
		}
		ans[i] = dis1[i] + dis2[i] / a[i];
		if (dis2[i] % a[i]) {
     
			ans[i]++;
		}
	}
	build(1, n, 1);
	while (q--) {
     
		int x;
		ll y;
		scanf("%d%lld", &x, &y);
		a[x] = y;
		if (dis1[x] < 2e18 && dis2[x] < 2e18) {
     
			ans[x] = dis1[x] + dis2[x] / a[x];
			if (dis2[x] % a[x]) {
     
				ans[x]++;
			}
			update(1, x);
		}
		printf("%lld\n", tree[1].ans);
	}
	return 0;
}

L3-2 还原文件

【xyc提供】

#include
using namespace std;
typedef unsigned long long ll;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const int p = 1e5+7;
ll dp[maxn],num[maxn],cyc[maxn],len[105],d;
int main(){
     
	int n,tval;scanf("%d",&n);
	cyc[0]=num[0]=1;
	for(int i=1;i<=n;i++){
     
		scanf("%d",&tval);
		cyc[i]=cyc[i-1]*p;
		num[i]=num[i-1]*p+tval;
	}
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++){
     
		scanf("%ulld",len+i);
		d=0;
		for(int j=1;j<=len[i];j++){
     
			scanf("%d",&tval); 
			d=d*p+tval;
		}
		for(int j=n;j>=len[i];j--){
     
			if(d==num[j]-num[j-len[i]]*cyc[len[i]]){
     
				dp[j]=i;
			}
		}
	}
//	for(int i=1;i<=n;i++){
     
//		cout<
//	}
	stack<int> ans;
	int now=n;
	while(dp[now]){
     
		ans.push(dp[now]);
		now=now-len[dp[now]]+1;
	}
	int flag=0;
	while(ans.size()){
     
		if(flag) cout<<" ";
		cout<<ans.top();
		ans.pop();
		flag=1;
	}
}
#include
using namespace std;
#define MXN 100010
#define MXM 110
int N, n[MXN], M;
map<int, int> hs;
struct ZT{
     
    int bh = 0;
    bool vis = false;
    vector<int> m;
    unordered_map<int, bool> mp;
    bool match(int p){
     
        if(mp.count(p)) return mp[p];
        else mp[p] = true;
        for(int i = 0; i < m.size(); i++){
     
            if(m[i] == n[p+i]) continue;
            mp[p] = false;
            break;
        }
        return mp[p];
    }
}zt[MXM];
deque<int> ans; // zt的下标
bool dfs(int p){
     
    if(ans.size() == M) return true;
    int ts = hs[n[p]];
    while(zt[ts].m[0] == n[p]){
     
        if(!zt[ts].vis && zt[ts].match(p)){
     
            ans.push_back(zt[ts].bh);
            zt[ts].vis = true;
            if(dfs(p+zt[ts].m.size()-1)) return true;
            else zt[ts].vis = false, ans.pop_back();
        } 
        ts++;
    }    
    return false;
}
int main(){
     
	cin >> N;
    for(int i = 1; i <= N; i++) scanf("%d", n+i);
    cin >> M;
    for(int i = 1; i <= M; i++){
     
        zt[i].bh = i;
        int cnt, tmp;
        scanf("%d", &cnt);
        for(int j = 1; j <= cnt; j++) {
     
            scanf("%d", &tmp);
            zt[i].m.push_back(tmp);
        }
    }
    sort(zt+1, zt+M+1, [](ZT x, ZT y){
      return x.m[0] < y.m[0];});
    for(int i = 1; i <= M; i++) if(hs.count(zt[i].m[0]) == 0) hs[zt[i].m[0]] = i;
    dfs(1);
    cout << ans.front();
    ans.pop_front();
    while(ans.size()){
     
        cout << " " << ans.front();
        ans.pop_front();
    }
    cout << endl;
    return 0;
} 

你可能感兴趣的:(第六届“中国高校计算机大赛-团体程序设计天梯赛”)