codeforces round #835 (div. 4)A~G

302 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第2天,点击查看活动详情

A

Given three distinct integers a, b, and c, find the medium number between all of them.

The medium number is the number that is neither the minimum nor the maximum of the given three numbers.

For example, the median of 5,2,6 is 5, since the minimum is 2 and the maximum is 6.

Input

The first line contains a single integer t (1≤t≤6840) — the number of test cases.

The description of each test case consists of three distinct integers a, b, c (1≤a,b,c≤20).

Output

For each test case, output a single integer — the medium number of the three numbers.

题意:找三个数中间的数

思路:排序输出a[2],就可以了

代码:

void solve()
{
    for(int i = 1;i <= 3;i ++)
    {
    cin >> a[i];
     }
     sort(a+1,a+4);
     cout << a[2] << '\n';
 }

B

In order to write a string, Atilla needs to first learn all letters that are contained in the string.

Atilla needs to write a message which can be represented as a string s. He asks you what is the minimum alphabet size required so that one can write this message.

The alphabet of size x (1≤x≤26) contains only the first x Latin letters. For example an alphabet of size 4 contains only the characters a, b, c and d.

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤100) — the length of the string.

The second line of each test case contains a string s of length nn, consisting of lowercase Latin letters.

Output

For each test case, output a single integer — the minimum alphabet size required to so that Atilla can write his message s.

题意:

找最后一个数是多少可以满足s里的字符在里面

思路:

遍历过去,找最大的那个字母是什么就可以了

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
ll a[10];
void solve()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0;i < n;i ++)
	{
		int x = s[i] - 'a' + 1;
		ans = max(ans,x);
	}
	cout << ans << '\n';
}
int main()
{
	int t = 1;
	cin >> t;
	while(t--)
	{
		solve();
	}
}

C

题意:找除了本身a[i]以外,最大的数x,计算a[i]-x的值

思路:从小到大排序,如果b[n] == a[i],那么a[i]-b[n-1],否则a[i]-b[n];

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 1e6;
ll 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(b+1,b+n+1);
	for(int i = 1;i <= n;i ++)
	{
		if(b[n] != a[i])
		{
			cout << a[i] - b[n] << ' ';
		}
		else
		cout << a[i] - b[n-1] << ' ';
	}
	cout << '\n';
}
int main()
{
	int t = 1;
	cin >> t;
	while(t--)
	{
		solve();
	}
}

D

题意:要求就是出现上升后再下降就是NO,其他都是YES

思路:先判断是否出现上升,出现后标记,在判断是否下降,注意不能拿三个判断例如:1 2 2 1,这种情况也是NO,白白wa了2两次,还浪费我半个小时,呜呜呜

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 1e6;
ll a[N],b[N];
void solve()
{
	int n;
	cin >> n;
	for(int i = 1;i <= n;i ++)
	{
		cin >> a[i];
	}
	bool f = 0;
	for(int i = 2;i <= n;i ++)
	{
		if( a[i] > a[i - 1])
		{
			f = 1;
		}
		if(f && a[i] < a[i - 1]){
			cout << "NO" << '\n';
			return ;
		}
	}
	cout << "YES" << '\n';
	
}
int main()
{
	int t = 1;
	cin >> t;
	while(t--)
	{
		solve();
	}
}

E

dp,话不多说

代码;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int  N = 2e5 + 5;
int a[N];
int n;
void check(ll cnt,ll sum,ll res)
{
	ll cnt1 = 0;
  	res = cnt;
  	ll ans = sum;
  	for(int i = 0; i < n; ++i)
  	{
  		if(a[i] == 1)
  		{
  			ans = max(sum - res + cnt1, ans);
  			cnt1++;
  		}
  		else
  		{
  			ans = max(sum - cnt1 + res - 1, ans);
  			res--;
  		}
  	}
  	cout << ans << '\n';
}
void solve()
{
    cin >> n;
  	memset(a,0,sizeof a);
  	ll res = 0;
  	for(int i = 0; i < n; i++)
  	{
  		cin >> a[i];
  		if(!a[i])
  		res ++;
  		//cout << res << "--" << '\n';
  	}
  	ll sum = 0;
  	ll cnt = res;
  	for(int i = 0; i < n; ++i)
  	{
  		if(a[i] == 1)
  			sum += res;
  		else
  			res--;
  	}
  	check(cnt,sum,res);
}
int main() 
{
  int t = 1;
  cin >> t;
  while (t--) 
  {
  	solve();
  }
}

F

题意:给你n个数表示每个任务对应的钱,在d天里赚到c元,问你最多花几天,其中k代表做完某再做务x,k天内不能再做x。

思路:二分加前缀和,先排序把前缀和求出来,如果n天内(n<=d)可以完成那么k任意大,如果最大赚钱的值mx*d<c那么就不可能了,剩下的情况二分答案

代码:

#include <bits/stdc++.h>

 
using namespace std;

  
typedef long long ll;

const int N = 2e6 + 5;

ll a[N],b[N];

ll n,c,d;

bool check(int mid)
{
    ll sum = 0;
    ll ans = 0;
    if(n-mid-1>= 0)
    {
        sum = b[n] - b[(n-mid)-1];
    }
    else
        sum = b[n];   
    int x = d/(mid + 1);
    int y = d%(mid + 1);
    ans = sum * x;
    ll cnt = x*(mid+1);
    if(cnt < d)
    {
        if(n - y >= 0)
        ans += b[n] - b[(n-y)];
        else
        ans += b[n];
    }
    if(ans >= c)
    return true;
    else
    return false;
}
void solve()
{
    cin >> n >> c >> d;
    ll sum = 0;
    for(int i = 1;i <= n;i ++)
    {
        cin >> a[i];
    }
    sort(a+1,a+n+1);
    for(int i = 1;i <= n;i ++)
    {
        b[i] = b[i-1] + a[i];
    }
    if(d >= n)
        sum = b[n];
    else
    {
        sum = b[n] - b[n-d];
    }
    if(sum >= c)
    {
        cout << "Infinity" << '\n';
        return ;
    }
    sum = a[n]*d;
    if(sum < c)
    {
        cout << "Impossible" << '\n';
        return ;
    }
    int l = 0,r = d;
    while(l < r)
    {
        int mid = (l + r + 1) >> 1;
        if(check(mid))
        l = mid;
        else
        r = mid - 1;
    }
    cout << r << '\n';
}
int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
    solve();
    }
}

G

思路:b开始dfs每到一个点都把当前的值加到set里,再从a开始dfs,每到一个点看看set中有没有,有就是yes,跑完没有就是no

代码:

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

const int MAX_N = 2e5 + 5;

int a, b;
bool ok;
vector <pair <int, int>> graph[MAX_N];
set <int> keep;

void dfs1(int u, int p, int total) {
    if (p != -1) {
        keep.insert(total);
    }

    for (auto [v, w] : graph[u]) {
        if (v != p) {
            dfs1(v, u, total xor w);
        }
    }
}

void dfs2(int u, int p, int total) {
    if (u == b) {
        if (total == 0) {
            ok = true;
        }
        return;
    }

    if (keep.count(total)) {
        ok = true;
    }

    for (auto [v, w] : graph[u]) {
        if (v != p) {
            dfs2(v, u, total xor w);
        }
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n >> a >> b;

        ok = false;
        keep.clear();
        for (int i = 1; i <= n; i++) {
            graph[i].clear();
        }

        for (int i = 2; i <= n; i++) {
            int u, v, w;
            cin >> u >> v >> w;

            graph[u].emplace_back(v, w);
            graph[v].emplace_back(u, w);
        }

        dfs1(b, -1, 0);
        dfs2(a, -1, 0);

        if (ok == true) {
            cout << "YES\n";
        }
        else {
            cout << "NO\n";
        }
    }
    return 0;
}