Codeforces Round #404 (Div. 2)A,B,C

A. Anton and Polyhedrons

题目链接:http://codeforces.com/contest/785/problem/A

智障水题

实现代码:

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    int ans=0,t;
    cin>>t;
    while(t--){
        cin>>s;
        if(s=="Tetrahedron")
            ans+=4;
        else if(s=="Cube")
            ans+=6;
        else if(s=="Octahedron")
            ans+=8;
        else if(s=="Dodecahedron")
            ans+=12;
        else ans+=20;
    }
    cout<<ans<<endl;
}

B. Anton and Classes

链接:http://codeforces.com/contest/785/problem/B

思路:想要最大的距离两种情况:

1.a数组在前,b在后,此时最大长度为b最大的左边界-a最小的右边界.

2.b数组在前,a在后,此时最大长度为a最大的左边界-b最小的右边界.

将这四个边界标记出来,讨论下两种情况就行了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define Max 200009
const int inf = 2e9;
struct node{
    int x,y;
};
int main()
{
    int m,i,n,maxx=-inf,maxy=-inf,minx=inf,miny=inf;
    node a,b;
    cin>>m;
    for(i=1;i<=m;i++){
        cin>>a.x>>a.y;
        if(a.y<miny)
            miny = a.y;
        if(a.x>maxx)
            maxx = a.x;}
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>b.x>>b.y;
        if(b.y<minx)
            minx = b.y;
        if(b.x>maxy)
            maxy = b.x;
    }
    int ans = max(maxy-miny,maxx-minx);
    if(ans<0)
        cout<<"0"<<endl;
    else
    cout<<ans<<endl;
}

C. Anton and Fairy Tale

链接:http://codeforces.com/contest/785/problem/C

解题思路:

一个房里有m粒米,每天补充n粒,每天损失天数粒米,第m天损失m粒米,如果没有米了直接结束,

有个特殊判断:当n>=m时,每天都能补满,只能等第m天一次损失完结束。

按题意可以推出当n天后每天损失n+i粒米,每天补充n粒米,也就是n天后每天损失i粒米。一开始的思路是直接将判断当m>=i*(i+1)/2,后面想到i额外损失的,实际上每天损失的米是n+i粒,在最后一天判断的时候会出错,因为他是先损失再补充如果一次直接损失完了,那么就直接结束了,所以正确的判断公式应该是(m-n)>=i*(i+1)/2

直接用二分爆搜就可以了,需要注意的是右边界可以选择10的十次方,如果是10的9次方搜不到1的18次方的数据,10的11次方又会爆掉。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll fun(ll x){
    return (x+1)*x/2;
}
int main()
{
    ll m,n,mid;
    cin>>m>>n;
    ll l = 1,r =  1e10+9;
    ll ans = m-n;
    if(n>=m){
        cout<<m<<endl;
        return 0;
    }
    while(l<r){
        mid = (l+r)/2;
        if(fun(mid)<ans)
            l = mid;
        else if(fun(mid)==ans){
            cout<<n+mid<<endl;
        return 0;}
        else if(fun(mid)>ans&&fun(mid-1)<ans){
            cout<<n+mid<<endl;
            return 0;}
        else
            r = mid;
        }
    return 0;
}
原文地址:https://www.cnblogs.com/kls123/p/7224687.html