Codeforces #305 (div2)

本文提供了四道算法竞赛题目的解答过程及思路,包括字符串回文判断、动态更新矩阵最大值、循环周期计算以及利用单调栈解决特定问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话说新的萌萌的图 来了一波呀~ 想起了叉姐的头像 Orz


A. Mike and Fax
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s.

He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.

He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.

Input

The first line of input contains string s containing lowercase English letters (1 ≤ |s| ≤ 1000).

The second line contains integer k (1 ≤ k ≤ 1000).

Output

Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.

Sample test(s)
input
saba
2
output
NO
input
saddastavvat
2
output
YES
水题 - - 看除k之后每个是不是回文 手残把s.size()写成了n 然后过了pretest 然后就无情fst了

AC代码如下:

//
//  Created by TaoSama on 2015-05-27
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

string s;
int n;

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(cin >> s >> n) {
        int c = s.size() / n;
        //cout << c<<endl;
        if(s.size() % n != 0) {
            cout << "NO\n";
            continue;
        }
        bool yes = true;
        for(int i = 0; i < s.size(); i += c) {
            for(int j = i, k = i + c - 1; j < k; ++j, --k) {
                if(s[j] != s[k]) {
                    yes = false;
                    break;
                }
            }
            if(!yes) break;
        }
        if(yes) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}

B. Mike and Fun
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes.

They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.

Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.

Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round.

Input

The first line of input contains three integers nm and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000).

The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0(for mouth) or 1 (for eyes).

The next q lines contain the information about the rounds. Each of them contains two integers i and j (1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.

Output

After each round, print the current score of the bears.

Sample test(s)
input
5 4 5
0 1 1 0
1 0 0 1
0 1 1 0
1 0 0 1
0 0 0 0
1 1
1 4
1 1
4 2
4 3
output
3
4
3
3
4
维和每行的max 然后更新之后扫一遍行就可以了

AC代码如下:

//
//  Created by TaoSama on 2015-05-27
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, m, q, a[505][505], Max[505];

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= n; ++i) {
        int t = 0, ct = 0;
        for(int j = 1; j <= m; ++j) {
            scanf("%d", &a[i][j]);
            if(a[i][j]) {
                ++ct;
                t = max(t, ct);
            } else ct = 0;
        }
        Max[i] = t;
    }

    while(q--){
		int x, y; scanf("%d%d", &x, &y);
		a[x][y] = !a[x][y];
		int ct = 0, t = 0;
		for(int i = 1; i <= m; ++i){
			if(a[x][i]) {
                ++ct;
                t = max(t, ct);
            } else ct = 0;
		}
		Max[x] = t;
		printf("%d\n", *max_element(Max + 1, Max + 1 + n));
    }

    return 0;
}


C. Mike and Frog
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar.

So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become  and height of Abol will become  where x1, y1, x2 and y2 are some integer numbers and  denotes the remainder of amodulo b.

Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.

Mike has asked you for your help. Calculate the minimum time or say it will never happen.

Input

The first line of input contains integer m (2 ≤ m ≤ 106).

The second line of input contains integers h1 and a1 (0 ≤ h1, a1 < m).

The third line of input contains integers x1 and y1 (0 ≤ x1, y1 < m).

The fourth line of input contains integers h2 and a2 (0 ≤ h2, a2 < m).

The fifth line of input contains integers x2 and y2 (0 ≤ x2, y2 < m).

It is guaranteed that h1 ≠ a1 and h2 ≠ a2.

Output

Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

Sample test(s)
input
5
4 2
1 1
0 1
2 3
output
3
input
1023
1 2
1 0
1 2
1 1
output
-1


- - 这题主观臆断一定循环了 然后知道了老老实实做就好了 有一点就是根据鸽笼原理 m时间内一定有循环 m都没找到就真的找不到了AC代码如下:

//
//  Created by TaoSama on 2015-05-27
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

long long m, h1, a1, x1, y1, h2, a2, x2, y2;

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    cin >> m;
    cin >> h1 >> a1 >> x1 >> y1;
    cin >> h2 >> a2 >> x2 >> y2;

    long long ans = 0;
    //求a的不循环节长
    while(h1 != a1) {
        h1 = (h1 * x1 + y1) % m;
        h2 = (h2 * x2 + y2) % m;
        ++ ans;
        if(ans > m) {
            cout << "-1\n";
            return 0;
        }
    }
    if(h2 == a2) {
        cout << ans << "\n";
        return 0;
    }

    long long per = 0, px2 = 1, py2 = 0;
    //a的循环节长度
    while(per == 0 || h1 != a1) {
        h1 = (h1 * x1 + y1) % m;
        px2 = (px2 * x2) % m;
        py2 = (py2 * x2 + y2) % m;
        ++per;
        if(per > m) {
            cout << "-1\n";
            return 0;
        }
    }

    long long cycle = 0;
    //来暴力匹配下b 上界是m/per 直接写m也行
    while(h2 != a2) {
        h2 = (px2 * h2 + py2) % m;
        ++cycle;
        if(cycle > m) {
            cout << "-1\n";
            return 0;
        }
    }
    ans += per * cycle;
    cout << ans << "\n";

    return 0;
}

D. Mike and Feet
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.

A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strengthof a group is the minimum height of the bear in that group.

Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.

Input

The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears.

The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears.

Output

Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.

Sample test(s)
input
10
1 2 3 4 5 4 3 2 1 6
output
6 4 4 3 3 2 2 1 1 1 

用单调栈来维护 以a[i]为最小的两个区间边界 单调栈是

 j < i, a[j] < a[i]的最大j

 j > i a[j] < a[i]的最小j 

说白了a[j] >=a[i]的左右两个边界 这一点也可以用dp的思想来维护

然后这个边界内都以a[i]为最小值  然后ans显然有一个性质 ans1 >= ans2 >= ans3 >= ... >= ans_n

然后用partial sum的思想取一下max 然后输出就好了

AC代码如下:

//
//  Created by TaoSama on 2015-05-27
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;

int n, a[N], l[N], r[N], ans[N];

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);


	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", a + i);

	stack<int> s;
	for(int i = 1; i <= n; ++i){
		while(s.size() && a[s.top()] >= a[i]) s.pop();
		l[i] = s.size() ? s.top() : 0;
		s.push(i);
	}
	while(s.size()) s.pop();
	for(int i = n; i >= 1; --i){
		while(s.size() && a[s.top()] >= a[i]) s.pop();
		r[i] = s.size() ? s.top() : n + 1;
		s.push(i);
	}
	/*for(int i = 1; i <= n; ++i)
		cout<<l[i]<<' ';
	cout<<endl;
	for(int i = 1; i <= n; ++i)
		cout<<r[i]<<' ';
	cout<<endl;*/
	for(int i = 1; i <= n; ++i){
		int width = r[i] - l[i] - 1;
		ans[width] = max(ans[width], a[i]);
	}
	/*for(int i = 1; i <= n; ++i)
		cout<<ans[i]<<' ';
	cout<<endl;*/
	for(int i = n - 1; i >= 1; --i)
		ans[i] = max(ans[i], ans[i+1]);
	for(int i = 1; i <= n; ++i)
		printf("%d%c", ans[i], i == n ? '\n' : ' ');
	return 0;
}

//
//  Created by TaoSama on 2015-05-28
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;

int n, a[N], l[N], r[N], ans[N];

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", a + i);
	for(int i = 1; i <= n; ++i) l[i] = r[i] = i;

	for(int i = 1; i <= n; ++i){
		while(a[l[i] - 1] >= a[i])
			l[i] = l[l[i] - 1];
	}
	for(int i = n; i >= 1; --i){
		while(a[r[i] + 1] >= a[i])
			r[i] = r[r[i] + 1];
	}

	for(int i = 1; i <= n; ++i){
		int w = r[i] - l[i] + 1;
		ans[w] = max(ans[w], a[i]);
	}
	for(int i = n - 1; i >= 1; --i)
		ans[i] = max(ans[i], ans[i + 1]);
	for(int i = 1; i <= n; ++i)
		printf("%d%c", ans[i], i == n ? '\n' : ' ');
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值