1.抛硬币
答案:0.50
3.棋盘放置
答案:14
题解:在n*n的棋盘中,象最多可以摆 2 * n - 2 个
5 旅行
答案:7
题解:每个点的方案使其他能到这个点的方案数之和。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int N = 10005;
int a[5];
int main(){
int n; cin >> n;
string s1,s2; cin >> s1 >> s2;
for(int i = 0; i < n; i++){
if(s1[i] - s2[i] == 0) a[1]++;
else if(s1[i] == 'S'){
if(s2[i] == 'R'){
a[2]++;
}else a[0]++;
}else if(s1[i] == 'R'){
if(s2[i] == 'S'){
a[0]++;
}else a[2]++;
}else if(s1[i] == 'P'){
if(s2[i] == 'S'){
a[2]++;
}else a[0]++;
}
}cout << a[0] << " " << a[2] << " " << a[1];
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int N = 10005;
int g[10][10];
int flag;
int jugheng(int x, int y){
for(int i = x + 1; i <= 8; i++){ // xia
if(g[i][y] != 0){
return 1;
}
}
for(int i = x - 1; i >= 0; i--){// shang
if(g[i][y] != 0){
return 1;
}
}
for(int j = y + 1; j <= 8; j++){
if(g[x][j] != 0){
return 1;
}
}
for(int j = y - 1; j >= 0; j--){
if(g[x][j] != 0){
return 1;
}
}return 0;
}
int jugcha(int x, int y){
for(int i = x + 1, j = y + 1; i <= 8 && j <= 8; i++, j++){
if(g[i][j] != 0) return 1;
}
for(int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--){
if(g[i][j] != 0) return 1;
}
for(int i = x + 1, j = y - 1; i <= 8 && j >= 0; i++, j--){
if(g[i][j] != 0) return 1;
}
for(int i = x - 1, j = y + 1; i >= 0 && j <= 8; i--, j++){
if(g[i][j] != 0) return 1;
}
}
void bfs(int x, int y){
//cout << g[x][y] << endl;
if(g[x][y] == 3){
if(jugheng(x,y) == 1){
flag = 1;
}
}else if(g[x][y] == 2){
if(jugcha(x,y) == 1){
flag = 1;
}
}else{
if(jugheng(x,y) == 1 || jugcha(x,y) == 1){
flag = 1;
}
}
}
int main(){
int n; scanf("%d", &n);getchar();
for(int i = 0; i < n; i++){
char c; int x, y;
scanf("%c%d%d", &c, &x, & y);getchar();
if(c == 'Q'){ // huanghou 1
g[x][y] = 1;
}else if(c == 'B'){// xiang
g[x][y] = 2;
}else if(c == 'R'){// che
g[x][y] = 3;
}
//cout << c << endl;
}
for(int i = 1; i <= 8; i++){
for(int j = 1; j <= 8; j++){
if(flag == 1){
printf("Attack!");
return 0;
}
if(g[i][j] != 0) bfs(i,j);
}
}if(flag == 1) printf("Attack!");
else printf("Safe!");
return 0;
}
/*
2
R 1 1
R 2 1
*/
/*
2
R 1 1
R 2 2
*/
/*
2
B 1 1
B 2 1
*/
/*
2
B 1 1
B 2 2
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100005;
int n,m;
int a[N];
bool cmp(int a, int b){
return a > b;
}
int cal(int x){
int sum = 0;
for(int i = 0; i < m; i++){
if(a[i] / x == 0) break;
sum += (a[i] / x);
}if(sum >= n) return 1;
return 0;
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++){
scanf("%d", &a[i]);
}sort(a, a + m, cmp);
int l = 1, r = N;
while(l < r){
int mid = (l + r) / 2;
if(cal(mid) == 0){
r = mid;
}else{
l = mid + 1;
}
}cout << --l;
return 0;
}
/*
4 1
4
*/
/*
1 1
4
*/
/*
4 2
4 5
*/
8 养猫
**题解:**构造哈夫曼树,这里可以用优先队列来解决
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
int n;scanf("%d", &n);
for(int i = 0; i < n; i++){
int t;scanf("%d", &t); q.push(t);
}int sum = 0;
while(q.size() > 1){
int a = q.top(); q.pop();
int b = q.top(); q.pop();
sum += (a + b);
q.push((a + b));
}printf("%d", sum);
return 0;
}
10 突破障碍
题解:利用广搜配合双端队列去找,碰到#放到后端, .和T放到前端,然后最先找到的就是用步长最少的,其中步长指的是#的个数
#include <iostream>
#include <cstring>
#include <deque>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 305;
int n,m;
char g[N][N];
int bi,bj,ei,ej;
int vis[N][N];
int ans;
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1 , -1, 0, 0};
struct node{
int x,y;
int cnt;
};
deque<node> q;
void bfs(){
q.push_back(node{bi,bj,0});
node t;
while(q.size()){
t = q.front(); q.pop_front();
if(t.x == ei && t.y == ej){
// cout << t.cnt << endl;
ans = t.cnt;
// cout << ans << endl;
return;
}
vis[t.x][t.y] = 1;
for(int i = 0; i < 4; i++){
int x = t.x + dx[i];
int y = t.y + dy[i];
if(x >= 0 && y >= 0 && x < n && y < m && vis[x][y] == 0 ){
if(g[x][y] == '.' || g[x][y] == 'T'){
q.push_front(node{x, y, t.cnt});
}else{
q.push_back(node{x,y, t.cnt + 1});
}
}
}
}
}
int main(){
scanf("%d%d", &n, &m); getchar();
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
scanf("%c", &g[i][j]);
if(g[i][j] == 'S') bi = i, bj = j;
if(g[i][j] == 'T') ei = i, ej = j;
}getchar();
}
bfs();
printf("%d", ans);
return 0;
}
``