lqgs2

163 阅读15分钟

6月6日

计算同一行或同一列的细胞 *

www.luogu.com.cn/problem/CF6…

dfs

#include <iostream>
using namespace std;
#define ll long long
char g[110][110];
bool vis[110][110];
ll res = 0;
int n, i, j;
bool check(int x, int y) {
	if(x == i && y < n)
		return 1;
	else if(y == j && x < n)
		return 1;
	return 0;
}
void dfs(int x, int y) {
	if(check(x, y)) {
		if(g[x][y] == 'C' && !vis[x][y])
			res++;
		dfs(x, y+1);
		dfs(x+1, y);
	}
}
int main() {
	cin >> n;
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			cin >> g[i][j];
		}
	}
	for(i = 0; i < n; ++i) {
		for(j = 0; j < n; ++j) {
			if(g[i][j] == 'C') {
				vis[i][j] = 1;
				dfs(i,j);
				vis[i][j] = 0;
			}
		}
	}
	cout << res;
	return 0;
} 

bfs染色法 *

www.luogu.com.cn/problem/CF1…

#include <iostream>
using namespace std;
#define ll long long
int n, m;
char source[1100][1100], target[1100][1100];
int dx[] = {-1,-1,0,1,1,1,0,-1};
int dy[] = {0,1,1,1,0,-1,-1,-1}; 
bool check() {
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			if(target[i][j] != source[i][j])
				return false;
		}
	}
	return true;
}
int main() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			cin >> target[i][j];
			source[i][j] = '.';
		}
	}
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			int flag = 1;
			for(int k = 0; k < 8; ++k) {
				int xx = dx[k] + i;
				int yy = dy[k] + j;
				//
				if(xx>n || xx<=0 || yy>m || yy<=0)
				{
					flag = 0;
					break;
				}
				else if(target[xx][yy] != '#')
				{
					flag = 0;
					break;
				}
			}
			if(!flag)
					continue;
			for(int k = 0; k < 8; ++k) {
				int xx = dx[k] + i;
				int yy = dy[k] + j;
				source[xx][yy] = '#';
			}
		}
	}
	if(check())
		cout << "YES";
	else
		cout << "NO";
	return 0;
} 

字符串搜索 *

www.luogu.com.cn/problem/CF2…

#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
#define ll long long
string s;
int n, t;
int res = 0;
void print() {
	for(int i = 0; i < s.size(); ++i)
		cout << s[i];
}
void dfs(int x) {
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] == 'B' && s[i+1] == 'G') {
			s[i] = 'G';
			s[i+1] = 'B';
			i++;
		}
	}
	res++;
	if(res == t)
		print();
	else
		dfs(x+1);
}
int main() {
	cin >> n >> t;
	cin >> s;
	dfs(0);
	return 0;
} 

6月7日

给定一个长度为 n 的字符串,求最大的位置集合,每个位置都是小写字母且各不相同,每两个位置之间没有大写字母。

www.luogu.com.cn/problem/CF8…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
char s[210];
set<char> st;
int main() {
	int n;
	cin >> n;
	cin >> s;
	int res = 0, sum = 0;
	for(int i = 0; i < n; ++i) {
		if(islower(s[i]))
			st.insert(s[i]);
		else {
			sum = st.size();
			st.clear();
			res = max(sum, res);
		}
	}
	sum = st.size();
	res = max(sum,res);
	cout << res;
	return 0;
} 

广搜-最短路

www.luogu.com.cn/problem/CF8…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
/*
	最短路径:
	1. 如果走过了该点,然后再回头走这个点肯定不是最优的
	2. 他走到的这个点除了他走过来的那个点以外,其他的点有一个被走过就表明不是最优的 
*/
bool vis[300][300];
int sx, sy;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1}; 
string s;
//check函数: true表示不是最优的,false表示最优的 
bool check(int x, int y) {
	if(vis[x][y]) //如果走过,表示不是最优的 
		return true;
	int res = 0; //记录周围有多少个格子被走过了 
	for(int i = 0; i < 4; ++i) {
		int xx = dx[i] + x;
		int yy = dy[i] + y;
		//如果走过,res就加1
		if(vis[xx][yy])
			res++; 
	} 
	return (res > 1);
}
int main() {
	cin >> s;
	sx = sy = 101;
	int len = s.size();
	for(int i = 0; i < len; ++i) {
		vis[sx][sy] = 1; //标记 
		if(s[i]=='L') {
			sy--;
			if(check(sx,sy)) {
				cout << "BUG";
				return 0; 
			} 
		} else if(s[i]=='R') {
			sy++;
			if(check(sx,sy)) {
				cout << "BUG";
				return 0; 
			} 
		} else if(s[i]=='D') {
			sx++;
			if(check(sx,sy)) {
				cout << "BUG";
				return 0; 
			} 
		} else {
			sx--;
			if(check(sx,sy)) {
				cout << "BUG";
				return 0; 
			} 
		}
	}
	cout << "OK";
	return 0;
} 

棋盘-深搜

www.luogu.com.cn/problem/CF4…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
char g[110][110];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int n, m;
using namespace std;
void dfs(char c, int x, int y) {
	g[x][y] = c; //染色
	//遍历周围四个点 
	for(int i = 0; i < 4; ++i) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		char tmp = (c == 'B' ? 'W' : 'B');
		//没有越界 
		if(g[xx][yy]=='.' && xx >= 0 && xx <= n && yy >= 0 && yy <= m) {
			dfs(tmp, xx, yy);
		}
	} 
}
int main() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			cin >> g[i][j];
		}
	}
	
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			if(g[i][j] == '.') {
				dfs('B', i, j);
			}
		}
	}
	
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			cout << g[i][j];
		}
		cout << endl; 
	}
	return 0;
} 

6月8日

Lucky Number(确定答案范围,打表)

CF96B Lucky Numbers (easy) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <algorithm>
#define ll long long
const int INF = 1e9;
using namespace std;
bool contain47(int n) {
	int flag = 1;
	while(n) {
		if(n % 10 != 4 && n % 10 != 7) {
			flag = 0;
			break;
		} 
		n /= 10;
	}
	return flag;
}
int getW4(int n) {
	int res = 0;
	while(n) {
		if(n % 10 == 4)
			res++;
		n /= 10;
	}
	return res;
}
int getW7(int n) {
	int res = 0;
	while(n) {
		if(n % 10 == 7)
			res++;
		n /= 10;
	}
	return res;
}
int nw(int n) {
	int res = 0;
	while(n) {
		n /= 10;
		res++;
	}
	return res;
}
ll a[] = {47,74,4477,4747,4774,7447,7474,7744,444777,447477,447747,447774,474477,474747,474774,477447,477474,477744,744477,744747,744774,747447,747474,747744,774447,774474,774744,777444,44447777,44474777,44477477,44477747,44477774,44744777,44747477,44747747,44747774,44774477,44774747,44774774,44777447,44777474,44777744,47444777,47447477,47447747,47447774,47474477,47474747,47474774,47477447,47477474,47477744,47744477,47744747,47744774,47747447,47747474,47747744,47774447,47774474,47774744,47777444,74444777,74447477,74447747,74447774,74474477,74474747,74474774,74477447,74477474,74477744,74744477,74744747,74744774,74747447,74747474,74747744,74774447,74774474,74774744,74777444,77444477,77444747,77444774,77447447,77447474,77447744,77474447,77474474,77474744,77477444,77744447,77744474,77744744,77747444,77774444,4444477777};
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int n;
	cin >> n;
	/*
	//打表 
	for(ll i = 1; i <= INF; ++i) {
		if(contain47(i) && getW4(i) == getW7(i) && ((getW4(i) + getW7(i)) % 2 == 0)) {
			cout << i << ",";
		}	
	}
	*/
	for(int i = 0; ; ++i) {
		if(a[i] >= n) {
			cout << a[i];
			break;
		}
	}
	return 0;
} 

President's Office (DFS)

www.luogu.com.cn/problem/CF6…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int n, m;
char g[110][110];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int vis[110][110];
char p;
set<char> st;
void dfs(int x, int y) {
	if(vis[x][y])
		return;
	vis[x][y] = 1;
	for(int i = 0; i < 4; ++i) {
		int xx = dx[i] + x;
		int yy = dy[i] + y;
//		if(xx < 1 || yy < 1 || xx > n || yy > m)
//			continue;
//		if(g[xx][yy] == '.' || vis[xx][yy])
//			continue;
//		if(g[xx][yy] == p || g[xx][yy] == ' ')
//			continue;
////		cout << xx << " " << yy << " " << g[xx][yy] << endl;
//		if(g[xx][yy] != '.' && g[xx][yy] != p && g[xx][yy] != ' ')	
		if(xx>=1 && xx<=n && yy>=1 && yy<=m && g[xx][yy] != '.' && g[xx][yy] != ' '
		&& !vis[xx][yy])
			if(g[xx][yy] != p)
				st.insert(g[xx][yy]);
			else
				dfs(xx,yy);
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n >> m >> p;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			cin >> g[i][j];
		}
	}
	st.clear();
	int sx, sy;
	for(int i = 1; i <= n; ++i)  {
		for(int j = 1; j <= m; ++j) {
			if(g[i][j] == p) {
				sx = i, sy = j;
			}
		}
	}
	dfs(sx,sy);
	int res = st.size();
	cout << res;
	return 0;
} 

井字棋 - dfs(模拟)

CF754B Ilya and tic-tac-toe game - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
char g[10][10];
int vis[10][10];
int n = 4;
int flag = 0;
void dfs(int x, int y) {
	if(y <= 2 && (g[x][y+1] == 'x' && g[x][y+2] == 'x')) {
		flag = 1;
		return;
	}
	if(y >= 3 && (g[x][y-1] == 'x' && g[x][y-2] == 'x')) {
		flag = 1; 
		return;
	}
	if(x <= 2 && (g[x+1][y] == 'x' && g[x+2][y] == 'x') ) {
		flag = 1;
		return;
	}
	if(x >= 3 && (g[x-1][y] == 'x' && g[x-2][y] == 'x')) {
		flag = 1;
		return;
	}
	//对角线 
	if(x <= 2 && y <= 2 && (g[x+1][y+1] == 'x' && g[x+2][y+2] == 'x') ) {
		flag = 1; 
		return;	
	}
	if(x >= 3 && y >= 3 && (g[x-1][y-1] == 'x' && g[x-2][y-2] == 'x')) {
		flag = 1;
		return;
	}
	if(x <= 2 && y >= 3 && (g[x+1][y-1] == 'x' && g[x+2][y-2] == 'x')) {
		flag = 1;
		return;
	}
	if(x >= 2 && y <= 3 && (g[x-1][y+1] == 'x' && g[x-2][y+2] == 'x') ) {
		flag = 1;
		return;
	}
	if(x-1>=1 && x+1<=4 && (g[x-1][y] == 'x' && g[x+1][y] == 'x')) {
		flag = 1;
		return;
	}  	
	if(y+1<=4 && y-1>=1 && (g[x][y-1] == 'x' && g[x][y+1] == 'x')) {
		flag = 1;
		return;
	}
	if(x-1>=1 && y-1>=1 && x+1<=4 && y+1<=4 && (g[x-1][y-1] == 'x' && g[x+1][y+1] == 'x')) {
		flag = 1;
		return;
	}
	if(x+1<=4 && x-1>=1 && y+1<=4 && y-1>=1 && (g[x+1][y-1] == 'x' && g[x-1][y+1] == 'x')) {
		flag = 1;
		return;
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			cin >> g[i][j];
		}
	} 
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			if(g[i][j] == '.')
				dfs(i,j);
			if(flag) {
				cout << "YES";
				return 0;
			}
		}
	}
	cout << "NO"; 
	return 0;
} 

Water The Garden (BFS)

www.luogu.com.cn/problem/CF9…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int pos[210]; //位置 
int t[210]; //时间 
int vis[210];
int res, n, k;
queue<int> q;
void bfs(int x) {
	if(q.empty())
		return;
	int tmp = q.front(); q.pop();
	res = max(res, t[tmp]);
	if(tmp-1 >= 1 && !vis[tmp-1]) {
		vis[tmp-1] = 1;
		t[tmp-1] = t[tmp] + 1;
		q.push(tmp-1);
	}
	if(tmp+1 <= n && !vis[tmp+1]) {
		vis[tmp+1] = 1;
		t[tmp+1] = t[tmp] + 1;
		q.push(tmp+1);
	}
	bfs(q.front());
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int p;
	cin >> p;
	while(p--) {
		cin >> n >> k;
		//初始化 
		memset(pos,0,sizeof(pos));
		memset(t,0,sizeof(t));
		memset(vis,0,sizeof(vis));
		while(!q.empty()) {
			q.pop();
		}
		res = -1;
		for(int i = 1; i <= k; ++i) {
			cin >> pos[i];
			t[pos[i]] = 1;
			vis[pos[i]] = 1;
			q.push(pos[i]); 
		}
		bfs(q.front());
		cout << res  << endl;
	}
	return 0;
} 

6月12日

Beat The Odds

www.luogu.com.cn/problem/CF1…

思路:反证法

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+10;
/*
	 
	  
*/
int a[N];
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		int n, res = 0, oddNum = 0, evenNum = 0;
		cin >> n;
		memset(a,0,sizeof(a));
		for(int i = 1; i <= n; ++i) {
			cin >> a[i];
		}
		for(int i = 1; i <= n; ++i) {
			if(a[i] % 2 == 0)
				evenNum++;
			else 
				oddNum++;
		}
		res = min(oddNum, evenNum);
		cout << res << endl;
	} 
	return 0;
} 

6月13日

Digits in Multiplication

www.luogu.com.cn/problem/AT2…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+10;
/*
	  
*/
ll n;
int getW(ll n) {
	int res = 0;
	while(n) {
		res++;
		n /= 10;
	}
	return res;
}
void solve() {
	int res;
	//从sqrt(n)开始找 
	for(int i = sqrt(n); i >= 1; --i) {
		if(n % i == 0) {
			res = n / i;
			break;
		}
	}
	cout << getW(res);
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n;
	solve();
	return 0;
} 

数学题

www.luogu.com.cn/problem/AT2…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
	  
*/
int n, a, b;
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n >> a >> b;
	int sum =  a + b;
	if(sum > n)
		cout << (sum - n);
	else
		cout << 0;
	return 0;
} 

深搜 dfs

www.luogu.com.cn/problem/AT1…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
	  
*/
int n = 3;
int m;
char s[10];
char a[10] = {'a','b','c'};
int vis[10];
void dfs(int k) {
	if(k == m) {
		for(int i = 0; i < k; ++i) {
			cout << s[i];
		}
		cout << endl;
		return;
	}
	for(int i = 0; i < n; ++i) {
//		if(!vis[i]) {
			s[k] = a[i];
//			vis[i] = 1;
			dfs(k+1);
//			vis[i] = 0;
//		}
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> m;
	dfs(0);
	return 0;
} 

起点和终点随机的迷宫bfs

www.luogu.com.cn/problem/AT5…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
	  
*/
int n, m, sx, sy, ex, ey;
char g[25][25];
int res = 0;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int vis[25][25];
struct node {
	int a, b, step;
};
bool inMap(int x, int y) {
	return (x >= 1 && y >= 1 && x <= n && y <= m);
}
queue<node> q;
void bfs(int sx, int sy, int ex, int ey) {
	q.push({sx,sy,0});
	vis[sx][sy] = 1;
	while(!q.empty()) {
		node tmp = q.front(); q.pop();
		if(tmp.a == ex && tmp.b == ey) {
			int temp = tmp.step;
			res = max(res, temp);
			return; 
		}
		for(int i = 0; i < 4; ++i) {
			int xx = dx[i] + tmp.a;
			int yy = dy[i] + tmp.b;
			if(inMap(xx,yy) && !vis[xx][yy] && g[xx][yy] != '#') {
				q.push({xx,yy,tmp.step+1});
				vis[xx][yy] = 1;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			cin >> g[i][j];
		}
	}
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			if(g[i][j] == '.') {
				sx = i, sy = j;
				for(int k = n; k >= 1; --k) {
					for(int p = m; p >= 1; --p) {
						if(g[k][p] == '.') {
							ex = k, ey = p;
							bfs(sx, sy, ex, ey);
							memset(vis,0,sizeof(vis)); //注意初始化 
							while(!q.empty()) {
								q.pop();
							}
//							printf("(%d,%d) (%d,%d)\n",sx,sy,ex,ey);
						}
					}
				}
				
			}
		}
	}
	cout << res;
	return 0;
} 

深搜 dfs *

www.luogu.com.cn/problem/AT4…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*

*/
int res = 100, a, b;
int vis[100];
void dfs(int ori, int step) {
	//找边界
	if(step > res)  //当前次数已经大于res了,去掉 
		return;
	if(ori == b) { //如果a调到b了 
		res = min(res, step);
		return;
	}
	if(ori <= b) {
		dfs(ori+1, step+1);
		dfs(ori+5, step+1);
		dfs(ori+10,step+1);
	} else {
		dfs(ori-1, step+1);
		dfs(ori-5, step+1);
		dfs(ori-10,step+1);
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> a >> b;
	dfs(a, 0);
	cout << res << endl;
	return 0;
} 

思路:bfs (起始状态->目标状态->求最短)

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
	
*/
int ori, tar;
int dx[] = {1,-1,5,-5,10,-10};
struct node {
	int a, step;
};
queue<node> q;
int vis[100];
int res = 10086;
void bfs() {
	q.push({ori,0});
	vis[ori] = 1;
	while(!q.empty()) {
		node tmp = q.front(); q.pop();
		if(tmp.a == tar) {
			res = min(res, tmp.step);
			return;
		}
		if(tmp.step > res) return;
		for(int i = 0; i < 6; ++i) {
			int x = tmp.a + dx[i];
			if(!vis[x] && x >= 0 && x <= 40) {
				q.push({x,tmp.step+1});
				vis[x] = 1;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> ori >> tar;
	bfs();
	cout << res << endl;
	return 0;
}  

广搜 (起始状态->终点状态, 求最短路径) *

www.luogu.com.cn/problem/sol…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*

*/
int m, n; //变化规则的数量,n要生成1的数量 
int a[90010], b[90010], vis[20000]; 
struct node {
	int n, x; //n表示1增加的数量,x表示总个数 
}fk; 
queue<node> q;
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> m >> n;
	for(int i = 1; i <= m; ++i)
		cin >> a[i] >> b[i];
	if(n == 1) {
		cout << 1 << endl;
		return 0;
	}
	q.push({1,1});
	vis[1] = 1;
	while(!q.empty()) {
		node tmp = q.front(); q.pop();
		//遍历每一个变化规则 
		for(int i = 1; i <= m; ++i) {
			if(tmp.x >= a[i]) {
				node p;
				p.n = tmp.n + 1;
				p.x = tmp.x - a[i] + b[i];
				if(!vis[p.x] && p.x-300 <= n) {
					if(p.x == n) {
						cout << p.n << endl;
						return 0;
					}
					vis[p.x] = 1;
					q.push(p);
				}
			} 
		}
	}
	cout << (-1) << endl;
	return 0;
} 

6月14日

搜索 (连通块+面积) *

us.hydro.ac/d/spoj/p/UC…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
	
*/
int n, m;
char g[300][300], g2[300][300];
bool vis[300][300];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
struct node {
	int a, b, step;
};
bool inMap(int x, int y) {
	return (x >= 1 && x <= n && y >= 1 && y <= m);
}
int rec[10000000];
int cnt = 0;
void dfs(int x, int y) {
	g[x][y] = '0';
	cnt++;
	for(int i = 0; i < 4; ++i) {
		int xx = dx[i] + x;
		int yy = dy[i] + y;
		if(inMap(xx,yy) && g[xx][yy] == '1') {
			dfs(xx,yy);
		} 
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	while(cin >> n >> m) {
		if(n == 0 && m == 0) break;
		int res = 0;
		for(int i = 1; i <= n; ++i) {
			for(int j = 1; j <= m; ++j) {
				cin >> g[i][j];
//				g2[i][j] = g[i][j];
			}
		}
		
		for(int i = 1; i <= n; ++i) {
			for(int j = 1; j <= m; ++j) {
				if(g[i][j] == '1') {
					cnt=0;
					res++;
					dfs(i,j);
					rec[cnt]++;
				}
			}
		}
		cout << res << endl;
		for(int i = 1; i <= 1000000; ++i) {
			if(rec[i]) {
				cout << i << " " << rec[i] << endl;
			}
		}
		memset(rec,0,sizeof(rec));
	}
	
	return 0;
}  

模拟题

www.luogu.com.cn/problem/UVA… vjudge.net/problem/UVA…

未ac

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
*/
bool check(string s, char x) {
	int l = 0, r = s.size() - 1;
	while(l <= r) {
		int mid = (l + r) >> 1;
		if(x == s[mid])
			return true;
		else if(x < s[mid])
			r = mid-1;
		else 
			l = mid+1;
	}
	return false;
}
bool check2(string s, char x) {
	for(int i = 0; i < s.size(); ++i) {
		if(x == s[i])
			return true;
	}
	return false;
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);

	int round = 1; 
	while(1) {
		int id;
		string cpt, guess;
		cin >> id;
		if(id == -1)
			break;
		cin >> cpt >> guess;
		cout << "Round " << round << endl;
		set<char> st;
		for(int i = 0; i < cpt.size(); ++i) {
			st.insert(cpt[i]); //cehs
		}
		string tmp;
		int len = 0, chance = 7;
		set<char>::iterator it = st.begin();
		for(; it != st.end(); ++it) {
			tmp += (*it);
		}
//		cout << tmp << endl;
//		sort(guess.begin(), guess.end());
		int rec[30] = {0}, wrongNum = 0;
		set<char> st2;
		for(int i = 0; i < guess.size(); ++i) {
			if(check(tmp, guess[i])) {
//				cout << rec[guess[i] - 'a'] << endl;
				if(rec[guess[i] - 'a'] == 0) {
					rec[guess[i] - 'a']++;
					st2.insert(guess[i]);
				} else if(rec[guess[i] - 'a'] >= 1) {
					wrongNum++;
				}
			} else {
				if(rec[guess[i] - 'a'] == 0) {
					rec[guess[i] - 'a']++;
					wrongNum++;
					chance--;
				} 
			}
		}
//		cout << wrongNum << endl;
		if(st2.size() == st.size() && st2 == st && chance) {
			cout << "You win." << endl;
		} else if(chance && wrongNum < 7 && st2 != st) {
			cout << "You chickened out." << endl;
		} else if(chance == 0){
			cout << "You lose." << endl;
		}

		round++;
	}
	return 0;
}  

6月15日

最大乘积 *

www.luogu.com.cn/problem/UVA… vjudge.net/problem/UVA…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
/*
*/

int a[100];
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int n;
	int cnt = 0;
	while(cin >> n) {
		ll res = 0;
		for(int i = 0; i < n; ++i) {
			cin >> a[i];
		}
		for(int i = 1; i <= n; ++i) {
			for(int j = 0; j + i <= n; ++j) {
				//j是子序列的起点,从0开始找
				ll cal = 1; //这个计数器是用来比较连续子序列乘积与ans原数的大小  
				for(int k = j; k < j + i; ++k) {
					//确定不同的终点
					cal *= a[k];
//					cout << cal << endl; 
				}
				res = max(res, cal);
			}
		}
		cout << "Case #" << (++cnt) << ": The maximum product is " << res << "." << endl << endl;
	}
	return 0;
}    

二分递归 *

www.luogu.com.cn/problem/UVA…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+100;
/*
*/
int a[10010], q[10010];
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	for(int id=1; ; id++) {
		int n, m;
		cin >> n >> m;
		memset(a,0,sizeof(a));
		memset(q,0,sizeof(q));
		if(n == 0 && m == 0) break;
		for(int i = 1; i <= n; ++i) 
			cin >> a[i];
		sort(a+1, a+1+n);
		for(int i = 1; i <= m; ++i) {
			cin >> q[i];
		}
		cout << "CASE# "  << id << ":" << endl;
		for(int i = 1; i <= m; ++i) {
			int pos;
			pos = lower_bound(a+1,a+1+n,q[i])-a;
			if(a[pos] == q[i])
				cout << q[i] << " found at " << pos << endl;
			else
				cout << q[i] << " not found" << endl;
		}
	}
		
	return 0;
}     

Divisibility by Eight (dp)

未AC

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
*/
bool contain0(string s) {
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] == '0')
			return true;
	}
	return false;
}
char rec[110];
int vis[110];
void dfs(string s, int k, int m, int idx) {
	if(k > m) return;
	if(k == m) {
		if(stol(rec) % 8 == 0) {
			cout << "YES" << endl;
			cout << rec;
			exit(0);
		}
//		cout << rec << endl;
		return;
	}
	for(int i = idx; i < s.size(); ++i) {
		if(!vis[i]) {
			rec[k] = s[i];
			vis[i] = 1;
			dfs(s, k+1, m, i+1);
			vis[i] = 0;
		}
	}
} 
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	string s;
	cin >> s;
//	dfs(s,0,3,0);
	if(contain0(s)) {
		cout << "YES" << endl;
		cout << 0;
	} else {
		int flag = 0;
		for(int i = 0; i < s.size(); ++i) {
			if((s[i] - '0') % 8 == 0) {
				flag = 1; 
				cout << "YES" << endl;
				cout << s[i] << endl;
				return 0;
			}
			if(i > 1)
				dfs(s,0,i,0);
		}
		if(!flag) {
			cout << "NO" << endl;
		}
	}
	return 0;
}  

AC

思路:末尾三位是8的倍数,三重循环

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
/*
*/
bool contain0(string s) {
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] == '0')
			return true;
	}
	return false;
}
char rec[110];
int vis[110];
/*
	当一个数想被8整除,末尾三个数一定是8的位数 
*/
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	string s;
	cin >> s;
	int flag = s.find('0');
	if(flag != -1) {
		cout << "YES" << endl;
		cout << 0;
		return 0;
	}
	for(int i = 0; i < s.size(); ++i) {
		if((s[i] - '0') % 8 == 0) {
			cout << "YES" << endl; 
			cout << s[i];
			return 0;
		} 
		for(int j = i+1; j < s.size(); ++j) {
			if(((s[i] - '0')*10 + (s[j] - '0')) % 8 == 0) {
				cout << "YES" << endl;
				cout << ((s[i] - '0')*10 + (s[j] - '0'));
				return 0;
			}
			for(int k = j+1; k < s.size(); ++k) {
				if(((s[i] - '0')*100 + (s[j] - '0')*10 + (s[k] - '0'))  % 8 == 0) {
					cout << "YES" << endl; 
					cout << ((s[i] - '0')*100 + (s[j] - '0')*10 + (s[k] - '0'));
					return 0;
				}
			}
		}
	}
	puts("NO");
	return 0;
}  

Stone Age Problem (线段树)

www.luogu.com.cn/problem/CF1…

超时

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
/*
  n个元素,进行q次操作
  1: 单点修改
  2: 所有元素改为x 
*/
struct tree {
	int l, r;
	ll sum;
}t[N<<2]; 
int lc(int k) {
	return (k << 1);
}
int rc(int k) {
	return (k << 1 | 1);
}
inline void push_up(int k) {
	t[k].sum = t[lc(k)].sum + t[rc(k)].sum;
}
int a[N];
inline void buildTree(int k, int l, int r) {
	t[k].l = l; t[k].r = r; t[k].sum = 0;
	if(l == r) {
		t[k].sum = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	buildTree(lc(k), l, mid);
	buildTree(rc(k), mid+1, r);
	push_up(k);
}
ll query(int k, int l, int r) {
	if(t[k].l >= l && t[k].r <= r) {
		return t[k].sum;
	}
	int mid = (t[k].l + t[k].r) >> 1;
	ll res = 0;
	if(l <= mid)
		res += query(lc(k), l, r);
	if(r > mid)
		res += query(rc(k), l, r);
	return res;
}
int n, q;
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n >> q;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	buildTree(1, 1, n);
	while(q--) {
		int p;
		cin >> p;
		if(p == 1) {
			int x, z;
			cin >> x >> z;
			a[x] = z;
			buildTree(1,1,n);
			//区间求和 - 线段树 
			cout << query(1, 1, n) << endl;
		} else {
			int x;
			cin >> x;
			fill(a+1, a+1+n, x);
			buildTree(1,1,n);
			//区间求和 
			cout << query(1,1,n) << endl;
		}
	}
	return 0;
}  

AC

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
/*
  n个元素,进行q次操作
  1: 单点修改
  2: 所有元素改为x 
*/
int a[N];
int n, q;
struct tree {
	int l, r;
	ll tag, sum;
}t[N<<2]; 
int lc(int k) {
	return (k << 1);
}
int rc(int k) {
	return (k << 1 | 1);
}
inline void push_up(int k) {
	t[k].sum = t[lc(k)].sum + t[rc(k)].sum;
}
inline void push_down(int k, int len) {
	if(t[k].tag != LLONG_MIN) {
		t[lc(k)].tag = t[k].tag;
		t[rc(k)].tag = t[k].tag;
		t[lc(k)].sum = (len - len / 2) * t[k].tag;
		t[rc(k)].sum = (len / 2) * t[k].tag;
		
		t[k].tag = LLONG_MIN; //恢复 
	}
}
inline void buildTree(int k, int l, int r) {
	t[k].l = l; t[k].r = r; t[k].sum = 0;
	t[k].tag = LLONG_MIN;
	if(l == r) {
		t[k].sum = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	buildTree(lc(k), l, mid);
	buildTree(rc(k), mid+1, r);
	push_up(k);
}
ll query(int k, int l, int r) {
	if(t[k].l >= l && t[k].r <= r) {
		return t[k].sum;
	}
	int mid = (t[k].l + t[k].r) >> 1;
	ll res = 0;
	push_down(k, (t[k].r - t[k].l + 1));
	if(l <= mid)
		res += query(lc(k), l, r);
	if(r > mid)
		res += query(rc(k), l, r);
	push_up(k);
	return res;
}
inline void update1(int k, int x, int z) {
	t[k].sum = t[k].sum - a[x] + z;
	if(t[k].l >= x && t[k].r <= x) {
		a[x] = z;
		return;
	}
	int mid = (t[k].l + t[k].r) >> 1;
	if(x <= mid)
		update1(lc(k), x, z);
	if(x > mid)
		update1(rc(k), x, z);
}
inline void update2(int k, int l, int r, int z) {
	if(t[k].l >= l && t[k].r <= r) {
		t[k].tag = z;
		t[k].sum = (t[k].r - t[k].l + 1) * z; 
		return;
	}
	push_down(k, (t[k].r - t[k].l + 1));
	int mid = (t[k].l + t[k].r) >> 1;
	if(l <= mid)
		update2(lc(k), l, r, z);
	if(r > mid)
		update2(rc(k), l, r, z);
	push_up(k);
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n >> q;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	buildTree(1, 1, n);
	while(q--) {
		int p;
		cin >> p;
		if(p == 1) {
			int x, z;
			cin >> x >> z;
//			a[x] = z;
//			buildTree(1,1,n);
			//区间求和 - 线段树 
			update2(1,x,x,z);
			cout << query(1, 1, n) << endl;
		} else {
			int x;
			cin >> x;
//			fill(a+1, a+1+n, x);
//			buildTree(1,1,n);
			//区间求和 
			update2(1,1,n,x);
			cout << query(1,1,n) << endl;
		}
	}
	return 0;
}  

Circular RMQ (线段树)

www.luogu.com.cn/problem/CF5…

未AC

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
/*
 	inc(lf,rg,v):区间修改
	rmq(lf,rg) 求出区间[lf,rg]中的最小值 
*/
const ll MAX = 9223372036854775807;
ll a[N];
int n;
struct tree {
	int l, r;
	ll data, tag;
}t[N<<2];
int lc(int k) {
	return (k << 1);
}
int rc(int k) {
	return (k << 1 | 1);
}
inline void push_up(int k) {
	t[k].data = min(t[lc(k)].data, t[rc(k)].data);
}
inline void push_down(int k) {
	if(t[k].tag) {
		t[lc(k)].tag += t[k].tag;
		t[rc(k)].tag += t[k].tag;
		
		t[lc(k)].data += t[k].tag;
		t[rc(k)].data += t[k].tag;
		t[k].tag = 0;
	}
}
inline void buildTree(int k, int l, int r) {
	t[k].l = l; t[k].r = r, t[k].tag = 0;
	if(l == r) {
		t[k].l = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	buildTree(lc(k), l, mid);
	buildTree(rc(k), mid+1, r);
	push_up(k);
}
inline void update(int k, int l, int r, int z) {
	if(t[k].l >= l && t[k].r <= r) {
		t[k].tag += z;
		t[k].data += z;
		return;
	}
	push_down(k);
	int mid = (t[k].l + t[k].r) >> 1;
	if(l <= mid)
		update(lc(k), l, r, z);
	if(r > mid)
		update(rc(k), l, r, z);
	push_up(k);
}
ll query(int k, int l, int r) {
	if(t[k].l >= l && t[k].r <= r) {
		return t[k].data;
	}
	ll res = MAX;
	int mid = (t[k].l + t[k].r) >> 1;
	push_down(k);
	if(l <= mid)
		res = min(res, query(lc(k), l, r));
	if(r > mid)
		res = min(res, query(rc(k), l, r));
	return res; 
}
int main() {
//	ios::sync_with_stdio(false);  cin.tie(0);
//	cin >> n;
	scanf("%d",&n);
	for(int i = 1; i <= n; ++i)
//		cin >> a[i];
		scanf("%lld",&a[i]);
	buildTree(1,1,n);
	int m;
//	cin >> m;
	scanf("%d",&m);
	while(m--) {
		int lf, rg, v;
//		cin >> lf >> rg;
		scanf("%d%d", &lf,&rg);
		++lf,++rg;
		if(getchar() == ' ') {
//			cin >> v;
			scanf("%d", &v);
			if(lf <= rg) {
				update(1, lf, rg, v);
			} else {
				update(1, 1, rg, v);
				update(1, lf, n, v);
			}
		} else {
			if(lf <= rg)	
//				cout << query(1, lf, rg) << endl;
				printf("%lld\n", query(1,lf,rg) + 1);
			else {
//				cout << min(query(1,1,rg), query(1,lf,n)) << endl;
				printf("%lld\n", min(query(1,1,rg), query(1,lf,n)) + 1);
			}
		}
	}
	return 0;
}   

Longest Regular Bracket Sequence

www.luogu.com.cn/problem/CF5…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
/*
*/

stack<int> stk;
bool vis[1001010];
int  top = 0;
int cnt = 0, res = 0, sum = 0; //cnt用来记录括号数量,res最长合法子串,sum表示最长合法子串的数量 
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	string s;
	cin >> s;
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] == '(') {
			stk.push(i); //8 
		} else {
			if(stk.size()) { //
				vis[stk.top()] = true; //vis[3] = 1, vis[2] = 1, vis[1] = 1, vis[9] = 1, vis[11] = 1, vis[8]=1
				vis[i] = true;    //vis[4] = 1, vis[5] = 1, vis[6] = 1, vis[10] = 1, vis[12] = 1, vis[13] = 1
				stk.pop();       //出栈 3 2 1 9 11 8
			}
		}
	}
	//遍历括号串 
	for(int i = 0; i <= s.size(); ++i) {
		if(vis[i]) //cnt记录括号对的个数 
			cnt++;
		else  {
			res = max(cnt, res);
			cnt = 0;
		}
	}
	if(res == 0) {
		cout << "0 1" << endl;
		return 0;
	}
	cnt = 0;
	for(int i = 0; i <= s.size(); ++i) {
		if(vis[i])
			cnt++;
		else {
			if(cnt == res) {
				sum++;
			}
			cnt = 0;
		}
	}
	cout << res << " " << sum << endl;
	return 0;
}    

Regular Bracket Sequence(括号匹配)

www.luogu.com.cn/problem/CF2…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
/*
*/
string s;
int stk[N];
bool vis[N];
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int top = 0;
	cin >> s;
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] == '(')
			stk[++top] = i;
		else {
			if(top) {
				vis[stk[top]] = vis[i] = true;
				top--;
			}
		}
	}
	int cnt = 0, res = 0;
//	for(int i = 0; i <= s.size(); ++i) {
//		if(vis[i]) {
//			cout << i << " ";
//		}
//	}
	for(int i = 0; i <= s.size(); ++i) {
		if(vis[i])
			cnt++;
		else {
			res = max(res, cnt);
		}
	}
	cout << res << endl;
	return 0;
}    

雨上 (DP)

搜索:未AC

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
/*
*/
int n;
char s[110];
int res = INF;
int vis[110];
void dfs(int x, int step) {
	if(x > n-1) return;
	if(x == n-1) {
		res = min(res, step);
		return;
	}
	for(int i = 1; i <= 3; ++i) {
		if(!vis[x+i]) {
			vis[x+i] = 1;
			if(s[x+i] == 'X')
				dfs(x+i, step+1);
			else
				dfs(x+i, step);
			vis[x+i] = 0;
		}
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n >> s;
	if(s[0] == 'X')
		dfs(0,1);
	else
		dfs(0,0);
	cout << res << endl;
	return 0;
}    

DP

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
/*
*/
int n;
char s[110];
int res = INF;
int memo[110];
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> s[i];
	for(int i = 1; i <= 3; ++i) {
		if(s[i] == '.')
			memo[i] = 0;
		else
			memo[i] = 1;
	}
	for(int i = 4; i <= n; ++i) {
		if(s[i] == '.')
			memo[i] = min(min(memo[i-1], memo[i-2]), memo[i-3]);
		if(s[i] == 'X')
			memo[i] = min(min(memo[i-1], memo[i-2]), memo[i-3])+1;
	}
	cout << memo[n] << endl;
	return 0;
}    

Alice and Bob

www.luogu.com.cn/problem/CF6…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+100;
/*
*/
int candy[N];
int n; 
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> candy[i];
	int a = 1, b = n;
	int sum1 = 0, sum2 = 0;
	while(a <= b) {
		if(sum1 <= sum2) {
			sum1 += candy[a++];
		} else {
			sum2 += candy[b--];
		}
	}
	cout << a-1 << " " << n - b;
	return 0;
}    

6月16日

www.luogu.com.cn/problem/CF9…

等差数列 (搜索)

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+100;
/*
*/
int n;
ll a[N], b[N];
ll res[10][10];
ll ans = INF;
int d;
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n;	
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	for(int p = -1; p <= 1; ++p) {
		for(int p2 = -1; p2 <= 1; ++p2) {
			for(int i = 1; i <= n; ++i) {
				b[i] = a[i];
			}
			res[p+2][p2+2] = abs(p) + abs(p2);
			b[1] += p;
			b[2] += p2;
			d = b[2] - b[1];
			for(int i = 1; i < n; ++i) {
				if(b[i+1]-1 == d+b[i]) {
					b[i+1]--;
					res[p+2][p2+2]++;
				} else if(b[i+1]+1 == d+b[i]) {
					b[i+1]++;
					res[p+2][p2+2]++;
				} else if(b[i+1] == d+b[i]) {
					
				} else {
					res[p+2][p2+2] = INF;
				}
			}
		}
	}
	for(int i = 1; i <= 3; ++i) {
		for(int j = 1; j <= 3; ++j) {
			ans = min(ans, res[i][j]);
		}
	}
	if(ans >= INF) {
		cout << -1;
	} else {
		cout << ans;
	}
	return 0;
}    

Vasya and Golden Ticket (字符串dfs)

www.luogu.com.cn/problem/CF1…

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+100;
/*
*/
//计算总和 
int getSum(string s) {
	int res = 0;
	for(int i = 0; i < s.size(); ++i)
		res += (s[i] - '0');
	return res;
}
//字符串 总和 第几段 
bool dfs(string s, int sum, int k) {
	//特判长度为1的情况 
	if(s.size() == 1 && s[0] - '0' == sum)
		return true;
	for(int i = 1; i <= s.size(); ++i) {
		int res = getSum(s.substr(0, i));
		
		if(i == s.size() && res == sum) 
			return true;
		
		if(res > sum && k)
			return false;
		
		if(res == sum || !k) {
			bool flag = dfs(s.substr(i, s.size()+1), res, k+1);
			if(flag)
				return true;
		}
	} 
	return false;
}
int n;
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	cin >> n;
	string s;
	cin >> s;
	if(dfs(s, 0, 0))
		cout << "YES";
	else
		cout << "NO";
	return 0;
}    

6月21日

字符串进制 (26进制转10,10转26进制)

www.luogu.com.cn/problem/CF1…

超时

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <map> 
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+100;
const int MAX = 1e9+10;
/*
	
*/
map<string,int> mp;
map<int,string> mp2;
void solve(string s) {
	int cnt = 0;
	for(char i = 'A'; i <= 'Z'; ++i) {
		cnt++;
		string tmp = "";
		tmp += i;
		mp[tmp] = cnt;
	}
	if(s.size() == 1) return;
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			cnt++;
			string tmp = "";
			tmp += i; tmp += j;
			mp[tmp] = cnt;
		}
	}
	if(s.size() == 2) return;
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				cnt++;
				string tmp = "";
				tmp += i; tmp += j; tmp += k;
				mp[tmp] = cnt;
			}
		}
	}
	if(s.size() == 3) return;
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				for(char p = 'A'; p <= 'Z'; ++p) {
					cnt++;
					string tmp = "";
					tmp += i; tmp += j; tmp += k; tmp += p;
					mp[tmp] = cnt;
				}
			}
		}
	}
	if(s.size() == 4) return;
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				for(char p = 'A'; p <= 'Z'; ++p) {
					for(char l = 'A'; l <= 'Z'; ++l) {
							cnt++;
							string tmp = "";
							tmp += i; tmp += j; tmp += k; tmp += p; tmp += l;
							mp[tmp] = cnt;
					}
				}
			}
		}
	}
	if(s.size() == 5) return;
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				for(char p = 'A'; p <= 'Z'; ++p) {
					for(char l = 'A'; l <= 'Z'; ++l) {
						for(char m = 'A'; m <= 'Z'; ++m) {
							cnt++;
							string tmp = "";
							tmp += i; tmp += j; tmp += k; tmp += p; tmp += l; tmp += m;
							mp[tmp] = cnt;
						}
					}
				}
			}
		}
	}
	if(s.size() == 6) return;
}
void solve2(int n) {
	int cnt = 0;
	for(char i = 'A'; i <= 'Z'; ++i) {
		cnt++;
		string tmp = "";
		tmp += i;
		if(cnt == n) {
			mp2[cnt] = tmp;
			return ;
		}
	}
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			cnt++;
			string tmp = "";
			tmp += i; tmp += j;
			if(cnt == n) {
				mp2[cnt] = tmp;
				return ;
			}
		}
	}
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				cnt++;
				string tmp = "";
				tmp += i; tmp += j; tmp += k;
				if(cnt == n) {
					mp2[cnt] = tmp;
					return ;
				}
			}
		}
	}
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				for(char p = 'A'; p <= 'Z'; ++p) {
					cnt++;
					string tmp = "";
					tmp += i; tmp += j; tmp +=k; tmp+=p;
					if(cnt == n) {
						mp2[cnt] = tmp;
						return ;
					}
				}
			}
		}
	}
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				for(char p = 'A'; p <= 'Z'; ++p) {
					for(char l = 'A'; l <= 'Z'; ++l) {
							cnt++;
							string tmp = "";
							tmp += i; tmp += j; tmp +=k; tmp+=p; tmp+=l;
							if(cnt == n) {
								mp2[cnt] = tmp;
								return ;
							}
					}
				}
			}
		}
	}
	for(char i = 'A'; i <= 'Z'; ++i) {
		for(char j = 'A'; j <= 'Z'; ++j) {
			for(char k = 'A'; k <= 'Z'; ++k) {
				for(char p = 'A'; p <= 'Z'; ++p) {
					for(char l = 'A'; l <= 'Z'; ++l) {
						for(char m = 'A'; m <= 'Z'; ++m) {
							cnt++;
							string tmp = "";
							tmp += i; tmp += j; tmp +=k; tmp+=p;tmp+=l;tmp+=m;
							if(cnt == n) {
								mp2[cnt] = tmp;
								return ;
							}
						}
					}
				}
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int n;
	cin >> n;
	while(n--) {
		string s;
		cin >> s;
		if(s[0] != 'R') {
			int st = 0, ed = 0;
			for(int i = 0; i < s.size(); ++i) {
				if(isdigit(s[i])) {
					ed = i;
					break; 
				}
			}
			string col = s.substr(st, ed);
			solve(col);
			st = ed = 0;
			for(int i = 0; i < s.size(); ++i) {
				if(isdigit(s[i])) {
					st = i;
					ed = s.size()-1;
					break;
				}
			}
			string row = s.substr(st,ed);
	//		cout << col << endl;
			cout << "R" << row << "C" << mp[col] << endl;
		} else {
			int st = 1, ed = 1;
			for(int i = 0; i < s.size(); ++i) {
				if(s[i] == 'C') {
					ed = i-1;
					break;
				}
			}
			string row = s.substr(st,ed);
			for(int i = 0; i < s.size(); ++i) {
				if(s[i] == 'C') {
					st = i+1;
					ed = s.size()-1;
					break;
				}
			}
			string col = s.substr(st,ed);
			int p = stoi(col);
			solve2(p);
	//		cout << row << " " << col;
			cout << mp2[p] << row << endl; 
		}
	}
	return 0;
}    

6月22日

字符串哈希

www.luogu.com.cn/problem/CF4…

超时

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <map> 
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
const int MAX = 1e9+10;
/*
*/
multimap<string,int> mp;
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int n;
	cin >> n;
	while(n--) {
		string s;
		cin >> s;
		if(mp.count(s) == 0) {
			cout << "OK" << endl;
			mp.insert({s, mp.count(s) + 1});
		} else {
			cout << s << mp.count(s) << endl;
			mp.insert({s, mp.count(s) + 1});
		}
	}
	return 0;
}    

Word Correction

www.luogu.com.cn/problem/CF9…

#include <iostream>
#include <cstdio> 
#include <cmath>
#include <cstring>
#include <string>
#include <map> 
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
const int MAX = 1e9+10;
/*
		weird
  i		 ^
*/
bool isVowels(char s) {
	return (s == 'a' || s == 'e' || s == 'i'
	|| s == 'o' || s == 'u' || s == 'y');
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	int n; cin >> n;
	string s; cin >> s;
	string res;
	res += s[0];
	for(int i = 1; i < s.size(); ++i) {
		if(isVowels(s[i]) && isVowels(s[i-1]))
			continue;
		else
			res += s[i];
	} 
	cout << res;
	return 0;
}    

6月23日

Romaji (字符串)

www.luogu.com.cn/problem/CF1…

#include <iostream>
#include <cstdio> 
#include <cmath>
#include <cstring>
#include <string>
#include <map> 
#include <set>
#include <stack>
#include <ctime>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6+100;
const int MAX = 1e9+10;
bool isVowel(char s) {
	return (s == 'a' || s == 'e' || s == 'i'
		  ||s == 'o' || s == 'u' || s == 'n');
}
int main() {
	ios::sync_with_stdio(false);  cin.tie(0);
	string s;
	cin >> s;
	bool flag = 1;
	if(!isVowel(s[s.size() - 1]) || s[s.size() - 1] == ' ') {
		flag = 0;
		cout << "NO";
		return 0;
	}
	for(int i = 0; i < s.size()-1; ++i) {
		if(!isVowel(s[i])) {
			if(!isVowel(s[i+1])) {
				flag = 0;
				break;
			}
			if(s[i+1] == 'n') {
				flag = 0;
				break;
			}
		} 
 	}
	if(flag) {
		cout << "YES";
	}
	else
		cout << "NO";
	return 0;
}