VK Cup 2018 - Round 1+Codeforces Round #470
A. Primal Sport
题意:有两个人轮流玩游戏。给出数X(i-1),轮到的人需要找到一个小于X(i-1)的素数x,然后得到Xi,Xi是x的倍数中大于等于X(i-1)的最小的数。现在已知X2,求最小的X0?
思路:根据题意,X1的取值范围为【X1-X2的最大质因子+1,X2),同理可知X0的取值范围为【X1-X1的最大质因子+1,,X1)。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int Res[maxn],tmp[maxn];
void Init()
{
memset(tmp, -, sizeof(tmp));
for (int i = ; i < maxn; i++)
{
if (tmp[i] == -)
{
for (int j = *i; j < maxn; j += i) tmp[j] = i;//标记质因子
}
if (tmp[i] == -) Res[i] = i;//为质数
else Res[i] = i - tmp[i] + ;//不是质数,tmp[i]为最大质因子
}
}
int main()
{
Init();
int n;
scanf("%d", &n);
int L = Res[n];
int ans = n;
for (int i = L; i < n; i++) ans = min(Res[i], ans);
printf("%d\n", ans);
return ;
}
B. Producing Snow
题意:有n天,每天都会产生Vi体积的雪花堆(堆与堆之间独立),每天存在的雪花都会融化Ti,问n天中各天融化的体积和。
思路:用优先队列或最小堆维护,将Vi+sum(T1-Ti-1)加入队列或堆。堆中所有小于等于sum(T1-Ti)的都需要pop,当天融化的体积为所有pop出来的减去sum(Ti-Ti-1)加上还在堆里的数目*Ti.
堆实现:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
const int maxn = ;
int V[maxn],T[maxn];
vector<long long>Heap;
int main()
{
int n;
scanf("%d", &n);
make_heap(Heap.begin(), Heap.end(), greater<long long>());//建立最小堆
for (int i = ; i <= n; i++) scanf("%d", V + i);
for (int i = ; i <= n; i++) scanf("%d", T + i);
long long pre = ;
for (int i = ; i <= n; i++)
{
Heap.push_back(V[i]+pre);
push_heap(Heap.begin(), Heap.end(), greater<long long>());
long long tot = ;
while (Heap.size()&&Heap[] <= T[i]+pre)
{
tot += Heap[]-pre;
pop_heap(Heap.begin(), Heap.end(), greater<long long>());
Heap.pop_back();
}
if (Heap.size())
{
tot += 1ll*T[i]*Heap.size();
}
if (i == n) printf("%I64d\n", tot);
else printf("%I64d ", tot);
pre += T[i];
}
return ;
}
优先队列实现:
#include<queue>
#include<functional>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = ;
int V[maxn], T[maxn]; int main()
{
priority_queue<long long, vector<long long>, greater<long long> >pq;
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", V + i);
}
for (int i = ; i <= n; i++)
{
scanf("%d", T + i);
}
long long pre = ;
for (int i = ; i <= n; i++)
{
pq.push(V[i] + pre);
long long tot = ;
while (pq.size() && pq.top() <= pre + T[i])
{
tot += pq.top() - pre;
pq.pop();
}
tot += pq.size()*T[i];
pre += T[i];
if (i == n) printf("%I64d\n", tot);
else printf("%I64d ",tot);
}
return ;
}
C. Perfect Security
题意:有n个数字构成的序列A,有一串n个数的key——P,求出key的某一个重新排序的序列,使得Ai^Pi最后得到的结果串字典序最小,并输出该结果串。
思路:01异或字典树。注意插入的细节;查询时记得数目标记减1,因为P中每个数只能用一次。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = ;
int A[maxn],P[maxn];
const int bits = ;
const int SIZE = ;
struct Trie
{
int Nums[maxn*bits][SIZE];
int ex[maxn*bits*SIZE];//经过结点i的数的数目
int Tot; void clear()
{
memset(Nums[], , sizeof(Nums[]));
Tot = ;
} void insert(int Num)
{
int Now = , curchr;
for (int i = ; i >=; i--)
{
int c = ((Num>>i)&);
if (!Nums[Now][c])
{
memset(Nums[Tot], , sizeof(Nums[Tot]));
ex[Tot] = ;
Nums[Now][c] = Tot++;
}
ex[Nums[Now][c]]++;//表示经过该点的数的数目
Now = Nums[Now][c];
}
}
int Search(int Num)
{
int u = ,c;
for (int i = ; i >=; i--)
{
c =((Num>>i)&);
if (ex[Nums[u][c]])
{
u = Nums[u][c];
ex[u]--;
Num ^= (c << i);
}
else
{
u = Nums[u][c^];
ex[u]--;
Num ^= ((c ^ ) << i);
}
}
return Num;
} }tree;
int main()
{
tree.clear();
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", A + i);
}
for (int i = ; i <= n; i++)
{
scanf("%d", P+i);
}
for (int i = ; i <= n; i++) tree.insert(P[i]);
for (int i = ; i <= n; i++)
{
int ans = tree.Search(A[i]);
if (i == n) printf("%d\n", ans);
else printf("%d ", ans);
}
return ;
}
D. Picking Strings
题意:给出S原串和T原串,每次各选取一个子区间,问在题目的变换下:A->BC,B->AC,C->AB,AAA->empty,是否能够从S串变换到T串?
思路:
B->AC->AAB->AAAC->C;C->AB->AAC->AAAB->B(B和C等价,可以将所有C替换为B)
AB->AAC->AAAB->B;B->AC->AB(B前面的A可以任意增减)
A->BC->BB(A可以转换为BB)
B->AB->BBB(已有B时,B的数量增加任意偶数个)
故只需考虑子串末尾A。由于末尾A无法被转化成,故原串和目标串末尾A数目需要保留相同个数
1.当T串B少,无解(B无法消除,只能增加)
2.当两串B数目奇偶数不同,无解(B只能增加偶数个)
3.T串末尾A比S串多,无解(末尾A无法增加,只能减少)
否则(两串B数目奇偶相同):
4.T串B多,S串末尾A多,必有解
5.两串B相同,S串末尾A比T串多的数目为3的倍数时有有解
6.两串A相同,S串有B即可。
7.其他无解
//B->AC->AAB->AAAC->C;C->AB->AAC->AAAB->B(B和C等价,可以将所有C替换为B)
//AB->AAC->AAAB->B;B->AC->AB(B前面的A可以任意增减)
//A->BC->BB(A可以转换为BB)
//B->AB->BBB(已有B时,B的数量增加任意偶数个)
//故只需考虑子串末尾A。由于末尾A无法被转化成,故原串和目标串末尾A数目需要保留相同个数
//1.当T串B少,无解(B无法消除,只能增加)
//2.当两串B数目奇偶数不同,无解(B只能增加偶数个)
//3.T串末尾A比S串多,无解(末尾A无法增加,只能减少)
//否则(两串B数目奇偶相同):
//4.T串B多,S串末尾A多,必有解
//5.两串B相同,S串末尾A比T串多的数目为3的倍数时有有解
//6.两串A相同,S串有B即可。
//7.其他无解
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = ;
char S[maxn], T[maxn];
//记录前i个字符中B+C的前缀和;记录以i结尾的连续A的个数
int Sa[maxn], Sb[maxn];
int Ta[maxn], Tb[maxn];
int main()
{
int n;
scanf("%s%s%d", S+, T+,&n);
int len1 = strlen(S+), len2 = strlen(T+);
for (int i = ; i <= len1; i++)
{
if (S[i] == 'A') Sa[i] = Sa[i - ] + ;
Sb[i] = (S[i] == 'A') ? Sb[i - ] : Sb[i - ] + ;
}
for (int i = ; i <= len2; i++)
{
if (T[i] == 'A') Ta[i] = Ta[i - ] + ;
Tb[i] = (T[i] == 'A') ? Tb[i - ] : Tb[i - ] + ;
}
while (n--)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int len1 = b - a + , len2 = d - c + ;
int t_sa = min(len1, Sa[b]), t_sb = Sb[b] - Sb[a - ];
int t_ta = min(len2, Ta[d]), t_tb = Tb[d] - Tb[c - ];
//当T串B少,当两串B数目奇偶数不同,T串末尾A比S串多,无解
if (t_sb > t_tb || (t_sb % ) != (t_tb % ) || t_ta > t_sa) printf("");
else if(t_tb>t_sb&&t_sa>t_ta)//隐含两串B数目奇偶相同
{//T串B多,S串A多(多出的A可以转化为BB,前面非连续的A始终可以转化)
printf("");
}
else if (t_tb == t_sb)
{//此时只有Sa-Ta多出的a的数目为3的倍数才能转化
if ((t_sa - t_ta) % == ) printf("");
else printf("");
}
else if (t_ta == t_sa)
{//此时只要sb的数目大于0
if (t_sb) printf("");
else printf("");
}
else printf("");
}
printf("\n");
return ;
}
A2. Protect Sheep
题意:有一个牧场,‘S’为羊,现在需要放置‘D’狗,防止羊‘S’被狼‘W’通过上下左右吃到。任意一种方案即可,放置狗的数目不限
思路:简单DFS
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
char MP[maxn][maxn];
bool Find[maxn][maxn];
int dx[] = { ,,,- };
int dy[] = { ,-,, };
int R, C;
bool flag;
void DFS(int ux,int uy)
{
for (int i = ; i < ; i++)
{
if (!flag) return;
int tx = ux + dx[i], ty = uy + dy[i];
if (tx >= R || tx < || ty >= C || ty < ) continue;
if (MP[tx][ty] == 'W')
{
flag = false;
return;
}
else if (MP[tx][ty] == 'S'&&!Find[tx][ty])
{
Find[tx][ty] = true;
DFS(tx, ty);
}
else if (MP[tx][ty] == '.') MP[tx][ty] = 'D';
}
}
int main()
{
scanf("%d%d", &R, &C);
flag = true;
for (int i = ; i < R; i++) scanf("%s", MP + i);
for (int i = ; i < R; i++)
{
for (int j = ; j < C; j++)
{
if (MP[i][j] == 'S' && !Find[i][j])
{
Find[i][j] = true;
DFS(i, j);
}
}
}
if (flag)
{
printf("Yes\n");
for (int i = ; i < R; i++) printf("%s\n", MP + i);
}
else printf("No\n");
return ;
}
VK Cup 2018 - Round 1+Codeforces Round #470的更多相关文章
- Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)A. Protect Sheep
http://codeforces.com/contest/948/problem/A A. Protect Sheep Bob is a farmer. He has a large pastu ...
- Codeforces Round #470 (rated, Div. 1, based on VK Cup 2018 Round 1) 923D 947D 948E D. Picking Strings
题: OvO http://codeforces.com/contest/947/problem/D 923D 947D 948E 解: 记要改变的串为 P1 ,记目标串为 P2 由变化规则可得: ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C.Producing Snow
题目链接 题意 每天有体积为Vi的一堆雪,所有存在的雪每天都会融化Ti体积,求出每天具体融化的雪的体积数. 分析 对于第i天的雪堆,不妨假设其从一开始就存在,那么它的初始体积就为V[i]+T[1. ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)
A. Protect Sheep time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport
Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try ...
- D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) F 构造
http://codeforces.com/contest/967/problem/F 题目大意: 有n个点,n*(n-1)/2条边的无向图,其中有m条路目前开启(即能走),剩下的都是关闭状态 定义: ...
随机推荐
- php获取文件后缀的9种方法
获取文件后缀的9种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- mac 干掉Dashboard
打开终端,输入下面的命令: defaults write com.apple.dashboard mcx-disabled -boolean YES 然后再重启一下 Dock,在终端输入 kill ...
- MAMP下配置虚拟主机域名
第一步:修改虚拟主机地址: /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf 第二步:
- iOS开发:iPhone6、6 plus适配
本文转载至 http://jingyan.baidu.com/article/8cdccae97a5c2b315413cda9.html 1 2 3 4 5 6 7 分步阅读 随着苹果公司持续推出新产 ...
- python 之 多进程
阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiprocessingpython中的多线程 ...
- because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'script-src' was not explicitly set, so 'default-s
html文件 修改成如下:<meta http-equiv="Content-Security-Policy" content="default-src *; st ...
- 【BZOJ4592】[Shoi2015]脑洞治疗仪 线段树
[BZOJ4592][Shoi2015]脑洞治疗仪 Description 曾经发明了自动刷题机的发明家SHTSC又公开了他的新发明:脑洞治疗仪--一种可以治疗他因为发明而日益增大的脑洞的神秘装置. ...
- 锚点链接 阻止a标签跳转
参考 http://blog.csdn.net/awe5566/article/details/22583699 href="#downJacket" 锚点链接 必须写: 但又 ...
- 160704、commons-beanutils.jar常用方法
package com.test.beanutils; import java.lang.reflect.InvocationTargetException;import java.text.Pars ...
- react 坑总结
1.react可以在里面直接更改state的变量 例如: 2.react 数组循环