724A Checking the Calendar
题目大意:题目感觉有点晦涩。。就是说给定不是闰年的某个月(1~11月)第一天是星期几,下个月的第一天是星期几,问你是否合法。
#include <cstdio>
int parseDay(char *ch) {
if (ch[0] == 'm' ) return 0;
if (ch[0] == 't' && ch[1] == 'u') return 1;
if (ch[0] == 'w' ) return 2;
if (ch[0] == 't' && ch[1] == 'h') return 3;
if (ch[0] == 'f' ) return 4;
if (ch[0] == 's' && ch[1] == 'a') return 5;
if (ch[0] == 's' && ch[1] == 'u') return 6;
return -1;
}
char ch[16];
int main() {
int a, b;
scanf("%s", ch); a = parseDay(ch);
scanf("%s", ch); b = parseDay(ch);
if ((a + 31) % 7 == b || (a + 28) % 7 == b || (a + 30) % 7 == b)
puts("YES");
else
puts("NO");
return 0;
}
724B Batch Sort
题目大意:给出n行,每行m个数字的表,可以对每行执行一次任意交换两数的1操作以及一次交换两列数字的操作,问是否能将表恢复成每行1..m的形式。
题解:
若不存在3~4个错位的,那么一一行内交换即可。
若没有错位的,直接忽略即可(假定我们要交换列,再交换回来就可以恢复了)
显然我们可以随意调整交换列操作的操作顺序,因为行内交换的任意的两个数(如果只能交换相邻元素题目就复杂多了),所以我们优先处理交换列。
若存在1行中有3~4个错位的,那么必定交换了列(不存在5个及5个以上的,存在则NO)
那么交换列至多有6种情况(
C24
)
如果多行存在3个错位的求交集(交集为空则NO)
交集元素个数至多为4(每行3~4错位的),那么枚举交换列可能的情况即可。
然后如果各行都不超过2个错位的就合法。
O(nm)
不过就n和m的范围。。。
O(nm3)
也没啥问题。。
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(i=j;i<k;++i)
const int N = 32;
int n, m;
int a[N][N], g[N][N], c[N];
int t[N], s[N], tl, sl = 0;
int checkOutMisplacement(int row, int *t) {
int i, p = 0;
rep(i,0,m) if (a[row][i] != i + 1) t[p++] = i;
return p;
}
bool judge() {
int i;
rep(i,0,n) if (checkOutMisplacement(i, t) > 2) return false;
return true;
}
int main() {
int i, j, k, flag = false, must = false;
scanf("%d%d", &n, &m);
rep(i,0,n) rep(j,0,m) scanf("%d", &a[i][j]);
rep(i,0,n) {
c[i] = checkOutMisplacement(i, g[i]);
if (c[i] > 4) return puts("NO"), 0;
if (c[i] >= 3) must = true;
if (!c[i]) continue;
if (flag) {
copy(s, s + sl, t); tl = sl;
sl = set_intersection(t, t + tl, g[i], g[i] + c[i], s) - s;
} else {
copy(g[i], g[i] + c[i], s); sl = c[i];
flag = true;
}
}
if (!must) return puts(judge() ? "YES" : "NO"), 0;
rep(i,0,sl) rep(j,i+1,sl) {
rep(k,0,n) swap(a[k][s[i]], a[k][s[j]]);
if (judge()) return puts("YES"), 0;
rep(k,0,n) swap(a[k][s[i]], a[k][s[j]]);
}
return puts("NO"), 0;
}
724C
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100010;
typedef pair<ll, ll> P;
ll mod(ll a, ll b, ll moder) {
if (b <= 100)
return a * b % MOD;
return (a * (b % 100) % MOD + mod(a, b / 100, MOD) * 100 % MOD) % MOD;
}
ll power(ll a, ll b) {
ll ans = 1;
for (; b; b /= 2, a *= a)
if (b & 1) ans *= a;
return ans;
}
ll extgcd(ll a, ll b, ll &x, ll &y){
if (b == 0) { x = 1; y = 0; return a; }
ll ans = extgcd(b, a % b, x, y);
ll p = y;
y = x - a / b * y;
x = p;
if (x < 0)
{
y += (x / b - 1) * a;
x -= (x / b - 1) * b;
}
if (x > b)
{
y += x / b * a;
x %= b;
}
return ans;
}
P crt(P p1, P p2)
{
ll x, y;
if (p1.second == 0 || p2.second == 0)
return make_pair(0, 0);
if (p2.first < p1.first)
{
P p = p1;
p1 = p2;
p2 =p;
}
ll gcd = extended_euclid(p1.second, p2.second, x, y);
if ((p2.first - p1.first) % gcd)
return make_pair(0, 0);
ll lcm = p1.second / gcd * p2.second;
ll ans = (p2.first - p1.first) / gcd;
ans = mod(ans, x, lcm);
ans = mod(ans, p1.second, lcm);
ans = (ans + p1.first) % lcm;
return make_pair(ans, lcm);
}
int main()
{
int n, m, k, x, y;
ll ans[N];
for (int i = 0; i < N; i++)
ans[i] = -1;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; i++)
{
scanf("%d%d", &x, &y);
P solve = crt(make_pair(x, n), make_pair(y, m));
if (solve.second && !(solve.first / n & 1) && !(solve.first / m & 1))
{
if (ans[i] == -1)
ans[i] = solve.first;
else
ans[i] = min(ans[i], solve.first);
}
solve = crt(make_pair(n - x, n), make_pair(y, m));
if (solve.second && (solve.first / n & 1) && !(solve.first / m & 1))
{
if (ans[i] == -1)
ans[i] = solve.first;
else
ans[i] = min(ans[i], solve.first);
}
solve = crt(make_pair(x, n), make_pair(m - y, m));
if (solve.second && !(solve.first / n & 1) && (solve.first / m & 1))
{
if (ans[i] == -1)
ans[i] = solve.first;
else
ans[i] = min(ans[i], solve.first);
}
solve = crt(make_pair(n - x, n), make_pair(m - y, m));
if (solve.second && (solve.first / n & 1) && (solve.first / m & 1))
{
if (ans[i] == -1)
ans[i] = solve.first;
else
ans[i] = min(ans[i], solve.first);
}
}
for (int i = 0; i < k; i++)
printf("%I64d\n", ans[i]);
return 0;
}
724D
题目大意:给出一个字符串,选出一些字符(集合为
C
),使得对任意的字串[j..j+m-1],存在
题解:注意到字符串的字典序最小时,如果已知字典序最大的字符k,那么[‘a’,k)的字符一定越多越好,即字符串里的所有的小于k的字符。对于k,一定是越少越好,要求我们选出一些字符,使得可以连接[‘a’,k)选中的区段(显然最终C中选中的字符间距不能超过m,这里认为间距不超过m的字符时连续的)。
#include <cstdio>
#include <cstring>
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define id(i) ((i) - 'a')
const int N = 100005;
char s[N];
int p[N], c[N];
int main() {
int m, n, i, j, k, now;
scanf("%d%s", &m, s + 1);
n = strlen(s + 1);
for (char g = 'a'; g <= 'z'; ++g) {
k = 0;
FOR(i,1,n) if (s[i] <= g) p[++k] = i;
p[++k] = n + 1;
FOR(i,1,k) if (p[i] - p[i - 1] > m) goto hell;
k = 0;
FOR(i,1,n) if (s[i] < g) p[++k] = i, ++c[id(s[i])];
p[++k] = n + 1;
now = 0;
FOR(i,1,k) {
while (p[i] - now > m)
for (j = now + m; j > now; --j)
if (s[j] == g) {
++c[id(g)]; now = j; break;
}
now = p[i];
}
for (char h = 'a'; h <= g; ++h)
while (c[id(h)]) putchar(h), c[id(h)]--;
break;
hell: continue;
}
return 0;
}