Codeforces 920 (div3)

Problem - A - Codeforces

没什么问题,几个if else语句,判断一下条件;

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    int k;
    cin >> k;
    
    while(k --)
    {
        int x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4;
        cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
        
        if(x1 == x2)
        {
            int t = abs(y2 - y1);
            cout << t * t << endl;
        }
        else if(x2 == x3)
        {
            int t = abs(y2 - y3);
            cout << t * t << endl;
        }
        else if(x3 == x4)
        {
            int t = abs(y3 - y4);
            cout << t * t << endl;
        }
        else if(x4 == x1)
        {
            int t = abs(y4 - y1);
            cout << t * t << endl;
        }
        else if(x1 == x3)
        {
            int t = abs(y3 - y1);
            cout << t * t << endl;
        }
        else if(x2 == x4)
        {
            int t = abs(y2 - y4);
            cout << t * t << endl;
        }
        else if(y1 == y2)
        {
            int t = abs(x2 - x1);
            cout << t * t << endl;
        }
        else if(y2 == y3)
        {
            int t = abs(x2 - x3);
            cout << t * t << endl;
        }
        else if(y3 == y4)
        {
            int t = abs(x3 - x4);
            cout << t * t << endl;
        }
        else if(y4 == y1)
        {
            int t = abs(x4 - x1);
            cout << t * t << endl;
        }
        else if(y1 == y3)
        {
            int t = abs(x3 - x1);
            cout << t * t << endl;
        }
        else if(y2 == y4)
        {
            int t = abs(x2 - x4);
            cout << t * t << endl;
        }
    }
    
    return 0;
}

Problem - B - Codeforces

B题,看到01数组 我们首先想到贪心或者思维题(找规律),所以我们选择找规律,我们可以发现只要出现一对1和0都不匹配,我们就可以将他们交换,从而匹配,剩下的就只能进行单个修改;

include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    int k;
    cin >> k;
    
    while(k --)
    {
        int n;
        cin >> n;
        string a , b;
        cin >> a >> b;
        
        if(a == b) cout << 0 << endl;
        else 
        {
            int num0 = 0 , num1 = 0;
            for(int i = 0 ; i < a.size() ; i ++)
            {
                if(a[i] == '1' && a[i] != b[i]) num1 ++;
                if(a[i] == '0' && a[i] != b[i]) num0 ++;
            }
            
            int ans = 0 ;
            if(num1 > num0)
            {
                ans += num0;
                num1 -= num0;
                ans += num1;
                cout << ans << endl;
            }
            else if(num1 < num0) 
            {
                ans += num1;
                num0 -= num1;
                ans += num0;
                cout << ans << endl;
            }
            else if(num1 == num0) 
            {
                ans = num1;
                cout << ans << endl;
            } 
        }
    }
    
    return 0;
}

Problem - C - Codeforces

C题,看完题目就可以大概猜到 是一种贪心,也算是一种模拟吧(个人觉得);

根据对几组数据的分析,我们可以知道当 持续到下一个发信息的时刻消耗的电量 <= 关机开机的电量的时候;

我们选择 待机, 否则我们就要 选择关机;最后判断 如果剩余电量 f > 0 那么就YES 否则就是NO;

注意当f = 0的时候 也是表示不成功NO;

还有就是要记得开LL,同时关闭同步流;

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
typedef long long LL;

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int k;
    cin >> k;
    
    while(k --)
    {
        LL n , f , a ,b;
        cin >> n >> f >> a >> b;
        LL t[N];
        for(int i = 1 ; i <= n ; i ++) cin >> t[i];

        for(int i = 1 ; i <= n ; i ++)
        {
            if(f <= 0) 
                break;
            f -= min((t[i]-t[i-1])*(LL)a , b);
        }
        
        if(f > 0) cout << "YES" << endl;
        else if(f <= 0 )cout << "NO" << endl;
    }
    
    return 0;
}

Problem - D - Codeforces

这一题,也是一题贪心的思想,要求绝对值的差值最大;我们将两个数组排序后,进行贪心;

对比A数组最大的数 - B数组最小的数的 绝对值 和 A数组最小的数 - B数组最大的数 的绝对值 进行比较;

找到哪个比较大,进行筛选;从而确定 新数组;

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
typedef long long LL;
int a[N] , b[N];

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int k;
    cin >> k;
    
    while(k --)
    {
        int n,m;
        cin >> n >> m;
        
        for(int i = 1; i <= n ; i ++) cin >> a[i];
        for(int i = 1; i <= m ; i ++) cin >> b[i];
        
        sort(a + 1 , a + 1 + n);
        sort(b + 1 , b + 1 + m);
        
        LL ans = 0;
        int st1 = 1 , ed1 = m;
        int st2 = 1 , ed2 = n;
        
        for(int i = 1 ; i <= n ; i ++)
        {
            if(abs(a[st1] - b[ed1]) >= abs(a[ed2] - b[st2]))
            {
                ans += abs(a[st1] - b[ed1]);
                st1 ++;
                ed1 --;
            }
            else 
            {
                ans += abs(a[ed2] - b[st2]);
                st2 ++;
                ed2 --;
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}
posted @ 2024-01-19 14:56  不过是过客  阅读(8)  评论(0编辑  收藏  举报