A. Vasya and Book
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya is reading a e-book. The file of the book consists of n pages, numbered from 1 to n. The screen is currently displaying the contents of page x, and Vasya wants to read the page y. There are two buttons on the book which allow Vasya to scroll d pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 10 pages, and d=3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.
Help Vasya to calculate the minimum number of times he needs to press a button to move to page y.
Input
The first line contains one integer t (1≤t≤103) — the number of testcases.
Each testcase is denoted by a line containing four integers n, x, y, d (1≤n,d≤109, 1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.
Output
Print one line for each test.
If Vasya can move from page x to page y, print the minimum number of times he needs to press a button to do it. Otherwise print −1.
Example
inputCopy
3
10 4 5 2
5 1 3 4
20 4 19 3
outputCopy
4
-1
5
Note
In the first test case the optimal sequence is: 4→2→1→3→5.
In the second test case it is possible to get to pages 1 and 5.
In the third test case the optimal sequence is: 4→7→10→13→16→19.
题意:模拟题
题解:分类模拟即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
int main(){
int t;
scanf("%d",&t);
while(t--){
ll n,x,y,d;
scanf("%lld%lld%lld%lld",&n,&x,&y,&d);
ll ans=-1;
if(abs(y-x)%d){
//if(x>y)swap(x,y);
if(abs(y-1)%d==0){
ans=(y-1)/d+(x-1)/d+((x-1)%d!=0);
}
if(abs(y-n)%d==0){
if(ans==-1)ans=(n-y)/d+(n-x)/d+((n-x)%d!=0);
else ans=min(ans,(n-y)/d+(n-x)/d+((n-x)%d!=0));
}
}
else{
ans=abs(y-x)/d;
}
printf("%lld\n",ans);
}
return 0;
}
B. Vova and Trophies
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vova has won n trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.
The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.
Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.
Input
The first line contains one integer n (2≤n≤105) — the number of trophies.
The second line contains n characters, each of them is either G or S. If the i-th character is G, then the i-th trophy is a golden one, otherwise it’s a silver trophy.
Output
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
Examples
inputCopy
10
GGGSGGGSGG
outputCopy
7
inputCopy
4
GGGG
outputCopy
4
inputCopy
3
SSS
outputCopy
0
Note
In the first example Vova has to swap trophies with indices 4 and 10. Thus he will obtain the sequence “GGGGGGGSGS”, the length of the longest subsegment of golden trophies is 7.
In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 4.
In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0.
题意:给一个只含’G’、'S’的字符串,最多交换两个字符的位置,求最长的连续的G的长度
题解:枚举被交换的S字符,计算S两侧连续G的个数即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
char ch[100005];
int main(){
int n;
scanf("%d",&n);
scanf("%s",ch+1);
int w=0;
for(int i=1;i<=n;i++){
if(ch[i]=='G'){
w++;
}
}
int ans=0;
for(int i=1;i<=n;i++){
if(ch[i]=='S'){
int t1=i-1;
int t2=i+1;
int ac=0;
while(t1>=1&&ch[t1]=='G'){
ac++;
t1--;
}
while(t2<=n&&ch[t2]=='G'){
ac++;
t2++;
}
ans=max(ans,min(ac+1,w));
}
}
if(w==n)ans=w;
printf("%d\n",ans);
return 0;
}
C. Multi-Subject Competition
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A multi-subject competition is coming! The competition has m different subjects participants can choose from. That’s why Alex (the coach) should form a competition delegation among his students.
He has n candidates. For the i-th person he knows subject si the candidate specializes in and ri — a skill level in his specialization (this level can be negative!).
The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.
Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.
(Of course, Alex doesn’t have any spare money so each delegate he chooses must participate in the competition).
Input
The first line contains two integers n and m (1≤n≤105, 1≤m≤105) — the number of candidates and the number of subjects.
The next n lines contains two integers per line: si and ri (1≤si≤m, −104≤ri≤104) — the subject of specialization and the skill level of the i-th candidate.
Output
Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or 0 if every valid non-empty delegation has negative sum.
Examples
inputCopy
6 3
2 6
3 6
2 5
3 5
1 9
3 1
outputCopy
22
inputCopy
5 3
2 6
3 6
2 5
3 5
1 11
outputCopy
23
inputCopy
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
outputCopy
0
Note
In the first example it’s optimal to choose candidates 1, 2, 3, 4, so two of them specialize in the 2-nd subject and other two in the 3-rd. The total sum is 6+6+5+5=22.
In the second example it’s optimal to choose candidates 1, 2 and 5. One person in each subject and the total sum is 6+6+11=23.
In the third example it’s impossible to obtain a non-negative sum.
题意:给出一些物品的类型以及价值,求在所选择类型的物品个数相同的情况下价值的最大值
题解:vector存每种类型的物品价值,排序后计算前缀和,枚举物品个数即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=1e5+5;
vector<ll>g[maxn];
ll ans[maxn],sum[maxn];
bool cmp(ll aw,ll bw){
return bw<aw;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
}
ll ac=0;
for(int i=1;i<=m;i++){
sort(g[i].begin(),g[i].end(),cmp);
for(int j=0;j<g[i].size();j++){
sum[j+1]=sum[j]+g[i][j];
if(sum[j+1]>0){
ans[j+1]+=sum[j+1];
ac=max(ans[j+1],ac);
}
}
}
printf("%lld\n",ac);
return 0;
}
D. Maximum Diameter Graph
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Graph constructive problems are back! This time the graph you are asked to build should match the following properties.
The graph is connected if and only if there exists a path between every pair of vertices.
The diameter (aka “longest shortest path”) of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.
The degree of a vertex is the number of edges incident to it.
Given a sequence of n integers a1,a2,…,an construct a connected undirected graph of n vertices such that:
the graph contains no self-loops and no multiple edges;
the degree di of the i-th vertex doesn’t exceed ai (i.e. di≤ai);
the diameter of the graph is maximum possible.
Output the resulting graph or report that no solution exists.
Input
The first line contains a single integer n (3≤n≤500) — the number of vertices in the graph.
The second line contains n integers a1,a2,…,an (1≤ai≤n−1) — the upper limits to vertex degrees.
Output
Print “NO” if no graph can be constructed under the given conditions.
Otherwise print “YES” and the diameter of the resulting graph in the first line.
The second line should contain a single integer m — the number of edges in the resulting graph.
The i-th of the next m lines should contain two integers vi,ui (1≤vi,ui≤n, vi≠ui) — the description of the i-th edge. The graph should contain no multiple edges — for each pair (x,y) you output, you should output no more pairs (x,y) or (y,x).
Examples
inputCopy
3
2 2 2
outputCopy
YES 2
2
1 2
2 3
inputCopy
5
1 4 1 1 1
outputCopy
YES 2
4
1 2
3 2
4 2
5 2
inputCopy
3
1 1 1
outputCopy
NO
Note
Here are the graphs for the first two example cases. Both have diameter of 2.
d1=1≤a1=2
d2=2≤a2=2
d3=1≤a3=2
d1=1≤a1=1
d2=4≤a2=4
d3=1≤a3=1
d4=1≤a4=1
题意:给出每个点的最大可能度数,求一棵直径最长的树
题解:度数>=2的点依次连接,把度数为1的点连在两端和中间节点即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=505;
vector<ll>g[maxn];
struct edge{
int fr;
int to;
int nex;
}e[maxn<<2];
int head[maxn],cnt,in[maxn];
void adde(int x,int y){
e[cnt].fr=x;
e[cnt].to=y;
e[cnt].nex=head[x];
head[x]=cnt++;
}
int main(){
int n;
scanf("%d",&n);
int sum=0;
int sum2=0;
int ww=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++){
int w;
scanf("%d",&w);
in[i]=w;
g[w].push_back(i);
if(w==1){
sum++;
}
else{
ww++;
sum2+=w-2;
}
}
sum2+=2;
// debug(sum);
// debug(sum2);
if(sum>sum2){
printf("NO\n");
}
else{
int w;
int ans=0;
int k=2;
while(g[k].size()==0)k++;
w=g[k][0];
int www=w;
int wwww=-1;
for(int i=2;i<=n;i++){
for(int j=0;j<g[i].size();j++){
if(g[i][j]==www||in[g[i][j]]==0)continue;
adde(w,g[i][j]);
wwww=g[i][j];
in[w]--;
in[g[i][j]]--;
w=g[i][j];
ans++;
}
}
int t0=0;
int f=1;
if(g[1].size()>=1){adde(g[1][0],www);in[www]--;t0++;}
if(g[1].size()>=2&&wwww!=-1){adde(g[1][1],wwww);in[wwww]--;t0++;}
for(int i=2;i<=n;i++){
for(int j=0;j<g[i].size();){
if(in[g[i][j]]==0){j++;continue;}
while(t0<g[1].size()&&in[g[1][t0]]==0)t0++;
if(t0==g[1].size()){
f=0;
break;
}
adde(g[1][t0],g[i][j]);
t0++;
in[g[i][j]]--;
if(in[g[i][j]]==0)j++;
}
if(f==0)break;
}
if(g[1].size()>=2)ans+=2;
else if(g[1].size()==1)ans++;
printf("YES %d\n",ans);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
printf("%d %d\n",e[i].fr,e[i].to);
}
}
return 0;
}
E. Increasing Frequency
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given array a of length n. You can choose one segment [l,r] (1≤l≤r≤n) and integer value k (positive, negative or even zero) and change al,al+1,…,ar by k each (i.e. ai:=ai+k for each l≤i≤r).
What is the maximum possible number of elements with value c that can be obtained after one such operation?
Input
The first line contains two integers n and c (1≤n≤5⋅105, 1≤c≤5⋅105) — the length of array and the value c to obtain.
The second line contains n integers a1,a2,…,an (1≤ai≤5⋅105) — array a.
Output
Print one integer — the maximum possible number of elements with value c which can be obtained after performing operation described above.
Examples
inputCopy
6 9
9 9 9 9 9 9
outputCopy
6
inputCopy
3 2
6 2 6
outputCopy
2
Note
In the first example we can choose any segment and k=0. The array will stay same.
In the second example we can choose segment [1,3] and k=−4. The array will become [2,−2,2].
题意:给一个长为n的数组,你可以进行一次修改操作,让某个区间的所有元素值+k,其中k为任意你想添加的数,求最终得到的数组里面值为c的个数最多有几个
题解:前缀和+枚举右端点。具体点就是把值为c的元素做前缀和,同时在遍历过程对当前元素ai也做前缀和,遍历过程枚举i为修改区间的右端点,那么左端点的值一定和右端点值相等,使用当前前缀和减去前缀和最小的左端点的前缀和来更新答案。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=5e5+5;
ll g[maxn];
ll a[maxn],b[maxn],c[maxn];
int main(){
ll n,cC;
scanf("%lld%lld",&n,&cC);
ll ans=0;
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
for(int i=1;i<=n;i++){
b[i]=b[i-1];
if(a[i]==cC){
b[i]++;
}
else{
c[a[i]]++;
ans=max(ans,max(1ll,c[a[i]]-b[i]-g[a[i]]));
g[a[i]]=min(c[a[i]]-b[i]-1,g[a[i]]);
}
}
printf("%lld\n",b[n]+ans);
return 0;
}
F. Speed Dial
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp’s phone book contains n phone numbers, each of them is described by si — the number itself and mi — the number of times Polycarp dials it in daily.
Polycarp has just bought a brand new phone with an amazing speed dial feature! More precisely, k buttons on it can have a number assigned to it (not necessary from the phone book). To enter some number Polycarp can press one of these k buttons and then finish the number using usual digit buttons (entering a number with only digit buttons is also possible).
Speed dial button can only be used when no digits are entered. No button can have its number reassigned.
What is the minimal total number of digit number presses Polycarp can achieve after he assigns numbers to speed dial buttons and enters each of the numbers from his phone book the given number of times in an optimal way?
Input
The first line contains two integers n and k (1≤n≤500, 1≤k≤10) — the amount of numbers in Polycarp’s phone book and the number of speed dial buttons his new phone has.
The i-th of the next n lines contain a string si and an integer mi (1≤mi≤500), where si is a non-empty string of digits from 0 to 9 inclusive (the i-th number), and mi is the amount of times it will be dialed, respectively.
It is guaranteed that the total length of all phone numbers will not exceed 500.
Output
Print a single integer — the minimal total number of digit number presses Polycarp can achieve after he assigns numbers to speed dial buttons and enters each of the numbers from his phone book the given number of times in an optimal way.
Examples
inputCopy
3 1
0001 5
001 4
01 1
outputCopy
14
inputCopy
3 1
0001 5
001 6
01 1
outputCopy
18
Note
The only speed dial button in the first example should have “0001” on it. The total number of digit button presses will be 0⋅5 for the first number + 3⋅4 for the second + 2⋅1 for the third. 14 in total.
The only speed dial button in the second example should have “00” on it. The total number of digit button presses will be 2⋅5 for the first number + 1⋅6 for the second + 2⋅1 for the third. 18 in total.
待补
G. Petya and Graph
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n vertices and m edges.
The weight of the i-th vertex is ai.
The weight of the i-th edge is wi.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.
Input
The first line contains two numbers n and m (1≤n≤103,0≤m≤103) - the number of vertices and edges in the graph, respectively.
The next line contains n integers a1,a2,…,an (1≤ai≤109) - the weights of the vertices of the graph.
The following m lines contain edges: the i-e edge is defined by a triple of integers vi,ui,wi (1≤vi,ui≤n,1≤wi≤109,vi≠ui). This triple means that between the vertices vi and ui there is an edge of weight wi. It is guaranteed that the graph does not contain loops and multiple edges.
Output
Print one integer — the maximum weight of the subgraph of the given graph.
Examples
inputCopy
4 5
1 5 2 2
1 3 4
1 4 4
3 4 5
3 2 2
4 2 2
outputCopy
8
inputCopy
3 3
9 7 8
1 2 1
2 3 2
1 3 3
outputCopy
0
Note
In the first test example, the optimal subgraph consists of the vertices 1,3,4 and has weight 4+4+5−(1+2+2)=8. In the second test case, the optimal subgraph is empty.
待补