思维解题: 这题数据出问题了,本来这种解法不应该过的。
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INT_MAX 0x3f3f3f3f
#define INT_MIN 0xc0c0c0c0
using namespace std;
const int N = 200000;
int arr[N];
signed main()
{
int n, m;
scanf("%lld%lld", &n, &m);
for(int i = 0; i < n; i++) scanf("%lld", &arr[i]);
for(int i = 0; i < m; i++) {
char ch;
int a, b;
getchar();
scanf("%c%lld%lld", &ch, &a, &b);
if (ch == 'Q') {
int maxx = 0;
for (int j = a - 1; j < b; j++) {
if(arr[j] > maxx) maxx = arr[j];
}
printf("%lld\n", maxx);
} else arr[a - 1] = b;
}
return 0;
}
正宗的解法在这里:线段树解法
线段树相关内容 Click here~~
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
//#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
const int N = 200010;
int m, p;//m操作次数,p取模的值是多少
struct Node{
int l, r;//左端点,右端点
int val;//区间[l, r]的最大值
}tr[N << 2];
int num[N];
//由子节点的信息来计算父节点的信息
void pushup(int cur){
tr[cur].val = max(tr[cur << 1].val, tr[cur << 1 | 1].val);
}
//cur代表当前节点,
void build(int cur, int l, int r){
//当前结点的左右儿子分别是tr[cur].l tr[cur].r
tr[cur] = {
l, r};
//如果已经是叶结点return
if(l == r) {
tr[cur].val = num[r];
return;
}
//否则求一下当前区间的中点
int mid = l + r >> 1;
//递归建立左边区间
build(cur << 1, l, mid);
//递归建立右边区间
build(cur << 1 | 1, mid + 1, r);
pushup(cur);
}
//[l, r]查询区间 cur代表当前线段树里面的端点。
int query(int cur, int l, int r) {
//①情况[TL,TR] ⊂ [L,R]
//树中节点,已经被完全包含在[l, r]中了。
if (tr[cur].l >= l && tr[cur].r <= r) {
return tr[cur].val;
}
int mid = tr[cur].l + tr[cur].r >> 1;
int val = 0;
//判断与左边有没有交集
if (l <= mid) {
val = query(cur << 1, l, r);
}
//这里为什么是 r > mid,因为划分的区间是[l, mid][mid + 1, r],所以要用>而不能=
//判断与右边有没有交集
if (r > mid) {
//为什么要取max?
//因为上面先比较左值,所以左值可能有一个最大值,要跟再右边区间查询的值进行比较,取最大的。
val = max(val, query(cur << 1 | 1, l, r));
}
//返回结果
return val;
}
//cur代表当前线段树里面的端点。tar代表要修改的位置
void modify(int cur, int tar, int val) {
//如果当前节点就是叶节点,那么直接修改就可以了
if (tr[cur].l == tar && tr[cur].r == tar) {
tr[cur].val = val;
return;
}
int mid = tr[cur].l + tr[cur].r >> 1;
if (tar <= mid) {
modify (cur << 1, tar, val);
} else {
modify (cur << 1 | 1, tar, val);
}
//递归完之后,要更新到父节点。
//pushup就是更新父节点的信息
pushup(cur);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(num, 0, sizeof num);
memset(tr, 0, sizeof tr);
for (int i = 1;i <= n; ++i) {
scanf("%d", &num[i]);
}
build(1, 1, n);
for (int i = 0; i < m; ++i) {
char ch;
int a, b;
getchar();//每一次都要getchar();放在循环里面的第一行。
scanf("%c%d%d",&ch,&a,&b);
if (ch == 'Q') {
printf("%d\n", query(1, a, b));
}
else{
modify(1, a, b);
}
}
}
return 0;
}
字符串b:
a b b a c c
b[1] b[2] b[3] b[4] b[5] b[6]
字符串a:
a b a c
a[1] a[2] a[3] a[4]
已知字符串b即已知字符串b的长度
通过观察得出规律:
两字符串之间的长度规律:b.length() = 2 × (a.length() - 1)
两字符串之间的数值规律:a[i] = b[2 × i - 1]
#include
#include
#include
using namespace std;
int main()
{
int t;
scanf("%d", &t);
getchar();
while (t--) {
char b[105];
scanf("%s", b);
for (int i = 0; i < strlen(b) - 1; i += 2) {
printf("%c", b[i]);
}
printf("%c\n", b[strlen(b) - 1]);
}
return 0;
}
这题是博弈论
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INT_MAX 0x3f3f3f3f
#define INT_MIN 0xc0c0c0c0
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
signed main()
{
int t = read();
while (t--) {
int n = read();
int m = read();
if (n % (m + 1)) printf("first\n");
else printf("second\n");
}
return 0;
}
暴力解法:
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
signed main()
{
int t = read();
while (t--) {
int n = read();
int cnt = 0;
while (n > 1) {
if (n % 2 == 0) {
n /= 2;
cnt += n;
} else {
n /= 2;
cnt += n;
n++;
}
}
printf("%lld\n", cnt);
}
return 0;
}
数学解法:输出n-1就可以了
原理解析:
n支队伍参加淘汰赛决出获胜队伍需要场次是:n - 1
那么有n支队伍,要决出获胜队伍,那么意思就是要淘汰 n - 1支队伍,一场比赛淘汰一支队伍,则需要n - 1场比赛。
#include
#include
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
printf("%d\n", n - 1);
}
return 0;
}
区间DP的原理及例题 Click here ~
#include
#include
#include
#include
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 410;//因为要开双链
const int inf = 0x3f3f3f3f;
int sum[N], val[N];//sum[N]表示合并付出的代价总和,val[N]表示每个点的值。
int dpmin[N][N], dpmax[N][N];//得分总和最小,得分总和最大
int main()
{
int n = read();
for (int i = 1; i <= n; ++i) {
val[i] = read();
val[i + n] = val[i];//化环形为链形
}
for (int i = 1; i <= n + n; ++i) {
sum[i] = sum[i - 1] + val[i];//前缀和 以便最后求出——最后一步所需要付出的代价
}
//初始化
memset(dpmin, 0x3f, sizeof dpmin);
memset(dpmax, -0x3f, sizeof dpmax);
//枚举链的长度
for (int len = 1; len <= n; ++len) {
//枚举区间左端点
for (int l = 1; l + len - 1 <= n + n; ++l) {
int r = l + len - 1;//区间右端点
//如果区间长度为1,则无需付出任何代价。
if (len == 1) dpmin[l][r] = dpmax[l][r] = 0;
else {
//枚举分界线
for (int k = l; k < r; ++k) {
dpmin[l][r] = min(dpmin[l][r], dpmin[l][k] + dpmin[k + 1][r] + sum[r] - sum[l - 1]);
dpmax[l][r] = max(dpmax[l][r], dpmax[l][k] + dpmax[k + 1][r] + sum[r] - sum[l - 1]);
}
}
}
}
//初始化最大值最小值。
int maxv = -inf, minv = inf;
for (int l = 1; l <= n; ++l) {
maxv = max(maxv, dpmax[l][l + n - 1]);
minv = min(minv, dpmin[l][l + n - 1]);
}
//得出结果
printf("%d\n", minv);
printf("%d\n", maxv);
return 0;
}
对数字运算进行模拟:
① 如果遇到 ’ + ’ 或 ’ - ',先将相应的数字push入栈(如果是 ’ - ’ 则push数字的相反数)
② 如果遇到优先级大的 ’ * ’ 或 ’ / ’ 先将相应数字与栈顶元素进行运算后将结果push进入
③ 最后将栈中所有元素相加。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
signed main()
{
char c, s;
char s;
double n;
//开头特殊处理:0 +'\n'时结束;
while(~scanf("%lf%c",&n,&c)) {
if(n == 0 && c=='\n') break;
stack<double>stk;
double ans=0;
while(!stk.empty()) stk.pop();
stk.push(n);
while(~scanf("%c %lf",&s,&n)) {
if (s == '+') stk.push(n);
if (s == '-') stk.push(-n);
if (s == '*') {
double tmp = stk.top() * n;
stk.pop();
stk.push(tmp);
}
if (s=='/') {
double tmp = stk.top() * 1.0 / n;
stk.pop();
stk.push(tmp);
}
c = getchar();
if (c == '\n') break;
}
while (!stk.empty()) {
ans += stk.top();
st.pop();
}
printf("%.2lf\n",ans);
}
return 0;
}
题解:用一个数组a[ ]进行计数,首先初始化a[ ]数组,全部为0
比如说:3 2 3 4 3 2 1, 算出其中最大的数字 maxx = max(maxx, a[i]); max()函数是比较两个数字大小;
如果是C语言的话需要自己写一个max()函数
int max (int n) {
if (a >= b) return a;
else return b;
}
遍历输入的数字然后依次执行
a[3]++;
a[2]++;
a[3]++;
a[4]++;
a[3]++;
a[2]++;
a[1]++;
此时最大的数字为4
然后从最大往最小遍历
for (int i = maxx; i >= 1; --i)
如果a[i] 不为 0,则输出 if (a[i])
注意输出格式。即可
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 10010;
int a[N];
signed main() {
int n = read();
int maxx = 0;
memset(a, 0, sizeof a);
for (int i = 1; i <= n; ++i) {
int x = read();
a[x]++;
maxx = max(maxx, x);
}
for (int i = maxx; i >= 1; --i) {
if (a[i]) printf("%lld-%lld\n", i, a[i]);
}
return 0;
}
题解:递推
f ( n ) = f ( n − 1 ) + f ( n − 2 ) f(n)=f(n-1)+f(n−2) f(n)=f(n−1)+f(n−2) 记得最后要 %1000000007
#include
#include
const int N = 110;
int f[N];
int main()
{
int n;
scanf ("%d", &n);
if (n == 1) printf("1\n");
f[1] = 1;
f[2] = 2;
for (int i = 3; i <= n; i++) {
f[i] = (f[i - 2] + f[i - 1]) % 1000000007;
}
printf("%d\n", f[n]);
}
题解:要求最少次数,所以自然是先 % 的数字越大,操作数最少
所以依次从 5 到 3 再到 2;
注意数据范围很大要用 long long 定义变量
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
signed main()
{
int n = read();
while (n--) {
int x = read();
int ans = 0;
while (x > 1) {
int flag = 0;
if (x % 5 == 0) {
x = x / 5 * 4;
flag = 1;
} else if(x % 3==0) {
x= x / 3 * 2;
flag = 1;
} else if (x % 2 == 0) {
x = x / 2;
flag = 1;
}
ans++;
if (!flag) break;
}
if (x == 1) printf("%lld\n",ans);
else printf("-1\n");
}
return 0;
}
前缀和算法是一个必须要掌握的算法,详细解释Click here ~~
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 1010;
int a[N];
int sum[N];
signed main() {
int n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
sum[i] = sum[i - 1] + a[i];
}
int t = read();
while(t--) {
int l = read(), r = read();
if (l > n) {
printf("0\n");
continue;
}
if (r > n) r = n;
printf("%lld\n", sum[r] - sum[l - 1]);
}
return 0;
}
最长非回文字符串包括三种情况:
① 如果这整个字符串每个字符都一样,那么此时最长的非回文子串长度 == 0
② 如果这整个字符串是回文字符串,那么此时最长的非回文子串长度 == s.size()-1
③ 这整个字符串不是回文字符串,那么此时最长非回文子串长度 == s.size()
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
const int N = 50010;
string s;
signed main()
{
cin >> s;
int l = 0, r = s.size() - 1;
char a = s[0];
int flag = 1;
while (l <= r) {
if (s[l] != a || s[r] != a) flag = 0;
if (s[l] == s[r]) l++, r--;
else break;
}
if (l > r && flag) printf("0\n");//情况①
else if (l > r) printf("%lld\n", s.size() - 1);//情况②
else printf("%lld\n", s.size());//情况③
return 0;
}
注意一下空格的输出方式
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
//最大公约数
int gcd(int x,int y) {
if(x<y) swap(x,y);//很多人会遗忘,大数在前小数在后
//递归终止条件千万不要漏了,辗转相除法
return x % y ? gcd(y, x % y) : y;
}
//计算x和y的最小公倍数
int lcm(int x,int y) {
return x * y / gcd(x, y);//使用公式
}
int ksm(int a, int b, int mod) {
int s = 1; while(b) {
if(b&1) s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
signed main() {
int n = read();
int kase = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n + n; ++j) {
if (i == j) printf("V");
else if (j == n + n - kase) {
printf("V");
kase++;
}else printf(" ");
}
printf("\n");
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
const int N = 200010;
signed main() {
int h1 = read(), m1 = read(), h2 = read(), m2 = read();
int h3 = 0, m3 = 0;
h3 = h2 - h1;
if (m1 > m2) {
h3--;
m3 = 60 - m1 + m2;
} else {
m3 = m2 - m1;
}
printf("%lld %lld\n", h3, m3);
return 0;
}
题解:简单的去重和排序
C++做法的STL sort()和unique()这两个函数,就是这么简单
#include
#include
#include
using namespace std;
int main()
{
int n;
scanf("%lld", &n);
int a[1010];
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
sort(a, a + n);
int len = unique(a, a + n) - a;//去重,并返回去重后的长度
printf("%lld\n", len);//输出去重后的长度
for (int i = 0; i < len; i++) printf("%lld ",a[i]);
printf("\n");
return 0;
}
C语言做法:就要自己手写两个函数,第一个是排序,第二个去重。
#include
int main()
{
int n;
int a[1010];
int b[1010];
int i, j, t;
int cnt;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
//排序 可以用C++ 的STL sort()函数代替
for (i = 1; i <= n; i++) {
for (j = i + 1; j <= n; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
//去重 可以用C++ 的STL unique()函数代替
cnt = 0;
for (i = 1; i <= n; i++) {
if (a[i] != a[i + 1]) {
cnt++;
b[cnt] = a[i];
}
}
printf("%d\n", cnt);
for (i = 1; i <= cnt; i++) printf("%d ",b[i]);
return 0;
}
可以不用数组,理解一下代码。
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
signed main(){
int n = read();
int ans = 1, cnt = 1;
int pre = read();
n--;
while (n--) {
int cur = read();
if (pre < cur) cnt++;
else cnt = 1;
ans = max(ans, cnt);
pre = cur;
}
printf("%lld\n", ans);
return 0;
}
题解:
杨辉三角,有个小坑,注意一下每一行最后不能有空格
a [ i ] [ j ] = a [ i − 1 ] [ j − 1 ] + a [ i − 1 ] [ j ] a[i][j]=a[i-1][j-1]+a[i-1][j] a[i][j]=a[i−1][j−1]+a[i−1][j]
#include
#include
#include
#include
#include
#include
#include
#include
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
int a[25][25];
signed main()
{
int n = read();
for (int i = 1; i <= n; i++) a[i][1] = a[i][i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 2; j < i; j++) {
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
}
for (int i = 1; i <= n; i++) {
printf("%lld", a[i][1]);
for (int j = 2; j <= i; j++) {
printf(" %lld", a[i][j]);
}
printf("\n");
}
return 0;
}