CCF CSP历年一二题代码汇总
实话说如果不是为了骗访问量,才不会写12题的qwq
201803-1 跳一跳
第一题向来是送分题。。。但是注意读题。。。
模拟题按题意走是坠稳的
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define LL long long 6 #define debug(x) cout << "[" << x << "]" << endl 7 using namespace std; 8 9 int main(){ 10 int ans = 0, last = 0, n; 11 while (scanf("%d", &n) == 1 && n){ 12 if (n == 2){ 13 if (ans == 0 || last == 1){ 14 last = 2; 15 ans += 2; 16 } 17 else { 18 last += 2; 19 ans += last; 20 } 21 } 22 else { 23 ans++; 24 last = 1; 25 } 26 } 27 printf("%d\n", ans); 28 return 0; 29 }
201803-2 碰撞的小球
t只有100,n也只有100,tn^2随便模拟一下。。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define LL long long 6 #define debug(x) cout << "[" << x << "]" << endl 7 using namespace std; 8 9 int a[110], b[110]; 10 11 int main(){ 12 int n, l, t; 13 scanf("%d%d%d", &n, &l, &t); 14 for (int i = 1; i <= n; i++){ 15 scanf("%d", &a[i]); 16 b[i] = 1; 17 } 18 while (t--){ 19 for (int i = 1; i <= n; i++){ 20 for (int j = i+1; j <= n; j++){ 21 if (i == j) continue; 22 if (a[i] == a[j]){ 23 b[i] = -b[i]; 24 b[j] = -b[j]; 25 } 26 } 27 if (a[i] == 0 || a[i] == l) b[i] = -b[i]; 28 } 29 for (int i = 1; i <= n; i++) a[i] += b[i]; 30 } 31 for (int i = 1; i <= n; i++) 32 printf("%d%c", a[i], i == n ? '\n' : ' '); 33 return 0; 34 }
201712-1 最小差值
暴力或排序随便怎么实现都行
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define INF 0x3f3f3f3f 6 #define LL long long 7 #define debug(x) cout << "[" << x << "]" << endl 8 using namespace std; 9 10 int a[1010]; 11 12 int main(){ 13 int n; 14 scanf("%d", &n); 15 for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 16 sort(a+1, a+n+1); 17 int ans = INF; 18 for (int i = 1; i < n; i++) 19 ans = min(ans, a[i+1]-a[i]); 20 printf("%d\n", ans); 21 return 0; 22 }
201712-2 游戏
写个死循环模拟到只剩一个人为止
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define LL long long 6 #define debug(x) cout << "[" << x << "]" << endl 7 using namespace std; 8 9 bool vis[1010]; 10 11 int main(){ 12 int n, k, num = 0, sum = 0; 13 scanf("%d%d", &n, &k); 14 while (1){ 15 for (int i = 1; i <= n; i++){ 16 if (vis[i]) continue; 17 num++; 18 if (num%10 == k || num%k == 0) { 19 vis[i] = 1; 20 sum++; 21 } 22 if (sum == n-1) break; 23 } 24 if (sum == n-1) break; 25 } 26 for (int i = 1; i <= n; i++){ 27 if (!vis[i]){ 28 printf("%d\n", i); 29 break; 30 } 31 } 32 return 0; 33 }
201709-1 打酱油
算就行
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define LL long long 6 #define debug(x) cout << "[" << x << "]" << endl 7 using namespace std; 8 9 int main(){ 10 int n, ans = 0; 11 scanf("%d", &n); 12 n /= 10; 13 ans += n/5*7; 14 n %= 5; 15 ans += n/3*4; 16 n %= 3; 17 ans += n; 18 printf("%d\n", ans); 19 return 0; 20 }
201709-2 公共钥匙盒
看题第一反应线段树做RMQ+树上的二分,看数据范围沉默。。。
此处只需要优先队列存时间点然后暴力模拟,注意重载的优先级
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<queue> 6 #include<cstdlib> 7 #define LL long long 8 #define debug(x) cout << "[" << x << "]" << endl 9 using namespace std; 10 11 struct node{ 12 int id, s, op; 13 bool operator < (const node& a) const { 14 return s > a.s || ((s == a.s && op < a.op) || (s == a.s && op == a.op && id > a.id)); 15 } 16 node(int id = 0, int s = 0, int op = 0): id(id), s(s), op(op){} 17 }; 18 int a[10010]; 19 20 int main(){ 21 priority_queue<node> q; 22 int n, k, w, s, c; 23 scanf("%d%d", &n, &k); 24 for (int i = 1; i <= k; i++){ 25 scanf("%d%d%d", &w, &s, &c); 26 q.push(node(w, s, 0)); 27 q.push(node(w, s+c, 1)); 28 } 29 for (int i = 1; i <= n; i++) a[i] = i; 30 while (!q.empty()){ 31 node u = q.top(); q.pop(); 32 if (u.op == 1){ 33 for (int i = 1; i <= n; i++){ 34 if (a[i] == 0){ 35 a[i] = u.id; 36 break; 37 } 38 } 39 } 40 else { 41 for (int i = 1; i <= n; i++){ 42 if (u.id == a[i]){ 43 a[i] = 0; 44 break; 45 } 46 } 47 } 48 } 49 for (int i = 1; i <= n; i++) 50 printf("%d%c", a[i], i == n ? '\n' : ' '); 51 return 0; 52 }