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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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  由变化规则可得: ...

  5. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C.Producing Snow

    题目链接  题意  每天有体积为Vi的一堆雪,所有存在的雪每天都会融化Ti体积,求出每天具体融化的雪的体积数. 分析 对于第i天的雪堆,不妨假设其从一开始就存在,那么它的初始体积就为V[i]+T[1. ...

  6. 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 ...

  7. 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 ...

  8. 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( ...

  9. 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条路目前开启(即能走),剩下的都是关闭状态 定义: ...

随机推荐

  1. 如何Vue-cli开始使用在Vue.js项目中启动TDD(测试驱动开发)

    通常,使用测试驱动开发(TDD)最困难的部分是开始.你必须下载带有奇怪依赖项的软件包,让测试套件与你的构建系统协同工作,然后你必须弄清楚如何编写一个测试!难怪这么多的开发者在你提起它的时候就开始跑开了 ...

  2. deep learning+ Depth Estimation

    Depth estimation/stereo matching/optical flow @CVPR 2017 Unsupervised Learning of Depth and Ego-Moti ...

  3. 目标跟踪之卡尔曼滤波---理解Kalman滤波的使用

    http://www.cnblogs.com/jcchen1987/p/4371439.html

  4. 【BZOJ】3399: [Usaco2009 Mar]Sand Castle城堡(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3399 贪心就是将两组排序,然后直接模拟即可.. 如果我们用a去匹配一个绝对值和它差不多的值,那么去匹 ...

  5. 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...

  6. 卡友友刷MPOS注册开通流程!

    1.下载友刷APP:打开微信扫描机器背面二维码—点击右上角游览器打开 2. 注册-身份证认证-结算卡绑定:用本人手机号注册完成后,顺着进行身份认证.以及储蓄结算卡绑定.具体看下图: 3.绑定机器:选择 ...

  7. (转)c/c++内存对齐问题

    struct/class/unio内存对齐: http://blog.csdn.net/microsues/article/details/6140329 class函数占用字节数问题: http:/ ...

  8. npm install 不自动生成 package-lock.json文件

    package-lock.json这个文件的作用就不详细说明了 有需要的可以参考 :https://www.cnblogs.com/cangqinglang/p/8336754.html 网上都说 n ...

  9. Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法

    归并排序能够有两种思路----top-down 和 bottom-up top-down: 递归实现,将数组分成两半.分别处理.再合并. 伪代码例如以下: split ( A[], l, r) { i ...

  10. JMETER 不同线程组 变量值 的参数传递(转)

    线程组 1   在线程组1中使用__setProperty函数设置jmeter属性值(此值为全局变量值),将所需变量值如${token}设置为jmeter属性值,即newtoken,示例: 1.添加- ...