Codeforces Round #835 (Div. 4)


一、A - Medium Number

  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int a[N];
void solve()
{
    cin >> a[1] >> a[2] >> a[3];
    sort(a + 1,a + 1 + 3);
    cout << a[2] << endl;
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
   
    return 0;
}

二、B - Atilla’s Favorite Problem

-思路: 排个序找最大值输出即可

  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int a[N];
void solve()
{
   int n; cin >> n;
   string s; cin >> s;
   sort(s.begin(),s.end());
   cout << s[n - 1] - 'a' + 1 << endl;
    
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
    
    return 0;
}

三、C - Advantage

  • 思路: 记录数组中的最大值与次大值,然后枚举数组中的每一个元素,如果A[i] 不是最大值,就减去最大值,如果是最大值就减去次大值
  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int a[N],b[N];
void solve()
{
   int n; cin >> n;
   
   for(int i = 1;i <= n;i ++ ) 
   {
       cin >> a[i];
       b[i] = a[i];
   }
   
   sort(a + 1,a + 1 + n);
   int mx = a[n];
   int mx2 = a[n - 1];
   
   for(int i = 1;i <= n;i ++ )
   {
       if(b[i] != mx)
       cout << b[i] - mx << ' ';
       else
       cout << b[i] - mx2 << ' ';
   }
   
   cout << endl;
   
    
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
    
    return 0;
}

四、D - Challenging Valleys

  • 思路: 数组只有3种情况 ,一直非递减,一直非递增,先递减再递增,那就判断如果当前遍历到的是递减的情况,就看一下前面是否出现递增,如果出现就不行 其他情况都可以
  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int a[N],b[N];
void solve()
{
   int n; cin >> n;
   bool f1 = false,f2 = false,success = true;
   for(int i = 1;i <= n;i ++ )  cin >> a[i];
   if(n == 1) cout << "YES" << endl;
   else
   {
         for(int i = 2;i <= n;i ++ ) // 当且出现一次
   {
       if(a[i] == a[i - 1]) continue;
      
       if(a[i] < a[i - 1])
       {
           if(!f2)
           f2 = true;
           
           if(f1)
           {success = false;
           break;
           }
       }
    
       if(a[i] > a[i - 1]) f1 = true; // 上升
   }
   
  if(success) cout << "YES" << endl;
  else cout << "NO" << endl;
   }
 
    
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
    
    return 0;
}

五、E - Binary Inversions

  • 思路: 只能改变一次,如果是在位置 i 的1变成0对结果的影响是, i 到 n 之间 0 的个数cnt0,和 1 到 i 之间1的个数cnt1,就是cnt1 - cnt0;如果位置 i 是 0 然后变成1的话,同理,所以我们就用两个前缀和数组记录一下1的个数与0的个数,枚举每一个位置取最大值就好
  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int a[N],s0[N],s1[N];

void solve()
{
   int n; cin >> n;
   for(int i = 1;i <= n;i ++ ) cin >> a[i];
   
   for(int i = 1;i <= n;i ++ )
   {
       if(a[i] == 1)
       {
           s1[i] = s1[i - 1] + 1;
           s0[i] = s0[i - 1];
       }
       else
       {
           s1[i] = s1[i - 1];
           s0[i] = s0[i - 1] + 1;
       }
       
   }
    int ans = 0;
    for(int i = 1;i <= n;i ++ )
    {
        if(a[i] == 1)
        {
            ans += s0[n] - s0[i];
        }
    }
    int res = ans;
    for(int i = 1;i <= n;i ++ )
    {
        if(a[i] == 0) // 把这个翻转成1
        {
            int k = s0[n] - s0[i];
            int w = s1[i - 1];
            
            ans = max(ans,res + k - w);
          
        }
        else
        {
            int k = s0[n] - s0[i]; // 后面的0
            int w = s1[i - 1];
            ans = max(ans,res - k + w);
            
        }
    }
    
    cout << ans << endl;
    
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
    
    return 0;
}

六、F - Quests

  • 思路: 首先贪心思想,开始我们一定要选权值大的,所以我们给数组排一个降序
    1 无解的情况 : d * a[1] < c : d天每天选择最大的还是不行,就无解
    2 无穷大的情况: a[1] + a[2] + … a[d] >= c,前d天每天只选择不同的权值的和大于等于c
    3 其它情况就是直接二分k值, 就是相当于周期是(k + 1),所以只用下标枚举从1到k + 1即可,然后计算a[i]被选择了几次,求和(a[i] * w1 + a[i +1] * w2 + a[i + 1] * w3…a[k + 1] * wk + 1)然后与c比较 w就是a[i]被选择几次
  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int a[N];
int c,d,n;
bool check(int mid)
{
    int sum = 0;
    
    for(int i = 1;i <= min(mid + 1,n);i ++ )
    {

        sum += a[i] * ((d + mid + 1 - i) / (mid + 1));
        
    }
    
   
    if(sum >= c)
    {
         return true;
    }
    
    return false;
}
bool cmp(int x,int y)
{
    return x > y;
}
void solve()
{
   cin >> n >> c >> d;
   int s = 0;
   for(int i = 1;i <= n;i ++ )
   {
        cin >> a[i];
   }
   
   sort(a + 1,a + 1 + n,cmp);
   for(int i = 1;i <= n;i ++ )
   if(i <= d)
   s += a[i];
   
   int sum = 0;
   sum = a[1] * d;
   if(sum < c) cout << "Impossible" << endl;
   else if(s >= c) cout << "Infinity" << endl;
   else
   {
       int l = 0,r = 1e9;
       
       while(l <= r)
       {
           int mid = (l + r) / 2;
           
           if(check(mid)) l = mid + 1;
           else r = mid - 1;
       }
       
      cout << r << endl;
   }
   
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
    
    return 0;
}

七、G - SlavicG’s Favorite Problem

  • 思路: dfs从a点开始对每一个点 p 求一个从a开始到p的前缀异或和,然后记录这个值但是不遍历b点,然后再从b点来一次dfs,如果之前出现过就代表可以成功
  • 代码:
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
#define int long long
using namespace std;

const int N = 1e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
int h[N], e[M], w[M], ne[M], idx;
int x[N],y[N];
map<int,int> mp;
int n,a1,b1;
bool f;

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs(int u,int fa,int k)
{
    mp[k] = 1;
    for(int i = h[u];i != -1; i = ne[i])
    {
        int j = e[i];
        if(j == fa) continue;
        if(j == b1) continue;
        
        dfs(j,u,k ^ w[i]);
    }
}

void dfs2(int u,int fa,int k)
{
  
    for(int i = h[u];i != -1; i = ne[i])
    {
        int j = e[i];
        if(j == fa) continue;
        dfs2(j,u,k ^ w[i]);
        if(f) return;
        if(mp[k ^ w[i]]) 
        {
            f = true;
            return;
        }
    }

}

void solve()
{
    cin >> n >> a1 >> b1;
    memset(h, -1, sizeof h);
    mp.clear();
    idx = 0;
    for(int i = 1;i <= n - 1;i ++ )
    {
         int u,v,q; cin >> u >> v >> q;
         add(u,v,q);
         add(v,u,q);
    }
    
    dfs(a1,-1,0);
     f = false;
    dfs2(b1,-1,0);
    
   
    if(f) cout << "YES" << endl;
    else cout << "NO" << endl;
    
   
}

signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();
    
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值