A.Beru-taxi

水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间;

单间循环即可;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 102550
#define PI 4*atan(1.0)
#define mod 100000001
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int main()
{
int sx, sy, n;
double ans = INF;
scanf("%d %d %d", &sx, &sy, &n);
for(int i=; i<=n; i++)
{
int x, y, v;
scanf("%d %d %d", &x, &y, &v);
double d = sqrt((x-sx)*(x-sx)+(y-sy)*(y-sy));
ans = min(ans, d/v);
}
printf("%.7f\n", ans);
return ;
}

B.Interesting drink

 有n个物品,每个物品为ai元,某人每天有x元钱,求每天他能买几种的物品;

排序二分即可;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 102550
#define PI 4*atan(1.0)
#define mod 100000001
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int a[N]; int main()
{
int n, m, num;
while(scanf("%d", &n)!=EOF)
{
for(int i=; i<n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
scanf("%d", &m);
for(int i=; i<m; i++)
{
scanf("%d", &num);
int ans = upper_bound(a, a+n, num)-a;
printf("%d\n", ans);
}
}
return ;
}

C.Hard problem

 简单dp:给你n个字符串,然后问这n个字符串要想变成字典序排列的字符串所需的代价是多少,每个字符串可以倒置,(倒置串 i 的代价是a[i]),或者不变;

可以用dp[i][0]表示前i个字符串已经是按字典序排列的,并且第i个字符串不倒置的最小代价;用dp[i][0]表示前i个字符串已经是按字典序排列的,并且第i个字符串倒置的最小代价;

所以公式很容易写出来;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define oo 1000000000000000
#define N 102550
#define PI 4*atan(1.0)
#define mod 100000001
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int n;
LL a[N], dp[N][]; char s[][N]; void Strrev(char str[])
{
int len = strlen(str);
for(int i=, j=len-; i<j; i++, j--)
swap(str[i], str[j]);
} int main()
{
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%I64d", &a[i]);
dp[i][] = dp[i][] = oo;
} int p = ; dp[][] = ; dp[][] = a[]; for(int i=; i<=n; i++)
{
p = p^;
scanf("%s", s[p]);
if(i == ) continue; strcpy(s[p+], s[p]);
Strrev(s[p+]);
strcpy(s[(p^)+], s[p^]);
Strrev(s[(p^)+]); if(strcmp(s[p], s[p^]) >= )
dp[i][] = min(dp[i][], dp[i-][]);
if(strcmp(s[p], s[(p^)+]) >= )
dp[i][] = min(dp[i][], dp[i-][]);
if(strcmp(s[p+], s[p^]) >= )
dp[i][] = min(dp[i][], dp[i-][] + a[i]);
if( strcmp(s[p+], s[(p^)+]) >= )
dp[i][] = min(dp[i][], dp[i-][] + a[i]);
}
LL ans = min(dp[n][], dp[n][]);
if(ans == oo)ans = -;
printf("%I64d\n", ans);
return ;
}

D.Vasiliy's Multiset

01字典树:有一个里面初始值只有 0 的集合,执行 n 个操作,每个操作有三种可能:

+ x把x添加到集合中去;

- X把x从集合中删去一个;保证集和中一定有x;

? x从集合中找到一个数y,使得x异或y最大化;

对于每个?操作输出对应的最大的值;

01字典树模板:把十进制数转化为二进制数,通过前补零的方法构成32位,然后从前到后依次添加到字典树中去;

在查找时,每次尽量查找当前位相反的数的那个位置, 因为异或是不同为1的;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define oo 1000000000000000
#define N 102550
#define PI 4*atan(1.0)
#define mod 100000001
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; struct node
{
int sum, num;///sum表示有多少个数经过当前这个节点;num为从根节点到叶子节点表示的数;
node *Next[];///指向下一层的节点;
}; void Add(node *head, int x, int cnt)///把x对应的二进制01插入到字典树中去;共32位,前补零形式;
{
node *p = head;
for(int i=; i>=; i--)///这一点不太理解位运算的,但是相当于是把x转换成二进制,然后插入其中;
{
int k = (x>>i)&;
if(p->Next[k] == NULL)///当不存在当前节点时,开创新节点,并连接在对应位置;
{
node *q = new node();
p->Next[k] = q;
}
p = p->Next[k];///接着往下走;
p->sum += cnt;///更新节点经过数;
}
p->num = x;///结束的时候更新叶子节点对应的数;
} int Find(node *head, int x)
{
node *p = head;
for(int i=; i>=; i--)
{
int k = (x>>i)&;
if(p->Next[k^] != NULL && p->Next[k^]->sum > )///尽量找与k不同的;应为异或是不同为1,所以要想最大,就要尽量不同;
p = p->Next[k^];
else if(p->Next[k] != NULL && p->Next[k]->sum > )
p = p->Next[k];
}
return (p->num)^x;///返回对应的最大结果;
} int main()
{
int n, x; char ch; scanf("%d", &n); node *head = new node(); Add(head, , ); for(int i=; i<=n; i++)
{
scanf(" %c %d", &ch, &x);
if(ch == '+')
Add(head, x, );
else if(ch == '-')///删除,相当于更新sum即可;
Add(head, x, -);
else
{
int ans = Find(head, x);
printf("%d\n", ans);
}
}
return ;
}

Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树的更多相关文章

  1. Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #367 (Div. 2) 套题

    吐槽:只能说是上分好场,可惜没打,唉 A:Beru-taxi (水题,取最小值) #include <cstdio> #include <cstring> #include & ...

  3. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  4. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  5. Codeforces Round #214 (Div. 2) c题(dp)

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]

    传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  8. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  9. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

随机推荐

  1. ios clang: error: linker command failed with exit code 1 (use -v to see invocation)解决方法

    当xcode编译时出现这个错误,一般是你的编译源码中存在重复的源码 解决方法:"Build Phases" -> "Compile Sources" 去删 ...

  2. Google Code Jam 2010 Round 1B Problem A. File Fix-it

    https://code.google.com/codejam/contest/635101/dashboard#s=p0   Problem On Unix computers, data is s ...

  3. Thrift入门及Java实例演示

    目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...

  4. 【C语言】14-返回指针的函数与指向函数的指针

    前言 前面我们花了接近3个章节学习指针,应该都感受到指针的强大了吧.指针可以根据地址直接操作内存中的数据,使用得当的话,不仅能使代码量变少,还能优化内存管理.提升程序性能.关于指针的内容还非常多,比如 ...

  5. 4.用文本编辑器输入课堂上练习的Hello.java,并在JDK环境下编译和运行。请将程序编译、运行的结果截图,填入下框中。

    一开始报错是因为在文本框了的:用的是中文下的,应该用英文下的;

  6. uva10098 Generating Fast, Sorted Permutation

    #include"iostream"#include"stdio.h"#include"string.h"#include"alg ...

  7. HTML练习----注册界面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. html5_d登陆界面_注册界面

    <!DOCTYPE html><html><head><script type="text/javascript">function ...

  9. 移动Web应用开发入门指南——交互篇

    交互篇 从PC到移动端,视觉和交互是用户能直接感受到的差异.在视觉篇中已经提到,移动设备的物理属性一部分影响到视觉,另外一些部分将影响到交互.那么,移动设备影响交互的物理属性都有哪些变化呢?对于这个问 ...

  10. Sqlmap注入技巧集锦

    当我们注射的时候,判断注入 http://site/script?id=10 http://site/script?id=11-1 # 相当于 id=10 http://site/script?id= ...