题目链接:http://codeforces.com/contest/1009

A. Game Shopping 题目:

题意:有n件物品,你又m个钱包,每件物品的价格为ai,每个钱包里的前为bi。你站在第一件物品前,如果你的第一个钱包能购买这件物品,你第一个钱包的钱直接消失(就相当于你是用第一个钱包里的所有钱购买了这件物品,不会找钱),如果无法购买那么就跳到下一件物品,以此类推,问你总共能购买多少件物品。

思路:直接模拟即可。

代码如下:

 #include <bits/stdc++.h>
using namespace std; const int maxn = ;
int n, m, ans;
int a[maxn], b[maxn]; int main() {
cin >>n >>m;
for(int i = ; i < n; i++) {
cin >>a[i];
}
for(int i = ; i < m; i++) {
cin >>b[i];
}
for(int i = , j = ; i < n ; i++) {
if(j >= m) break;
if(b[j] >= a[i]) {
j++;
ans++;
}
}
cout <<ans <<endl;
return ;
}

B. Minimum Ternary String  题目:

题意:给你一个只含012的串,0可以和1交换,1可以和2交换,0不可以和2交换,问进行多次交换后字典序最小的串是啥。

思路:因为1可以和02交换,所以要想字典序最小1必须全在2前面;2不能和0换,所以2和0的相对位置不变。故此题的思路就是将第一个2前的的所有0放在最前面,然后紧跟所有的1,后面的2和0就按照原顺序即可。方法1:暴力。方法2:借用vetctor。

代码如下:

 //方法一
//#include <bits/stdc++.h>
//using namespace std;
//
//const int maxn = 1e5 + 7;
//int a[maxn];
//string s, t;
//
//int main() {
// cin >>s;
// int cnt1 = 0, cnt2 = 0, cnt3 = 0;
// int pos = -1;
// for(int i = 0; i <s.size(); i++) {
// if(s[i] != '0') {
// pos = i;
// break;
// }
// }
// for(int i = s.size() - 1; i >= pos; i--) {
// if(s[i] == '0') {
// cnt1++;
// if(cnt3 > 0) {
// for(int j = 0; j < cnt3; j++) {
// t += '2';
// }
// cnt3 = 0;
// }
// } else if(s[i] == '1') {
// cnt2++;
// } else if(s[i] == '2') {
// cnt3++;
// if(cnt1 > 0) {
// for(int j = 0; j <cnt1; j++) {
// t += '0';
// }
// cnt1 = 0;
// }
// }
// }
// for(int i = 0; i < cnt3; i++) {
// t += '2';
// }
// for(int i = 0; i <cnt2; i++) {
// t += '1';
// }
// for(int i = 0; i < pos; i++) {
// t += '0';
// }
// for(int i = 0; i < cnt1; i++) {
// t += '0';
// }
// reverse(t.begin(), t.end());
// cout <<t <<endl;
// return 0;
//} //方法二
#include <bits/stdc++.h>
using namespace std; string s;
vector<char> v; int main() {
cin >>s;
int cnt = ;
for(int i = ; i < s.size(); i++) {
if(s[i] == '') cnt++;
else v.push_back(s[i]);
}
int flag = ;
for(int i = ; i < v.size(); i++) {
if(v[i] == '' && !flag) {
while(cnt--) {
cout<<"";
}
cout <<"";
flag = ;
} else {
cout <<v[i];
}
}
//可能没有0、1,所以要在这里输出一遍
for(int i = ; i <cnt; i++) {
cout <<"";
}
cout <<endl;
return ;
}

C. Annoying Present 题目:

题意:有n个元素,初始为0,进行m次操作,每次操作给你x和d,你选择一个i,然后对每个元素(记为j,包括i)进行操作ai+x+d*dist(i,j),问最后所有元素的最大平均值是多少。

思路:这个就是个贪心+公式题。这题的贪心方法是当d>0时,i选择最左(最右都可以,因为dist(i,j)是一样的,最后是算平均值,所有加在哪个元素上是没有区别的;当d<0时,选择最中间,此处要将n分奇偶,因为奇数和偶数是有所区别的。当推出这些之后会发现这题就是个等差数列求和,注意会爆int,比赛的时候就是被这个点坑死。ps.赛后这题是被hack人数最多的,本场hack数排第一的大佬就是这题hack了600多人,第一个坑点就是求和不能用double,会有精度损失,第二个坑点就是不要先除,要先求和再除。

代码实现如下:

 #include <bits/stdc++.h>
using namespace std; typedef long long ll;
ll n, m, x, d, ans; int main() {
scanf("%I64d%I64d", &n, &m);
while(m--) {
scanf("%I64d%I64d", &x, &d);
ans += x * n;
if(d > ) {
ans += (n - ) * n / * d;
} else {
if(n & ) ans += (n / + ) * (n / ) * d;
else ans += (n / ) * (n / ) * d;
}
}
printf("%.8f\n", ans * 1.0 / n);
return ;
}

D. Relatively Prime Graph 题目:

题意:构造一个只有n个点m条边的联通图,且相邻节点的编号要互质。

思路:当m<n-1时,由图联通知至少需要n-1条边,因此此处为impossible。1与所有的数都互质,所以此处保证了联通性。因为只需要m条边,由欧拉函数可以证得此题暴力的复杂度是稍大于O(m),我只会估算,具体证明暂时不会……因此此题可以暴力水过。

代码如下:

 #include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = 1e5 + ;
int n, m, t; struct node {
int x, y;
}edge[maxn]; int main() {
cin >>n >>m;
if(n - > m) {
puts("Impossible");
return ;
}
int flag = ;
for(int i = ; i <= n; i++) {
for(int j = i + ; j <= n; j++) {
if( __gcd(i, j) == ) {
edge[t].x = i;
edge[t].y = j;
t++;
if(t >= m) {
flag = ;
break;
}
}
}
if(flag) break;
}
if(flag) {
puts("Possible");
for(int i = ; i < m; i++) {
cout <<edge[i].x <<" " <<edge[i].y <<endl;
}
} else {
puts("Impossible");
}
return ;
}

赛后补题

E. Intercity Travelling 题目:

题意:Leha从数轴的0到n,中间的整数点都有可能有休息点也可能没有休息点。当你连续坐k站时,每两站间的疲劳值为a1,a2……ak,如果第k站有休息点,那么你可以在此处休息,然后接下来的站点的疲劳值又从a1开始,否则继续为ak+1。题目给你n和ai,每个站点有休息点的概率都是1/2,问你期望值*2^(n-1)的值。

思路:我们知道从0到1的疲劳值一定为a1,1到2的疲劳值为a1(1/2),a2(1/2),括号里面为概率,当1有休息点时为a1,否则为a2;2到3的疲劳值为a1(1/2),a2(1/4),a3(1/4)……以此类推得到下图:

然后将他们加起来乘以2^(n-1)即可。ps.别用cin,cout,会T9,别问我怎么知道的!

代码实现如下:

 #include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int mod = ;
const int maxn = 1e6 + ;
int n;
ll ans;
ll a[maxn], p[maxn]; int main() {
scanf("%d", &n);
p[] = ;
for(int i = ; i <= n; i++) {
p[i] = p[i-] * % mod;
}
for(int i = ; i <= n; i++) {
scanf("%I64d", &a[i]);
}
ans = a[n];
for(int i = ; i < n; i++) {
ans = (ans + (a[i] * (p[n - i] + p[n - i - ] * (n - i) % mod) % mod) % mod) % mod;
}
printf("%I64d\n", ans);
return ;
}

Educational Codeforces Round 47 (Rated for Div. 2) 题解的更多相关文章

  1. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  2. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  3. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

  5. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  6. Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling

    题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...

  7. Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph

    题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...

  8. Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)

    题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...

  9. Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String

    题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...

随机推荐

  1. 关于JavaScript中this的软绑定

    首先,什么是软绑定? 所谓软绑定,是和硬绑定相对应的一个词,在详细解释软绑定之前,我们先来看看硬绑定.在JavaScript中,this的绑定是动态的,在函数被调用的时候绑定,它指向什么完全取决于函数 ...

  2. sorted 返回字典的所有键

  3. const,static,volatile关键字的作用

    const关键字: 1.欲阻止一个变量被改变,可使用const,在定义该const变量时,需先初始化,以后就没有机会改变他了: 2.对指针而言,可以指定指针本身为const,也可以指定指针所指的数据为 ...

  4. wordpress 页面显示指定分类文章

    首页显示指定分类备份主题文件夹中的 index.php 文件,修改index.php找到如下一行代码:<?php if (have_posts()) : ?>在上面这行代码的前面加上:&l ...

  5. BZOJ 4499: 线性函数

    4499: 线性函数 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 177  Solved: 127[Submit][Status][Discuss] ...

  6. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  7. 【洛谷P1379】八数码难题 状压bfs

    对于这道题来说,每个时刻的状态是整个棋盘所有棋子的位置,即:任何一个棋子位置发生了移动,都会使得状态转移. 因此,需要采取将整个状态作为广搜的搜索对象,进行状态压缩.采用哈希得到每个状态的对应的数值, ...

  8. 编译Uboot——错误记录

    我使用的是ZLG的EasyARM i.MX280A的开发板.官方提供的编译器时arm-fsl-linux-gnueabihf(gcc 4.4.4).自己尝试使用arm-linaro-linux-gnu ...

  9. Django 日志配置按日期滚动

    记录下Django关于日期的配置,以及如何根据日期滚动切割日志的问题. 配置的源码在githun上 https://github.com/blackmatrix7/django-examples/tr ...

  10. POJ1185 状压dp(二进制//三进制)解法

    很显然这是一道状压dp的题目 由于每个最优子结构和前两行有关,一个显而易见的想法是用三维dp[i][j][k]用来记录在第i行下为j状态,i - 1行为k状态时的最大值,然而dp[100][1 < ...