Codeforces 864 C Bus 思维

  题目链接: http://codeforces.com/problemset/problem/864/C

  题目描述: 输入a, b, f, k , 一段长度为a的路来回走, 中间f的地方有一个加油站, 油罐的容量为b, 问想要走b次这条路至少需要加多少次油  

  解题思路: 由于K <= 1e4, 所以将每次需要走路的连续序列构造出来再去遍历一遍这个序列看啥时候需要加油就可以了

  代码:

#include <iostream>
#include <cstdio>
#include <map>
#include <iterator>
#include <string>
using namespace std;

const int maxn = 1e4+100;
int dis[maxn];

int main() {
    int a, b, f, k;
    cin >> a >> b >> f >> k;
    int temp1 = f;
    int temp2 = a-f;
    temp1 *= 2, temp2 *= 2;
    dis[1] = f;
    int flag = 0;
    if( k & 1 ) flag = 1;
    if( flag == 1 ) {
        dis[k+1] = a-f;
        int temp = 1;
        for( int i = 2; i <= k; i++ ) {
            if( temp==1 ) {
                dis[i] = temp2;
            }
            else {
                dis[i] = temp1;
            }
            temp = -temp;
        }
    }
    else {
        dis[k+1] = f;
        int temp = 1;
        for( int i = 2; i <= k; i++ ) {
            if( temp==1 ) {
                dis[i] = temp2;
            }
            else {
                dis[i] = temp1;
            }
            temp = -temp;
        }
    }
//    for( int i = 1; i <= k+1; i++ ) {
//        cout << dis[i] << " ";
//    }
//    cout << endl;

    int ans = 0;
    flag = 0;
    int temp = 0;
    for( int i = 1; i <= k+1; i++ ) {
        if( dis[i] > b ) {
            flag = 1;
            break;
        }
        if( temp + dis[i] <= b ) {
            temp += dis[i];
        }
        else {
            temp = dis[i];
            ans ++;
        }
    }
    if( flag ) {
        cout << "-1" << endl;
    }
    else {
        cout << ans << endl;
    }
    return 0;
}

  思考: 自己首先一开始想当然的理解了题意, 造成的后果就是样例怎么也推不出来, 还以为是题错了就咸鱼了一个点儿........后来自己又重新把题画了一遍才发现是自己傻逼, 然后才A掉,妈的智障

时间: 2024-08-27 22:32:26

Codeforces 864 C Bus 思维的相关文章

CodeForces 1102C-简单的思维题

题目链接:http://codeforces.com/problemset/problem/1102/C C. Doors Breaking and Repairing time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are policeman and you are playing a game with Slavik

CodeForces 789D 欧拉路径计数,思维

CodeForces 789D 题意:n个点m条边的无向图,求经过其中m-2条边两次,剩下2条边一次的方案数有几种,如果剩下两条边的集合一样算同一种. tags: 选出两条边,其它m-2条边假想复制成两条,这样就是要求欧拉路径是否存在,即奇点个数是否为0或2. 所以该怎么选这两条边呢? 先把边分为自环边和普通边. 1.选取两条不相邻普通边,图中存在4个奇点,不满足欧拉路径条件: 2.选取两条相邻普通边,图中存在2个奇点,满足欧拉路径条件: 3.选取一条普通边一条自环,图中存在2个奇点,满足欧拉路

CodeForces 789E bfs建模,思维

CodeForces 789E 题意:有k种可乐,每种的测试为ai/1000. 要你合成一种浓度为n/1000的可乐,问最小要杯可乐,每种可乐可重复取. tags:  要注意到浓度绝不会超过1000/1000. 假设选取m杯可乐,则 (a1+a2+......+am) / m = n,变换一下为(a1-n)+(a2-n)+......+(am-n) = 0.即要选 m杯可乐,其浓度减 n之和为0.而浓度不超过1000,故(a1-n)+(a2-n)+....+(as-n)的和肯定在 -1000~1

2017-03-16 Codeforces 453A 概率期望,思维 UOJ 228(待补)

Codeforces 453A   A. Little Pony and Expected Maximum 题意:一个m面质地均匀的骰子,每面出现的概率都是独立的1/m, 你需要投掷n次,其结果是这n次出现的最大点数.问投掷n次骰子的结果的期望值是多少,要求相对误差或绝对误差不超过1e-4. tags:枚举骰子出现最大值i,计算出最大值为i时的概率,就得到了答案. 最大值为i的概率=(i/m)^n-((i-1)/m)^n. #include<bits/stdc++.h> using names

Codeforces Round #399 B 思维 C 模拟 D 概率dp E SG博弈

Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)B. Code For 1 题意:数n,不断拆分为 n/2, n&1, n/2,直到都为0或1.求区间[l, r]有多少个1. tags:画一画很容易看出来,类似dfs中序遍历. //#399 B #include<bits/stdc++.h> using namespace std; #pragma comment(linker, &quo

codeforces 497B Tennis Game 思维+二分~

链接:http://codeforces.com/problemset/problem/497/B 这种题考察的是基本功..像我这种基本功为0的喳喳只能卡在这里了... 思路:n范围为10^5, 必须找出nlogn的算法才行.枚举t,然后用二分找出解..巧妙~~ 好题赞一个,虽然不会做~~~ 代码: /* *********************************************** Author :ltwy Created Time :2014年12月18日 星期四 16时28

Maximal GCD CodeForces - 803C (数论+思维优化)

C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1,?a2,?...

CodeForces 625B 字符串模拟+思维

题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环strstr来做题 然而改第一个字母不行 因为有可能重叠 比如在lll之中找ll 改了第一个还能找出来 但是其实只改一个就可以了 之后又想是不是能改最后一个 但是用strstr不会... 所以最后想出了暴力扫一遍的神奇解法..因为最多 a是五次方 b是30 最多是3乘十的六次方..结果46ms就好了

codeforces A. Bayan Bus(简单模拟)

1 #include <queue> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 #define N 20000 8 using namespace std; 9 10 int num[5][15]; 11 12 int main() 13 { 14 int n;