2021年度训练联盟热身训练赛第一场

链接:https://ac.nowcoder.com/acm/contest/12606/D
来源:牛客网
 

题目描述

Your friend has secretly picked N consecutive positive

integers between 1 and 100, and wants you to guess if their sum is

even or odd.

If the sum must be even, output 'Even{\tt Even}Even'.  If the sum must be odd, output 'Odd{\tt Odd}Odd'. If the sum could be even or could be odd,

output 'Either{\tt Either}Either'.

输入描述:

The input is a single integer N with 1≤N≤101 \le N \le 101≤N≤10.

输出描述:

Output a single word. The word should be 'Even{\tt Even}Even', 'Odd{\tt Odd}Odd', or 
'Either{\tt Either}Either', according to the rules given earlier.

示例1

输入

复制1

1

输出

复制Either

Either

示例2

输入

复制2

2

输出

复制Odd

Odd

思路分析:因为是在1到100内的连续整数个数,当是连续奇数个的时候,这个时候奇偶是无法确定的,当时连续四个或者四个倍数的时候,一定是偶数个,其余的条件是奇数

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    if(n%2==1)
    {
        cout<<"Either"<<endl;
    }
    else if(n%4==0)
    {
        cout<<"Even"<<endl;
    }
    else
    {
        cout<<"Odd"<<endl;
    }
        
    return  0;
}

链接:https://ac.nowcoder.com/acm/contest/12606/F
来源:牛客网
 

题目描述

To save money, Santa Claus has started hiring other animals besides reindeer to

pull his sleigh via short term 'gig' contracts. As a result, the actual
animals that show up to pull his sleigh for any given trip can vary greatly in
size.

Last week he had 2 buffalo, 37 voles and a schnauzer. Unfortunately, both
buffalo were hitched on the left side and the entire sleigh flipped over in
mid-flight due to the weight imbalance.

To prevent such accidents in the future, Santa needs to divide the animals for
a given trip into two groups such that the sum of the weights of all animals in
one group equals the sum of the weights of all animals in the other. To make
the hitching process efficient, Santa is seeking an integer target weight t
such that all animals that are lighter than t go in one group and those
heavier than t go into the other. If there are multiple such t, he wants
the smallest one. There's one small wrinkle: what should be done if there some
animals have weight exactly equal to t? Santa solves the problem this way: if
there are an even number of such animals, he divides them equally among the two
groups (thus distributing the weight evenly). But if there are an odd number of
such animals, then one of those animals is sent to work with the elves to make
toys (it is not put in either group), and the remaining (now an even number)
are divided evenly among the two groups.

输入描述:

Input describes a list of animals' weights. The first line contains an integer
m (2≤m≤1052 \le m \le 10^52≤m≤105), indicating the number of animals. The next m lines
each have a positive integer. These are the weights of the animals (in ounces).
Animals weighing more than 20 00020\,00020000 ounces are too big to pull the sleigh so
no given weight will exceed this maximum.

输出描述:

Output the smallest integer target weight t, as described above. It's
guaranteed that it is possible to find such an integer.

示例1

输入

复制4 3 6 1 2

4
3
6
1
2

输出

复制4

4

示例2

输入

复制4 11 8 3 10

4
11
8
3
10

输出

复制10

10

示例3

输入

复制2 99 99

2
99
99

输出

复制99

99

题意理解:

输入n个数,分为两组,要求两组数的和相等,给定一个数t,要求t比一组的全部数大,比另一组的全部数小,找出最小的t。

tip:当t和n个数中的数有相同时,如果个数为偶数,就把他们分别分到两组,如果个数为奇数,就剔除一个,变成偶数,按偶数处理。

直接处理前后缀和,当某个i的i-1的前缀和i+1的后缀的值一样大小的时候,答案就是i

accode:

#include<iostream>
#include<memory>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; 
typedef long long ll;

const int N = 1e5+10;

int n,m,a[N];
ll pre[N],back[N],cnt[N];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		cnt[a[i]]++;
	}
	for(int i=1;i<=1e5;i++)
	{
		pre[i]=pre[i-1]+cnt[i]*i;
	}
	for(int i=1e5;i>=1;i--)
	{
		back[i] = back[i+1] + cnt[i]*i;
	}
	for(int i=1;i<=1e5;i++)
	{
		if(pre[i-1]==back[i+1])
		{
			printf("%d\n",i);
			break;
		}
	 } 
	return 0;	
} 

H On Average They’re Purple

链接:https://ac.nowcoder.com/acm/contest/12606/H
来源:牛客网
 

题目描述

 

Alice and Bob are playing a game on a simple connected graph with N nodes and M edges.

Alice colors each edge in the graph red or blue.

A path is a sequence of edges where each pair of consecutive edges have a node in common. If the first edge in the pair is of a different color than the second edge, then that is a ''color change.''

After Alice colors the graph, Bob chooses a path that begins at node 1 and ends at node N. He can choose any path on the graph, but he wants to minimize the number of color changes in the path. Alice wants to choose an edge coloring to maximize the number of color changes Bob must make. What is the maximum number of color changes she can force Bob to make, regardless of which path he chooses? changes she can force Bob to make, regardless of which path he chooses?

输入描述:

The first line contains two integer values N and M with 2≤N≤100 0002 \le N \le 100\,0002≤N≤100000 and 1≤M≤100 0001 \le M \le 100\,0001≤M≤100000. The next M lines contain two integers aia_iai​ and bib_ibi​ indicating an undirected edge between nodes aia_iai​ and bib_ibi​ (1≤ai,bi≤N1 \le a_i, b_i \le N1≤ai​,bi​≤N, ai≠bia_i \not= b_iai​​=bi​).

All edges in the graph are unique.

输出描述:

Output the maximum number of color changes Alice can force Bob to make on his route from node 1 to node N.

示例1

输入

复制3 3 1 3 1 2 2 3

3 3
1 3
1 2
2 3

输出

复制0

0

示例2

输入

复制7 8 1 2 1 3 2 4 3 4 4 5 4 6 5 7 6 7

7 8
1 2
1 3
2 4
3 4
4 5
4 6
5 7
6 7

输出

复制3

3

题意理解:

题目吹的很厉害,但是根本其实就是一个最短路减一,或者是bfs即可

#include<iostream>
#include<memory>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

const int N = 1e5+10;
const int INF = 0x3f3f3f;
const int maxn = 100005 * 2;

int n,m;
bool vis[maxn] = {0};
vector<int> vec[maxn];
int dis[maxn] = {0};
queue<ll> q;


int main()
{
    cin>>n>>m;
    int u,v;
    for(int i=1;i<=m;i++)
    {
        cin>>u>>v;
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    q.push(1);
    vis[1] = 1;
    
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int v:vec[u]){
            if(vis[v]) continue;
            vis[v] = 1;
            
            dis[v] = dis[u]+1;
            q.push(v);
        }
    }
    
    
    cout<<dis[n] - 1 <<endl;
    return 0;    
}

 

链接:https://ac.nowcoder.com/acm/contest/12606/E
来源:牛客网
 

题目描述

You are given a list of integers x1,x2,…,xnx_1, x_2, \ldots, x_nx1​,x2​,…,xn​ and a number k.

It is guaranteed that each i from 1 to k appears in the list at least once.

Find the lexicographically smallest subsequence of x that contains
each integer from 1 to k exactly once.

输入描述:

The first line will contain two integers n and k, with
1≤k≤n≤200 0001\le k\le n\le 200\,0001≤k≤n≤200000.
The following n lines will each contain an integer xix_ixi​ with
1≤xi≤k1\le x_i\le k1≤xi​≤k.

输出描述:

Write out on one line, separated by spaces, the lexicographically smallest
subsequence of x that has each integer from 1 to k exactly once.

示例1

输入

复制6 3 3 2 1 3 1 3

6 3
3
2
1
3
1
3

输出

复制2 1 3

2 1 3

示例2

输入

复制10 5 5 4 3 2 1 4 1 1 5 5

10 5
5
4
3
2
1
4
1
1
5
5

输出

复制3 2 1 4 5

3 2 1 4 5

因为取牌是有后效性的,也就是说你要提前处理出所有数值的牌还剩多少张,然后就是用单调栈进行贪心了。我们要维护单调栈内递增,这样可以做到当前最优的状态,然后配合预处理的牌的张数进行出栈。有一个地方要特殊注意一下,如果这个数值已经在栈内了,那就直接过。

acccode:

#include<iostream>
#include<memory>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; 
typedef long long ll;

const int N = 2e5+10;

int a[N],cnt[N];
int stk[N],tp,vis[N];

int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		cnt[a[i]]++;
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[a[i]])//不在栈中的时候
		{
			while(tp && a[stk[tp]] >a[i] && cnt[a[stk[tp]]]){
				vis[a[stk[tp]]] = 0;
				tp--;
			}
			stk[++tp] = i;
			vis[a[i]] = 1;
		}
		cnt[a[i]]--;//如果数值已经在栈中了就直接跳过
	}
	for(int i=1;i<=tp;i++)
	cout<<a[stk[i]]<<" ";
	return 0;	
} 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值