2020 - 3 - 18(天气 - 微凉)
蓄势待发篇hh
1852:Ants
描述
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
输入
The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
输出
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
样例输入
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
样例输出
4 8
38 207
Ac代码:
#include<iostream>
using namespace std;
const int N = 1e6+10;
int x[N];
int main(){
int T;
cin >> T;
while(T --)
{
int L,n;
cin >> L >> n;
//蚂蚁位置
for(int i = 0;i < n;i++) cin >> x[i];
int minT = 0;
//最少花费时间等于 = 每个ant距离两端最小距离与相邻蚂蚁最小距离比较的较大值,取较大值是可以将比它小的包进去让它提前掉落然后总体可以保证最小
for(int i = 0;i < n;i++){
minT = max(minT,min(x[i],L - x[i]));
}
int maxT = 0;
//最大花费时间等于 = 每个ant距离两端的最大距离与相邻ant的最大值比较再取最大
for(int i = 0;i < n;i++){
maxT = max(maxT,max(x[i],L - x[i]));
}
cout << minT << " " << maxT << endl;
}
return 0;
}
2020 - 3 - 19(天气 - 有点热)
深度优先搜索(DFS - Depth-First Search)
它从某个状态开始,不断转移状态直到无法转移,然后退回到其他状态,如此不断重复,直到找到
例题:
Ac代码
#include<iostream>
using namespace std;
const int N = 25;
int n,k;
int a[N];
//已经从前u项得到了和sum,然后对于i项之后的进行分支
bool dfs(int u,int sum){
//如果前n项都计算过了,则返回sum是否与k相等
if(u == n)
return sum == k;
//不加上a[i]的情况
if(dfs(u + 1,sum + a[u]))
return true;
//加上a[i]的情况
if(dfs(u + 1,sum))
return true;
//没有任何一种情况符合相加得k
return false;
}
int main(){
cin >> n;
for(int i = 0;i < n;i++) cin >> a[i];
cin >> k;
if(dfs(0,0)) cout <<"Yes" << endl;
else cout << "No" << endl;
return 0;
}
2020 - 3 - 20(天气 - 晴)
Ac:
#include<iostream>
using namespace std;
const int N = 110;
int n,m;
char map[N][N];
void dfs(int x,int y){
map[x][y] = '.';
for(int i = -1;i <= 1;i++){//这里小于等于1忘记写了
for(int j = -1;j <= 1;j++){
int nx = x + i,ny = y + j;
if(nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] == 'W')
//这里感觉移动(0,0)没用 想去掉这层递归 但是答案就不对了希望有大佬看到能解答下
//if(i == j && i)
dfs(nx,ny);
}
}
return;
}
int main(){
cin >> n >> m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin >> map[i][j];
}
}
int res = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(map[i][j] == 'W'){
dfs(i,j);
res ++;
}
}
}
cout << res << endl;
return 0;
}
2020 - 3 - 20(天气 - 晴)
AC:
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
const int N = 110,INF = -1;
typedef pair<int,int> PII;
char maze[N][N];
int d[N][N];
int n,m,ax,ay,bx,by;
int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};
int bfs(){
queue<PII> q;
d[ax][ay] = 0;
q.push({ax,ay});
while(q.size()){
PII t = q.front();
q.pop();
if (t.first == bx && t.second == by) {
break;
}
for(int i = 0;i < 4;i++){
int x = t.first + dx[i],y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && maze[x][y] != '#' && d[x][y] == -1)
{
q.push({x,y});
d[x][y] = d[t.first][t.second] + 1;
}
}
}
return d[bx][by];//这里我把d写成maze得71死活不对 然后一个字母一个字母调了三个小时欲哭无泪没想到这里会错
}
int main(){
cin >> n>> m;
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++)
{
cin >> maze[i][j];
d[i][j] = -1;
if(maze[i][j] == 'S'){
ax = i;ay = j;
}
if(maze[i][j] == 'G'){
bx = i;by = j;
}
}
cout << bfs() << endl;
return 0;
}
3-27(天气-阴)
AC
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
const int N = 101;
int c[N],A;
int main(){
int coin[6] = {1,5,10,50,100,500};
for(int i = 0;i < 6;i++){
cin >> c[i];
}
cin >> A;
int res = 0;
for(int i = 5;i >= 0;i--){
int t = min(c[i],A/coin[i]);
A -= t * coin[i];
res += t;
}
cout << res << endl;
return 0;
}
AC:
个人代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
const int N = 1e5 + 10;
int c[N],A;
int a[N],b[N];
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
for(int i = 1;i <= n;i++){
cin >> b[i];
}
int res = 0;
for(int i = 1;i <= n;i++){
int j = 0;
while(b[i] > a[j + i + 1]){
j ++;
}
i += j;
res ++;
}
cout << res << endl;
return 0;
}
参考书代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
const int N = 1e5 + 10;
int c[N],A;
int a[N],b[N];
typedef pair<int,int> P;
P p[N];
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++) cin >> b[i];
//b数组中结束时间早的放在前面
for(int i = 1;i <= n;i++){
p[i].first = b[i],p[i].second = a[i];
}
sort(p+1,p+n+1);
//t表示最后工作的结束时间
int res = 0,t = 0;
for(int i = 1;i <= n;i++){
if(t < p[i].second)
{
res ++;
t = p[i].first;
}
}
cout << res << endl;
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
const int N = 1e5 + 10;
int c[N],A;
int a[N],b[N];
typedef pair<int,int> P;
P p[N];
char s[N];
int n;
void solve(){
int a = 0,b = n - 1;
bool l = false;
while(a <= b){
cout << a <<" " << b << endl;
if(s[a] == s[b]){
cout << s[a];
a+=1;
}else if(s[a] > s[b]){
putchar(s[b--]);
cout << endl;
}else if(s[a] < s[b]){
putchar(s[a++]);
cout << endl;
}
}
}
int main(){
cin >> n;
for(int i = 0;i < n;i++) cin >> s[i];
solve();
return 0;
}//感觉有个小bug就是相同的取前面的还是取末尾的 这会影响整体的字典序
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
const int N = 1e5 + 10;
int c[N],r;
int a[N],b[N];
typedef pair<int,int> P;
P p[N];
char s[N];
int n;
int main(){
cin >> n >> r;
for(int i = 0;i < n;i++) cin >> a[i];
int i = 0,res = 0;
while(i < n){
int c = a[i ++];
while(i < n && a[i] - c <= r) i ++;
int p = a[i - 1];
while(i < n && a[i] - p <= r) i ++;
res ++;
}
cout << res << endl;
return 0;
}
2020 - 3 - 29(天气 - 小雨)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e4 + 10;
int a[N];
int main(){
LL ans = 0;
int n;
cin >> n;
for(int i = 0;i < n;i++) cin >> a[i];
while(n - 1){
int m1 = 0,m2 = 1;
if(a[m1] > a[m2]) swap(m1,m2);
for(int i = 2;i < n;i ++){
if(a[i] < a[m1]){
m2 = m1;
m1 = i;
}else if(a[i] < a[m2]){
m2 = i;
}
}
//merge
int t = a[m1] + a[m2];
ans += t;
if(m1 == n - 1){//这特判是我没看懂的
swap(m1,m2);
}
a[m1] = t;
a[m2] = a[n - 1];
n --;
}
cout << ans;
return 0;
}