做法:直接循环找
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int main() {
int n = 1200000, res = 0, mid;
for(int i = 1; i <= n; ++i) {
if(n % i == 0) ++res;
}
cout << res;
return 0;
}
答案:96
做法:一个根节点,其他都是叶子节点
答案:2018
做法:贪心
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int check(int x) {
while(x) {
if(x % 10 == 9) return 1;
x /= 10;
}
return 0;
}
int main() {
int res = 0;
_rep(1, 2019, i) {
if(check(i)) ++res;
}
cout << res;
return 0;
}
答案:544
——以下代码能保证正确性,有问题请指出——
做法:贪心,从1到n每个都判断一下
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int check(int x) {
int lst = 10;
while(x) {
if(x%10 > lst) return 0;
lst = x % 10;
x /= 10;
}
return 1;
}
int main() {
int n; scanf("%d", &n);
int res = 0;
_rep(1, n, i) if(check(i)) ++res;
cout << res;
return 0;
}
做法:每个数字都判断一下前面是否有比当前这个数字小的,后面是否有比当前这个数字大的,如果有,则结果+1
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 1010;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[N], mx[N], mn[N];
int main() {
int n; scanf("%d", &n);
mn[0] = M, mx[n+1] = 0;
_rep(1, n, i) {
scanf("%d", &a[i]); mn[i] = min(mn[i-1], a[i]);
}
for(int i = n; i >= 1; --i) mx[i] = max(mx[i+1], a[i]);
int res = 0;
_rep(1, n, i) if(mn[i] < a[i] && mx[i] > a[i]) ++res;
cout << res;
return 0;
}
题意:
做法:这题表面上很简单,但是细节居多,下面列出我踩过的坑:
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 1010;
const LL Mod = 1e9+7;
const int M = 1e6+10;
string s;
int isy(char c) {
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return 1;
return 0;
}
int a, b, c, d;
int main() {
cin >> s;
int len = s.size(), p = 0;
while(p < len && !isy(s[p])) ++p, a = 1;
while(p < len && isy(s[p])) ++p, b = 1;
while(p < len && !isy(s[p])) ++p, c = 1;
while(p < len && isy(s[p])) ++p, d = 1;
if(a && b && c && d && p >= len) puts("yes");
else puts("no");
return 0;
}
做法:用bfs扩展,每个草扩展4个方向,对4个方向的草上标记好扩展到这个点时过了几个月,重点是vis数组,这个数组能让代码的内存节约很多。
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 1010;
const LL Mod = 1e9+7;
const int M = 1e6+10;
struct xx {
int x, y, mon;
} u, v;
int n, m, k;
char tu[N][N];
int vis[N][N];
queue<xx> q;
void bfs() {
int x, y;
while(!q.empty()) {
u = q.front(); q.pop();
if(u.mon >= k) return;
vis[u.x][u.y] = 1;
_for(0, 4, i) {
x = u.x + ne[i][0]; y = u.y + ne[i][1];
if(x < 1 || y < 1 || x > n || y > m || vis[x][y]) continue;
tu[x][y] = 'g', v.x = x, v.y = y, v.mon = u.mon + 1;
q.push(v);
}
}
return;
}
int main() {
scanf("%d%d", &n, &m);
_rep(1, n, i) {
scanf("%s", tu[i]+1);
_rep (1, m, j) {
if(tu[i][j] == 'g') {
u.x = i, u.y = j, u.mon = 0;
q.push(u);
}
}
}
scanf("%d", &k);
bfs();
_rep(1, n, i) printf("%s\n", tu[i]+1);
return 0;
}
题意:
做法:记忆化搜索,最容易想到的dp[i][j]是代表前一项为i,后一项为j的方案总数,这样的话需要在dfs中嵌套循环,复杂度很高。dp[i][j]代表前一项为i,当前项为1到j的方案总数和(前缀和优化),考虑转移方程: d p [ i ] [ j ] = d p [ i ] [ j − 1 ] + 1 + d p [ j ] [ a b s ( i − j ) − 1 ] dp[i][j] = dp[i][j-1]+1+dp[j][abs(i-j)-1] dp[i][j]=dp[i][j−1]+1+dp[j][abs(i−j)−1]dp[i][j],上一项为i,当前项为1到j的方案总和,通过dp[i][j-1],上一项为i,当前项为1到j-1的方案总和,加上本身的1个答案,以及dp[j][abs(i-j)-1],上一项为j的所有合法,这个大部分是已经求出来了的,大大降低了复杂度。这篇题解很nice。
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 1010;
const int Mod = 10000;
const int M = 1e6+10;
int n, dp[N][N];
int dfs(int i, int j) {
if(j <= 0) return 0;
if(dp[i][j] != -1) return dp[i][j];
return dp[i][j] = (dfs(i, j-1) + dfs(j, abs(i-j)-1) + 1) % Mod;
}
int main() {
scanf("%d", &n);
_rep(1, n, i) _rep(1, n, j) dp[i][j] = -1;
printf("%d\n", dfs(n, n));
return 0;
}
做法:每次尽可能选大的,用st表维护区间最大值,r控制它一定有足够的数留给m。
代码:
#include
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
int f = 1; res = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0'; c = getchar(); }
res *= f;
}
const int ne[8][2] = {
1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int Mod = 10000;
const int M = 1e6+10;
int n, m;
int a[N], st[N][30];
int Max(int x, int y) {
return a[x] >= a[y] ? x : y;
}
void init() {
_for(0, n, i) st[i][0] = i;
for(int j = 1; (1 << j) <= n; ++j) {
for(int i = 0; i+(1<<j)-1 < n; ++i)
st[i][j] = Max(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
int search(int l, int r) {
int k = (int)(log((double)(r - l + 1)) / log(2.0));
return Max(st[l][k],st[r - (1 << k) + 1][k]);
}
int main() {
scanf("%d%d", &n, &m);
_for(0, n, i) scanf("%d", &a[i]);
init();
int l = 0, r = n-m, p;
while(m--) {
p = search(l, r);
printf("%d ", a[p]);
l = p+1, ++r;
}
return 0;
}