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. cocos2d ARCH_OPTIMAL_PARTICLE_SYSTEM这个未定义的问题

    在新版本的cocos2d中ARCH_OPTIMAL_PARTICLE_SYSTEM已经被移除由 CCParticleSystemQuad取代 CCParticleSystem *test = [ARC ...

  2. CC150 - 11.3

    Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...

  3. UOJ#77. A+B Problem

    题目名称是吸引你点进来的. 从前有个 n 个方格排成一行,从左至右依此编号为 1,2,⋯,n. 有一天思考熊想给这 n 个方格染上黑白两色. 第 i 个方格上有 6 个属性:ai,bi,wi,li,r ...

  4. COJ977 WZJ的数据结构(负二十三)

    试题描述 输入一个字符串S,输出S的最长连续回文子串长度. 输入 输入一个字符串S. 输出 输出S的最长连续回文子串长度 输入示例 abacbbc 输出示例 4 其他说明 1<=|S|<= ...

  5. 怎么通过URL访问到服务器上的物理文件

    目的 :搞清楚在jsp页面中如何引用static目录结构中的静态资源 一.URL URL的全称是统一资源定位符,俗称网页地址(网址),如同在网络上的门牌,是因特网上标准的资源的地址(Address). ...

  6. java 枚举类型分析

    最近做android开发,需要用到枚举值,这样可以连续赋值,我按之前c++那样书写,如下所示: public enum ColorSelect { RED_BAGE = 0, GREEN_BAGE, ...

  7. iOS开发之正则表达式

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达 ...

  8. HTML&CSS----练习做网页

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

  9. 结合计划任务每天从Symantec官网下载离线病毒库

    #三种方法,由初级到高级 $numbers = 1..40 | Foreach {"{0:D3}" -f $_} #将数字类型格式化并转换为字符串类型使用-f字符串操作符 ForE ...

  10. HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...