A. Maxim and Biology
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int route[26][26];
void solve() {
for(int u = 0; u < 26; u += 1) {
for(int v = 0; v < 26; v += 1) {
if(u <= v)
route[u][v] = min(abs(v - u), u + 26 - v);
else
route[u][v] = min(abs(u - v), v + 26 - u);
}
}
}
int getLength(string s, int i) {
return route[s[i] - 'A'][0] + route[s[i + 1] - 'A']['C' - 'A'] + route[s[i + 2] - 'A']['T' - 'A'] + route[s[i + 3] - 'A']['G' - 'A'];
}
int main() {
solve();
int n;
string s;
cin >> n >> s;
int length = s.size();
int Min = 100;
for(int i = 0; i < length - 3; i += 1) {
Min = min(Min, getLength(s, i));
}
cout << Min;
return 0;
}
B. Dima and a Bad XOR
暴力
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int N, M, x, vis[1024], result, G[505][505];
bool sign;
vector<int> vec[505], path, lastpath;
void DFS(int dep, int n) {
if(sign)
return ;
if(dep == N + 1) {
if(n != 0) {
result = n;
lastpath = path;
sign = true;
}
return ;
}
int length = vec[dep].size();
for(int i = 0; i < length; i += 1) {
int T = vec[dep][i];
path.push_back(T);
DFS(dep + 1, n ^ G[dep][T]);
path.pop_back();
}
return ;
}
int main() {
scanf("%d%d", &N, &M);
for(int r = 1; r <= N; r += 1) {
memset(vis, 0, sizeof(vis));
for(int c = 1; c <= M; c += 1) {
scanf("%d", &G[r][c]);
if(vis[G[r][c]])
continue;
vis[G[r][c]] = 1;
vec[r].push_back(c);
}
}
sign = false;
result = 0;
DFS(1, 0);
if(result == 0)
printf("NIE");
else {
printf("TAK\n");
for(int i = 0; i < lastpath.size(); i += 1) {
if(i != 0)
printf(" ");
printf("%d", lastpath[i]);
}
}
return 0;
}
C. Problem for Nazar
题意:有两个集合,一个是奇数集合(1,3,5,7,…),一个是偶数集合 (2,4,6,8,…),现构造如下序列,第一次操作,从奇数集合中取1个数(1),第二次操作,从偶数序列中取2两个数(2,4),第三次操作从奇数集合中取4个数(3,5,7,9),第四次操作从偶数集合中取8个数(6,8,10..)以此类推,构造一个无穷大的数列,题目给你一个区间[l,r]问,区间和是多少?
思路:求区间和,那么想到一个求区间和的公式S[l..r]=S[1...r]-S[1...l-1],分别统计区间[1...r]和[1...l-1]当中奇数和偶数的个数,等差数列求和然后作差
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e2;
long long solve(long long x) {
int k = 1;
long long n[2];
memset(n, 0, sizeof(n));
long long s = 1;
while(x) {
n[k] += min(s, x);
x -= min(x, s);
s <<= 1;
k ^= 1;
}
for(int i = 0; i < 2; i += 1)
n[i] = n[i] % M;
long long res = (2 * n[0] % M + n[0] * max(0LL, n[0] - 1) % M) % M;
res = (res + n[1] % M + n[1] * max(0LL, n[1] - 1) % M) % M;
return res;
}
int main() {
lo ng long l, r;
scanf("%lld%lld", &l, &r);
printf("%lld\n", (solve(r) - solve(l - 1) + M) % M);
return 0;
}
D. Stas and the Queue at the Buffet
题意:初始有n个元素,每个元素有两种属性(a,b),每个元素对序列的贡献的计算公式是a⋅(j−1)+b⋅(n−j) ,j的含义是该元素在序列中的位置,现在希望你能找到一种排列,使得所有元素对序列的贡献和最小,输出这个最小值
思路:计算元素贡献的式子可以拆开 a⋅(j−1)+b⋅(n−j) = (a-b)j+bn-a,我们发现bn-a与元素所处的位置无关,所以现在需要我们讨论的问题就是使前半部分(a-b)j尽量小,a-b已知,怎么安排j也很显然
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
long long a[N], b[N], c[N], res;
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i += 1) {
scanf("%lld%lld", &a[i], &b[i]);
c[i] = a[i] - b[i];
res += b[i] * n - a[i];
}
sort(c + 1, c + 1 + n);
for(int i = 1; i <= n; i += 1) {
res += c[i] * (n - i + 1);
}
printf("%lld\n", res);
return 0;
}
E. Number of Components
官方题解:
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
int a[N];
long long res;
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i += 1)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i += 1) {
if(a[i] > a[i - 1])
res += (long long)(a[i] - a[i - 1]) * (n - a[i] + 1); //l的取值个数为(a[i] - a[i - 1]),r的取值个数为(n - a[i] + 1)
if(a[i] < a[i - 1])
res += (long long)(a[i - 1] - a[i]) * a[i]; //l的取值个数为a[i],r的取值个数为(a[i - 1] - a[i])
}
printf("%lld\n", res);
return 0;
}