【AtCoder】CODE FESTIVAL 2016 qual A
CODE FESTIVAL 2016 qual A
A - CODEFESTIVAL 2016
……
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 500005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;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;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
string s;
void Solve() {
cin >> s;
cout << s.substr(0,4) << " " << s.substr(4) << endl;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
B - 仲良しうさぎ / Friendly Rabbits
……
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;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;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int a[MAXN];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
}
int ans = 0;
for(int i = 1 ; i <= N ; ++i) {
if(a[a[i]] == i) ++ans;
}
out(ans / 2);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
C - 次のアルファベット / Next Letter
先从前面到后面把能变成a的都变成a,然后把所有的操作给最后一个字母即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;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;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int K,N;
char s[MAXN];
void Solve() {
scanf("%s",s + 1);
read(K);
N = strlen(s + 1);
for(int i = 1 ; i <= N ; ++i) {
int t = s[i] - 'a';
if(t != 0) {
if(K >= 26 - t) {s[i] = 'a';K -= (26 - t);}
}
}
K %= 26;
int t = s[N] - 'a';
t = (t + K) % 26;
s[N] = 'a' + t;
for(int i = 1 ; i <= N ; ++i) {
putchar(s[i]);
}
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
D - マス目と整数 / Grid and Integers
\(b_{i,j} + b_{i + 1,j + 1} = b_{i,j + 1} + b_{i + 1,j}\)
可以得到\(b_{i,j + 1} - b_{i,j} = b_{i + 1,j + 1} - b_{i + 1,j}\)
同样的横列也可以得到
\(b_{i + 1,j} - b_{i,j} = b_{i + 1,j + 1} - b_{i,j + 1}\)
也就是我们可以找到两个数列\(x_{1}..x_{R}\)
和\(y_{1}..y_{C}\)
\(b_{i,j} = x_{i} + y_{j}\)
是满足对角线两列相加相等的
然后我们把\(x_{i} + y_{j}\)固定的连一条边,一个联通块内固定一个便可以使得所有点都标记上值,我们在标记的过程中同时看是否合法即可
那么如何保证非负呢
我们在一个联通块内,如果最小的x和最小的y相加是正的,以为一个联通块内y增加1会使得所有的x减少1,x增加1同理,这种情况使得所有的x和y都变成非负的,显然合法
如果最小的x和最小的y是负的,那么就不合法了,也无法调整了
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;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;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int R,C,N;
struct node {
int to,next;
int64 val;
}E[MAXN * 10];
int head[MAXN],sumE;
int64 val[MAXN],xmin,ymin;
bool vis[MAXN];
bool used[MAXN];
void add(int u,int v,int64 c) {
E[++sumE].to = v;
E[sumE].next = head[u];
E[sumE].val = c;
head[u] = sumE;
}
bool dfs(int u) {
vis[u] = 1;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(!vis[v]) {
val[v] = E[i].val - val[u];
if(!dfs(v)) return false;
}
else {
if(val[u] + val[v] != E[i].val) return false;
}
}
return true;
}
void find_min(int u) {
used[u] = 1;
if(u <= R) xmin = min(xmin,val[u]);
if(u > R) ymin = min(ymin,val[u]);
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(!used[v]) find_min(v);
}
}
void Solve() {
read(R);read(C);read(N);
int a,b;int64 c;
for(int i = 1 ; i <= N ; ++i) {
read(a);read(b);read(c);
add(a,b + R,c);add(b + R,a,c);
}
for(int i = 1 ; i <= R + C ; ++i) {
if(!vis[i]) {
if(!dfs(i)) {puts("No");return;}
xmin = 1e18,ymin = 1e18;
find_min(i);
if(xmin + ymin < 0) {puts("No");return;}
}
}
puts("Yes");
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
E - LRU パズル / LRU Puzzle
假如对一个序列进行了操作,这个序列最后的样子是未操作的数仍然有序放在最后,然后被操作的数从后往前扫描操作序列,第一个出现的数排在最前,重复出现的直接忽略即可
那么我们对假如Q次放到同一序列里,也做一遍这个操作,会得到一个最终序列,这个序列就是我们必须让每一个都变成这样的序列
序列从末尾到前非单调下降的第一个位置之前,就是每个序列都必须有的一段操作序列
于是我们从后往前扫看看这种操作序列最多是否超过N个即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;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;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M,Q;
int a[MAXN],pos[MAXN],reserve[MAXN];
bool vis[MAXN];
vector<int> v;
void Solve() {
read(N);read(M);read(Q);
v.pb(0);
for(int i = 1 ; i <= Q ; ++i) {read(a[i]);}
for(int i = Q ; i >= 1 ; --i) {
if(!vis[a[i]]) {
v.pb(a[i]);
vis[a[i]] = 1;
}
}
for(int i = 1 ; i <= M ; ++i) {
if(!vis[i]) v.pb(i);
}
for(int i = 1 ; i <= M ; ++i) pos[v[i]] = i;
int p = M;
while(p > 1 && v[p - 1] < v[p]) --p;
if(p == 1) {puts("Yes");return;}
for(int i = Q ; i >= 1 ; --i) {
if(a[i] == v[1]) reserve[1]++;
else {
if(reserve[pos[a[i]] - 1]) {
reserve[pos[a[i]] - 1]--;
reserve[pos[a[i]]]++;
}
}
}
int res = 0;
for(int i = p - 1 ; i <= M ; ++i) {
res += reserve[i];
}
if(res >= N) {puts("Yes");return;}
else {puts("No");return;}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}