Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树
A
/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
using namespace std;
typedef long long ll;
const long long mod = 1e9 + ;
const int N = 2e5 + ;
int main()
{
ll n;
cin >> n;
if (n == )
{
cout << << endl;
return ;
}
n++;
if (n % == )
{
cout << n / << endl;
}
else
{
cout << n << endl;
}
}
B
/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
using namespace std;
typedef long long ll;
const long long mod = 1e9 + ;
const int N = 2e5 + ;
string a, b, c;
string str[];
int num[][];
int ansermaxn[];
int main()
{
ll n;
cin >> n;
for (int i = ; i <= ; i++)
{
cin >> str[i];
}
int aim = ;
int ansermax = -;
int ansnow;
for (int i = ; i <= ; i++)
{
for (int j = ; j < str[i].size(); j++)
{
num[i][str[i][j]]++;
}
}
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (num[i][j] + n <= str[i].size())
{
ansnow = num[i][j] + n; if (ansnow > ansermax)
{
aim = i;
ansermax = ansnow;
}
}
else
{
if (num[i][j] == str[i].size())
{
if (n == )
{
ansnow = str[i].size() - ;
}
else
{
ansnow = str[i].size();
}
}
else
{
ansnow = str[i].size();
}
if (ansnow > ansermax)
{
aim = i;
ansermax = ansnow;
}
}
ansermaxn[i] = max(ansermaxn[i], ansnow);
}
}
int cnt = ;
for (int i = ; i <= ; i++)
{
//cout << ansermaxn[i] << endl;
if (ansermaxn[i] == ansermax)
{
cnt++;
}
}
if (cnt > )
{
cout << "Draw" << endl;
return ;
}
if (aim == )
{
cout << "Kuro" << endl;
}
else if (aim == )
{
cout << "Shiro" << endl;
}
else
{
cout << "Katie" << endl;
}
}
C
/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
using namespace std;
typedef long long ll;
const long long mod = 1e9 + ;
const int N = 3e5 + ;
vector<int> gra[N];
ll anser = ;
ll reduce = ;
ll number1 = ;
ll number2 = ;
ll number = ;
int flag = ;
void dfs(int x, int pre, int aim, int flag)
{
if (flag)
{
number++;
}
int len = gra[x].size();
for (int i = ; i < len; i++)
{
int to = gra[x][i];
if (to == pre)
{
continue;
}
if (to == aim)
{
dfs(to, x, aim, );
}
else
{
dfs(to, x, aim, flag);
}
}
}
int main()
{
int n;
cin >> n;
int x, y;
int u, v;
cin >> x >> y;
for (int i = ; i <= n - ; i++)
{
scanf("%d %d", &u, &v);
gra[u].pb(v);
gra[v].pb(u);
}
anser = 1LL * (n - ) * n;
dfs(x, -, y, );
number1 = number;
number = ;
dfs(y, -, x, );
number2 = number;
anser -= 1LL * number2 * number1;
cout << anser << endl;
}
D
一开始给你一个空的集合
有两种操作:
①集合中加入一个数字X
②询问给你三个数字Xi Ki Si 查找集合中是否存在数字V满足 ① Ki是Xi和V的因子 ② V<=Si-Ki 当如果有多个满足条件的数输出Xi Xor V最大的那个V
解:
01字典树暴力操作
直接每次insert一个未出现过的数的时候 暴力Insert到每个它的因子里面去
复杂度为:1e5*ln(1e5)*位数(最多为18)=1e7
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
vector<int>G[maxn];
int vis[maxn];
void read(int &x)
{
x = ;
char c = getchar();
while (c > '' || c < '')
{
c = getchar();
}
while (c >= '' && c <= '')
{
x = (x << ) + (x << ) + c - '', c = getchar();
}
}
struct Trie //01字典树指针版
{
struct node
{
int Min, val;
node *ch[];
node()
{
Min = maxn; //维护一个最小值
ch[] = ch[] = NULL;
}
}*rt[maxn];
void init()
{
for (int i = ; i < maxn; i++)
for (int j = i; j < maxn; j += i) //复杂度1e5*ln1e5=1e6
{
G[j].push_back(i); //把一个数的因子全部push进去
}
for (int i = ; i < maxn; i++)
{
rt[i] = new node; //每个数赋予一个新指针
}
}
void insert(int x)
{
int Len = G[x].size(); //插入一个数
for (int i = ; i < Len; i++)
{
node *cur = rt[G[x][i]]; //暴力枚举insert到这个数的每个因子里面取
cur->Min = min(cur->Min, x); //维护最小值
for (int j = ; j >= ; j--) //争取取每位^1的使之异或值最大
{
if (cur->ch[x >> j & ] == NULL) //如果没有的话创造一个新节点
{
cur->ch[x >> j & ] = new node;
}
cur = cur->ch[x >> j & ];
cur->Min = min(cur->Min, x); //每个节点维护最小值
}
cur->val = x; //最后一个节点的值为x 最后取答案用
}
}
int query(int x, int k, int s)
{
if (x % k != ) //如果k是x和v的gcd的因子的话 x和v都是k的倍数
{
return -;
}
node *cur = rt[k]; //
if (cur->Min > s - x) //如果最小的都不能满足的话 不存在
{
return -;
}
for (int i = ; i >= ; i--)
{
int tb = x >> i & ;
if (cur->ch[tb ^ ] != NULL && cur->ch[tb ^ ]->Min <= s - x) //如果^1的值满足条件
{
cur = cur->ch[tb ^ ];
}
else
{
cur = cur->ch[tb];
}
}
return cur->val; //返回使之异或值最大的数列值
}
} T;
int main()
{
int N, i, j, opt, x, k, s;
T.init();
read(N);
while (N--)
{
read(opt);
if (opt == )
{
read(x);
if (!vis[x])
{
vis[x] = , T.insert(x);
}
}
else
{
read(x);
read(k);
read(s);
printf("%d\n", T.query(x, k, s));
}
}
return ;
}
Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树的更多相关文章
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- python 找出字符串中出现次数最多的字母
# 请大家找出s=”aabbccddxxxxffff”中 出现次数最多的字母 # 第一种方法,字典方式: s="aabbccddxxxxffff" count ={} for i ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- Vasiliy's Multiset CodeForces -706D || 01字典树模板
就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 #include<cstdio> #include<algorithm> using namespace st ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- Codeforces 948D Perfect Security 【01字典树】
<题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...
- 数据结构&字符串:01字典树
利用01字典树查询最大异或值 01字典树的是只含有0和1两种字符的字典树,在使用它的时候,把若干数字转成二进制后插入其中 在查询树中的哪个数字和给定数字有最大异或值的时候,从根开始贪心查询就ok了 H ...
随机推荐
- leetcode171 Excel列表序列号
/** 可看做26进制到10进制转换问题:v=26*v+s[i]-'A'; **/ class Solution { public: int titleToNumber(string s) { ; f ...
- 手动清空微信PC客户端数据
微信PC客户端,用久了之后,会产生大量数据,包括聊天记录.聊天图片.视频等等,非常占存储空间,除非很重要的聊天记录或文件,建议额外保存,其他的可以手动删掉就好,可以节省存储空间. 1.找到[C:\Us ...
- Activity的onSaveinstaceState()保存fragment状态
Activity的onCreat方法: @Override protected void onCreate(Bundle savedInstanceState) { FragmentManager s ...
- Ubuntu 16.04下安装sublime Text的插件
Sublime Text是什么: 它是一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制,用她来写代码,绝对是一种享受.相比于难于上手的Vim,浮肿沉重 ...
- web开发(三) 会话机制,Cookie和Session详解
在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6422391.html>,在此仅供学习参考之用. 一.会话 ...
- notepad++ 正则表达式(记录)
删除操作notepad++去掉行尾空格或逗号查找目标:\s+$ (或,+$)替换为空Note: 以换行符结尾表示是$\r\n,而不是\r\n$ notepad++删除文本文件里面的空白行查找目标:^[ ...
- Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use.
Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use. The ...
- java:Oracle(级联删除,左右内外交叉自然连接,子查询,all,any,in)
1.级联删除: -- 级联删除:裁员,公司倒闭 -- 级联删除(cascade),设置为null(setnull),放任不管(No action) -- cascade:(以一对多为例)如果删除多的一 ...
- LeetCode.977-排序数组的平方(Squares of a Sorted Array)
这是悦乐书的第369次更新,第397篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第231题(顺位题号是977).给定一个整数数组A按有序递增顺序排序,返回每个数字的平方, ...
- USACO2.1 Hamming Codes【枚举+二进制处理+输出格式+题意理解】
这道题加了2个看起来奇奇怪怪的$tag$ 1.输出格式:不得不说这个格式输出很恶心,很像$UVA$的风格,细节稍微处理不好就会出错. 因为这个还$WA$了一次: ,m=n; ) { ;i<=t+ ...