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. HDU 1533 & KM模板

    题意 求二分图最小完备匹配. SOL 建个图那么方便的事情是吧...然后边权都是正的(好像根边权也没什么关系),既然要求最小那么把边权取个相反数跑个KM就好了.. CODE: /*========== ...

  2. ios7隐藏系统底部导航

    ios7隐藏系统底部导航 minimal-ui <meta id="viewport" name="viewport" content="wid ...

  3. TYVJ P1091 等差数列 Label:dp

    背景 广东汕头聿怀初中 Train#3 Problem 3 描述 等差数列的定义是一个数列S,它满足了(S[i]-S[i-1]) = d (i>1).显然的一个单独的数字或者两个数字也可以形成一 ...

  4. POJ 2891 Strange Way to Express Integers(中国剩余定理)

    题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...

  5. wc2016总结

    因为我太弱了,高一才第一次来wc. 前几天讲课,被各种小学微积分和初中高等代数虐,简直naive.只好自己做做bzoj,想着练练模板之类的东西. 考试当天自觉状态不错,翻开试题感觉各种神奇(这难道是串 ...

  6. git 远程版本库

    [root@localhost workspace]# cd repos/ [root@localhost repos]# ll 总用量 drwxr-xr-x root root 12月 : hell ...

  7. ztree学习之异步加载节点(一)

    ztreedemo.jsp: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  8. The declared package does not match the expected package

    The declared package does not match the expected package. 1.选中项目右击选择Build Path-->再选择Configure Bui ...

  9. 将java的class文件放到一个指定文件夹下

    用javac执行java文件时,要把java文件的class文件放到指定文件夹下,注意文件夹要创建好,执行javac -d 文件夹 ***.java 如图: 在class文件夹下就出现了L的class ...

  10. 【转】用JIRA管理你的项目

    原文地址:http://snowolf.iteye.com/blog/875529