NOIP 2013 普及组

计数问题
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
	int n, x;
	cin >> n >> x;
	
	int cnt = 0;
	for (int i = 1; i <= n; i ++) {
		for (int j = i; j; j /= 10) {
			if (j % 10 == x) cnt ++;
		}
	}
	cout << cnt << endl;

	return 0;
}
表达式求值
#include <iostream>
#include <cstdio>
using namespace std;

const int N = 1e5 + 10, MOD = 10000;
int stk[N], tt;

int main() {
	int a;
	char op;
	
	cin >> a;
	stk[++tt] = a % MOD;
	while (cin >> op >> a) {
		if (op == '+') stk[++tt] = a % MOD;
		else stk[tt] = a % MOD * stk[tt] % MOD;
	}
	
	int ans = 0;
	while (tt != 0) {
		ans = (ans + stk[tt --]) % MOD;
	}
	cout << ans << endl;
	
	return 0;
}
小朋友的数字
#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;

const int N = 1e6 + 10;
int n, p, x;
LL sum, t[N], f[N];	//特征值, 分数 

int main() {
	scanf("%d %d", &n, &p);
	
	t[0] = -1e18;
	for (int i = 1; i <= n; i ++) {
		scanf("%d", &x);
		sum += x;
		t[i] = max(t[i-1], sum);	//前i个数字中的最大字段和 
		if (sum < 0) sum = 0;
	}
	
	f[1] = t[1] % p;				//题意 
	f[2] = (f[1] + t[1]) % p;		//题意 
	for (int i = 3; i <= n; i ++) {
		f[i] = max(f[i-1], t[i-1] + f[i-1]) % p;
	}

	if (f[1] > 0) cout << f[n];
	else cout << max(f[1], f[n]);
	
	return 0;
}
车站分级 90分代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 1005;
int n, m;
int a[N], lv[N], du[N], e[N][N];
queue <int> q;

void topsort() {
	for (int i = 1; i <= n; i ++) {
		if (du[i] == 0) {
			q.push(i);
			lv[i] = 1;
		}
	}
	
	while (q.size()) {
		int front = q.front();
		q.pop();
		for (int j = 1; j <= n; j ++) {
			if (e[front][j]) {
				du[j] --;
				if (du[j] == 0) {
					q.push(j);
					lv[j] = lv[front] + 1;
				}
			} 
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);
	
	for (int i = 1; i <= m; i ++) {
		int s, b, f, x; 			//共s个站点,起点,终点 
		scanf("%d", &s);
		memset(a, 0, sizeof(a));
		
		for (int j = 1; j <= s; j ++) {
			scanf("%d", &x);
			a[x] = 1;				//本趟列车停靠x站
			if (j == 1) b = x;
			if (j == s) f = x;
		}

		for (int j = b; j <= f; j ++) {
			for (int k = b + 1; k <= f - 1; k ++) {
				if (a[j] && !a[k] && !e[k][j]) {
					e[k][j] = 1;	//level j > level k
					du[j] ++;
				}
			}
		}
	}
	
	topsort();
	
	int ans = 0;
	for (int i = 1; i <= n; i ++) {
		ans = max(ans, lv[i]);
	}
	cout << ans;
	
	return 0;
}
车站分级 100分代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;

const int N = 1005;
int n, m;
int a[N], lv[2*N], du[2*N], e[2*N][2*N];
queue <int> q;

void topsort() {
	for (int i = 1; i <= n + m; i ++) {		//n个真实站台 + m个虚拟站台 
		if (du[i] == 0) {
			q.push(i);
			if (i <= n) lv[i] = 1;			//真实站台的最低等级为1,虚拟站台等级均为0 
		}
	}
	
	while (q.size()) {
		int front = q.front();
		q.pop();
		for (int j = 1; j <= n + m; j ++) {
			if (e[front][j]) {
				du[j] --;
				if (du[j] == 0) {
					q.push(j);
					lv[j] = lv[front] + (front > n);	//虚拟站台 -> 真实站台,等级+1 
				}
			} 
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);
	
	for (int i = 1; i <= m; i ++) {
		int s, b, f, x; 			//共s个站点,起点,终点 
		scanf("%d", &s);
		memset(a, 0, sizeof(a));
		
		for (int j = 1; j <= s; j ++) {
			scanf("%d", &x);
			a[x] = 1;				//本趟列车停靠x站
			if (j == 1) b = x;
			if (j == s) f = x;
		}

		for (int j = b; j <= f; j ++) {
			if (a[j] == 1) {
				e[n+i][j] = 1;		//虚拟站台n+i -> j站台 
				du[j] ++;
			}
			else {
				e[j][n+i] = 1;		//j站台 -> 虚拟站台n+i 
				du[n+i] ++;		
			} 
		}
	}
	
	topsort();
	
	int ans = 0;
	for (int i = 1; i <= n; i ++) ans = max(ans, lv[i]);
	cout << ans;
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值