[Offer收割]编程练习赛41

时间:2023-03-09 02:46:59
[Offer收割]编程练习赛41

比赛日程安排

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
freopen("input.txt", "w", stdout);
cout << << endl; for(int i = ; i < ; i++) cout << << ' '; fclose(stdout);
} const int day[] = { , , , , , , , , , , , };
VI a[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
//std::ios::sync_with_stdio(0), cin.tie(0);
int n, m, t;
scanf("%d", &t); while(t--) {
bool ok = true;
scanf("%d%d", &n, &m); for(int i = ; i <= n; i++) a[i].clear(); for(int i = ; i < m; i++) {
int mm, d, u, v, tmp = ;
scanf("%d-%d%d%d", &mm, &d, &u, &v); for(int j = ; j < mm; j++)tmp += day[j - ]; tmp += d;
a[u].push_back(tmp);
a[v].push_back(tmp);
} for(int i = ; i <= n; i++) {
sort(a[i].begin(), a[i].end()); for(int j = ; j < a[i].size(); j++) {
if(a[i][j] - a[i][j - ] <= ) ok = false;
}
} if(ok) printf("YES\n");
else printf("NO\n");
} return ;
}

反转子串

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
#include<set>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII; char str[];
int l[], r[], s[]; void work(int L, int R, int rev) {
int i;
if (L > R) return;
if (rev & ) {
i = R;
while (i >= L) {
if (str[i] == ')') {
work(l[i] + , i - , rev + );
i = l[i] - ;
} else {
putchar(str[i]);
i--;
}
}
} else {
i = L;
while (i <= R) {
if (str[i] == '(') {
work(i + , r[i] - , rev + );
i = r[i] + ;
} else {
putchar(str[i]);
i++;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//std::ios::sync_with_stdio(0), cin.tie(0);
scanf("%s", str);
int n = strlen(str);
int dep = , top;
memset(l, , sizeof(l));
memset(r, , sizeof(r));
for (int i = ; i < n; i++) {
if (str[i] == '(') {
s[dep++] = i;
} else if (str[i] == ')') {
top = s[dep - ];
l[i] = top;
r[top] = i;
dep--;
}
}
work(, n - , );
return ;
}
//agfdecbhijk

区间问题

把所有区间按结束位置排序,从小到大处理。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
freopen("input.txt", "w", stdout);
cout << << endl;
for (int i = ; i < ; i++) cout << << ' ';
fclose(stdout);
} PII a[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n, x, y;
cin >> n;
for (int i = ; i < n; i++) {
cin >> x >> y;
a[i] = make_pair(y, x);
}
sort(a, a + n);
int l = a[].first - , r = a[].first, ans = ;
for (int i = ; i < n; i++) {
if (a[i].second <= l) continue;
if (a[i].second <= r) {
ans++;
l = r;
r = a[i].first;
} else {
ans += ;
l = a[i].first - , r = a[i].first;
}
}
cout << ans << endl;
return ;
}

01间隔矩阵

比我想的简单一点,我觉得会超时的代码AC了,可能是因为数据是随机生成所以长宽都不会太大的缘故。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
freopen("input.txt", "w", stdout);
int n = , m = , x = , c;
cout << n << ' ' << m << endl;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
for (int k = ; k < x; k++) c = rand() % ;
cout << c;
}
cout << endl;
}
fclose(stdout);
} char a[][];
int h[], c[], l[], r[], tmph[], ans;
void calc(int ll, int rr) {
for (int i = ll; i <= rr; i++) tmph[i] = h[i];
tmph[ll - ] = , tmph[rr + ] = ;
l[ll] = ll - , r[rr] = rr + ;
for (int i = ll + ; i <= rr; i++) {
l[i] = i - ;
while (tmph[l[i]] >= tmph[i]) l[i] = l[l[i]];
}
for (int i = rr - ; i >= ll; i--) {
r[i] = i + ;
while (tmph[r[i]] >= tmph[i]) r[i] = r[r[i]];
}
for (int i = ll; i <= rr; i++) ans = max(ans, tmph[i] * (r[i] - l[i] - ));
} int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n, m;
cin >> n >> m;
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
cin >> a[i][j];
}
}
ans = ;
for (int i = ; i <= n; i++) {
h[] = ;
if (i == ) {
for (int j = ; j <= m; j++) h[j] = ;
} else {
for (int j = ; j <= m; j++)
if (a[i][j] != a[i - ][j]) h[j]++;
else h[j] = ;
}
c[] = ;
for (int j = ; j <= m; j++)
if (a[i][j] != a[i][j - ]) c[j] = c[j - ];
else c[j] = c[j - ] ^ ;
int ptr = , ll, rr;
while (ptr <= m) {
ll = ptr;
while (ptr + <= m && c[ptr + ] == c[ptr]) ptr++;
rr = ptr;
calc(ll, rr);
ptr = rr + ;
}
}
cout << ans << endl;
return ;
}