A
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;
int main(){
int n, k; scanf("%d%d", &n, &k);
int cnt = 0;
for(int i = 0; i < n; ++i) {
int x; scanf("%d", &x);
cnt += x;
if(cnt >= 8) k -= 8, cnt -= 8;
else k -= cnt, cnt = 0;
if(k <= 0) {
printf("%d\n", i + 1);
return 0;
}
}
puts("-1");
return 0;
}
B
题意:给出n行,每行有8个座位, {1, 2} {3 4} {4, 5} {5, 6} {7, 8} 一行中这些位置算相邻,给出k个不同部队的士兵,要求不同部队的士兵部能坐相邻的位置,问能否达到这个目的
昨晚- - 我也被叉掉了、
就很尴尬、
题解的思路很清晰、比我的要清晰多了QAQ
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;
int has[5], cnt[5];
int main(){
int n, k; scanf("%d%d", &n, &k);
has[2] = n * 2, has[4] = n;
for(int i = 0; i < k; ++i) {
int x; scanf("%d", &x);
while(x >= 3) {
if(has[4] > 0) {
has[4]--, x -= 4;
} else if(has[2] > 0) {
has[2]--, x -= 2;
} else {
return puts("NO"), 0;
}
}
if(x > 0) cnt[x]++;
}
while(cnt[2] > 0) {
if(has[2] > 0) {
has[2]--, cnt[2]--;
} else if(has[4] > 0) {
cnt[2]--, has[4]--, has[1]++;
} else {
cnt[2]--, cnt[1] += 2;
}
}
if(cnt[1] > has[1] + has[2] + has[4] * 2) {
puts("NO");
} else {
puts("YES");
}
return 0;
}
C
题意:有n个节点n - 1条边,一头马在结点1,这头马有个习惯,只会走之前没有走过的结点,问最后深度的期望值
思路:只要注意到马在的当前节点有多少个子结点,它前往任何一个子结点的概率就是到达当前结点的概率 * (1 / 子结点数量)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;
int dep[qq], num[qq];
vector<int> G[qq];
double ans = 0;
void Dfs(int u, int fa, int dis, double babl) {
dep[u] = dis;
int len = G[u].size();
for(int i = 0; i < len; ++i) {
int v = G[u][i];
if(v == fa) continue;
if(u != 1) Dfs(v, u, dis + 1, babl * ((1.0) / (len - 1)));
else Dfs(v, u, dis + 1, babl * ((1.0) / (len)));
num[u] += num[v];
}
num[u] = num[u] + 1;
if(num[u] == 1) {
ans += dep[u] * 1.0 * babl;
}
}
int main(){
int n; scanf("%d", &n);
for(int i = 1; i < n; ++i) {
int a, b; scanf("%d%d", &a, &b);
G[a].pb(b), G[b].pb(a);
}
Dfs(1, -1, 0, 1);
printf("%.12lf\n", ans);
return 0;
}