标签:style blog http color os io 数据 for
题意从1~N中选k个数,和为s的方案数
第一眼搜索,估计错状态量,又去yydp...浪费大量时间
数据很小的,状态数都不会超过2^N...直接dfs就过了
//state二进制表示选取的数
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn = 200; int N, S, K, cnt; void dfs(int state, int k, int s, int start) { // cout << k << ‘ ‘ << s << endl; if(k == 0 && s == 0) { // for(int i = 0; i < N; i++) // if(state&(1<<i)) cout << (i+1) << ‘ ‘; // cout << endl; cnt++; } if(k == 0) return; for(int i = start; i < N; i++) { if(state&(1<<i)) continue; if(s < i+1) break; dfs(state|(1<<i), k-1, s-(i+1), i+1); } } int main() { #ifdef LOCAL freopen("A.in", "r", stdin); #endif while(scanf("%d%d%d", &N, &K, &S) != EOF) { if(N == 0 && K == 0) break; cnt = 0; dfs(0, K, S, 0); printf("%d\n", cnt); } return 0; }
数据范围很小,直接模拟整个过程就行了。我是把长度、时间都×2,毕竟0.5不好处理。每隔1s计算所有蚂蚁位置,处理相交情况,标记走出通道。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn = 200+20; int n, l, cnt, tcost, ans; int p[maxn], dx[maxn]; bool done[maxn]; int main() { #ifdef LOCAL freopen("B.in", "r", stdin); #endif while(scanf("%d%d", &n, &l) != EOF) { if(n == 0 && l == 0) break; memset(done, 0, sizeof(done)); cnt = 0; for(int i = 1; i <= n; i++) { int x; char dir; getchar(); scanf("%c%d", &dir, &x); if(dir == ‘R‘) dx[i] = 1; else dx[i] = -1; p[i] = x*2; } for(int t = 1; t <= 2*l; t++) { int lastone = 0; for(int i = 1; i <= n; i++) { if(done[i]) continue; p[i] += dx[i]; if(p[i] == 0 || p[i] == 2*l) { if(!lastone || p[i] == 0) lastone = i; done[i] = true; cnt++; } } for(int i = 2; i <= 2*l; i++) { if(i % 2) continue; int x = 0; for(int j = 1; j <= n; j++) { if(done[j] || p[j] != i) continue; if(!x) x = j; else { dx[x] *= -1; dx[j] *= -1; } } } if(cnt == n) { tcost = t; ans = lastone; break; } } //finish cout << tcost/2 << ‘ ‘ << ans << endl; } return 0; }
心态又开始崩了,A了两道就没有认真的看题了...不过这次剩下的题似乎都A不出来呢
UESTC 2014 Summer Training #18 Div.2,布布扣,bubuko.com
UESTC 2014 Summer Training #18 Div.2
标签:style blog http color os io 数据 for
原文地址:http://www.cnblogs.com/gemmeg/p/3901766.html