Codeforces Round #409 (Div. 2)

来源:互联网 发布:合并分区软件 编辑:程序博客网 时间:2024/06/05 18:32

A - Vicious Keyboard(water)

题意:

  给你一串字符(不超过100个),你最多可以把一个字符改成v或者k,问你这串字符中,"vk"最多有几个。

思路:

  因为数据范围非常小,所以为了保证正确率,枚举原串,枚举原串每个字符被更改为v,枚举原串每个字符被更改为k,一共是1+n+n个串。每次O(n)扫出数量,n^2解决。

#include <bits/stdc++.h>using namespace std;const int maxn = 1e5 + 5;const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int>pii;int vis[105];int main(){    string s;    cin >> s;    int len = s.size();    int ans = 0;    for(int i = 0; i < len - 1; i++)    {        if(s[i] == 'V' && s[i + 1] == 'K')  ans++;    }    for(int i = 0; i < len; i++)    {        char ch = s[i];        s[i] = 'V';        int temp = 0;        for(int j = 0; j < len - 1; j++)        {            if(s[j] == 'V' && s[j + 1] == 'K')  temp++;        }        s[i] = ch;        ans = max(ans, temp);    }    for(int i = 0; i < len; i++)    {        char ch = s[i];        s[i] = 'K';        int temp = 0;        for(int j = 0; j < len - 1; j++)        {            if(s[j] == 'V' && s[j + 1] == 'K')  temp++;        }        s[i] = ch;        ans = max(ans, temp);    }    cout <<ans <<endl;    return 0;}



B - Valued Keys(构造)

题意:

  定义一种计算方式f(x,y)=z,z[i] = min(x[i],y[i])。

  给你一个串x,一个串z,求出一个符合条件的y。

思路:

  。。。这场最水的题,窝都不敢相信这么简单,犹豫了几分钟才写。

  那只要判断一下x[i]>z[i]恒成立,然后y=z不就好了。。

#include <bits/stdc++.h>using namespace std;const int maxn = 1e5 + 5;const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int>pii;string x, y, z;bool solve(){    y = "";    cin >> x >> z;    int len = x.size();    for(int i = 0; i < len; i++)    {        if(z[i] > x[i]) return false;        y += z[i];    }    return true;}int main(){    if(solve())    {        cout << y <<endl;    }    else puts("-1");    return 0;}



C - Voltage Keepsake(二分)

题意:

  告诉你有n个设备,每个设备单位时间耗电量为ai,自带电量bi。你有一个可以随时切换的充电器,充电效果为p每个单位时间。问你每个设备均有电的状态最长能保持多久。

思路:

  简单的二分答案,然后judge一下是不是,delta = p - sigma{ai}且delta >= 0,即整个系统的耗电量均能被补充,若是,则INF。

  显然,如果不是INF, 那么,delta >= 1,所以至少每个单位时间净耗电为1个单位,所以最长长度应该是一开始1e5个设备满电,1e10的电量,这种情况下,时间为1e10。

  WA点,右上界开的太小了。

 

#include <bits/stdc++.h>using namespace std;const int maxn = 100000 + 5;const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int>pii;LL a[maxn], b[maxn];LL n, p;LL cost, store;bool judge(double time){    double add = 0;    for(int i = 0; i < n; i++)    {        double co = time * a[i];        if(b[i] > co) continue;        else        {            add += (co - b[i]);        }    }    return time * p > add;}int main(){    cin >> n >> p;    cost = store = 0;    for(int i = 0; i < n; i++)    {        cin >> a[i] >> b[i];        cost += a[i];        store += b[i];    }    if(cost <= p)    {        puts("-1");    }    else    {        double lb = 0, rb = 1e11;        for(int i = 0; i < 100; i++)        {            double mid = (lb + rb) / 2;            if(judge(mid))  lb = mid;            else rb = mid;        }        printf("%.7f\n", lb);    }    return 0;}

D - Volatile Kite(叉积算三角形面积)

题意:

  给你一个凸n边形,每个顶点可以在二维平面任意移动不超过D的距离,问你D最大是多少,使得不论怎么移动,仍然为凸n边形。

思路:

  显然这个思路可以迅速得到:三个三个相邻点去考虑,答案就是min{顶点(x2,y2)到由(x1,y1),(x3,y3)的直线的距离/2},高中的数学知识,是不是很容易证三个定点的时候,最大的D就是这个,那么这题就解决了。

  但是!!!WA点!!!用海伦公式去算的时候!!!误差太大了,所以是用叉积计算三角形的面积orz。

#include <bits/stdc++.h>using namespace std;const int maxn = 1e3 + 5;typedef long long LL;const double eps = 1e-12;LL x[maxn], y[maxn];int n;double calc(int a, int b){    return sqrt((double)(x[a] - x[b]) * (x[a] - x[b]) + (double)(y[a] - y[b]) * (y[a] - y[b]));}double solve(int a, int b, int c){    LL x1 = x[a], x2 = x[b], x3 = x[c];    LL y1 = y[a], y2 = y[b], y3 = y[c];    double S = fabs((x2 - x1) * (y2 - y3) - (x2 - x3) * (y2 - y1)) / 2;    return S / calc(a, c);}int main(){    scanf("%d", &n);    for(int i = 0; i < n; i++)    {        scanf("%I64d%I64d", &x[i], &y[i]);    }    double ans = 1e12;    for(int i = 0; i < n; i++)    {        double temp = solve(i, (i + 1) % n, (i + 2) % n);        ans = min(ans, temp);    }    printf("%.12f\n", ans);    return 0;}



E - Vulnerable Kerbals













0 0