Solved Problem ID Title Ratio (Accepted / Submitted)
1001 Multiply 2 Divide 2 4.23% (28/662)
1002 Hack of Multiply 2 Divide 2 2.51% (34/1357)
1003 Find the Number of Paths 20.00% (4/20)
1004 Yet Another Easy Permutation Count Problem 24.00% (6/25)
1005 Yet Another Easy Function Sum Problem 3.47% (14/403)
1006 Maex 29.17% (890/3051)
1007 Shinobu loves trip 11.93% (330/2765)
1008 Shinobu Loves Segment Tree 60.58% (209/345)
1009 Map 19.73% (448/2271)
1010 Planar graph 33.05% (546/1652)
1011 Find different 61.94% (96/155)
1012 Loop 32.23% (718/2228)
Maex
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 261 Accepted Submission(s): 112
Problem Description
You are given a rooted tree consisting of n vertices numbered from 1 to n, and the root is vertex 1.
Vertex i has a natural number weight ai, and no two different vertexes have the same weight.
Define bu=MEX { x \space | \space \exists v \in subtree\left( u \right), x = a_v$}. Unfortunately,a_iarenotgiven.Pleasefindoutthemaximumpossible\sum_{i=1}^{n}b_i.The\textbf{MEX}$ of a set is the minimum non-negative integer that doesn’t belong to the set.
Input
The first line contains one integer T(1≤T≤10), indicating the number of test cases.
For each test case:
The first line contains one integer n(1≤n≤5⋅105), indicating the number of nodes.
In the following n−1 lines, each line contains two interger u,v(1≤u,v≤n), indicating an edge (u,v) of the tree.
A guarantee is that forming trees.
Output
For each test case:
One line with an integer, indicating the maximum possible ∑ni=1bi.
Sample Input
3
5
1 2
3 2
1 5
4 1
3
1 2
2 3
1
Sample Output
8
6
1
Source
2022“杭电杯”中国大学生算法设计超级联赛(6)
题意:
思路:
#include
using namespace std;
const int maxn = 5e5+10;
vector<int>G[maxn];
long long siz[maxn];
long long res = 0;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
void dfs(int u , int fa){
siz[u]++;
for(int v : G[u]){
if(v==fa)continue;
dfs(v,u);
siz[u] += siz[v];
}
return ;
}
void dfs2(int u, int fa, long long ans){
res = max(res, ans);
for(int v : G[u]){
if(v==fa)continue;
dfs2(v, u, ans+siz[u]);
}
}
int main(){
IOS;
int T; cin>>T;
while(T--){
int n; cin>>n;
for(int i = 0; i <= n; i++){
G[i].clear();
siz[i] = 0;
}
for(int i = 1; i < n ;i++){
int u, v; cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
res = 0;
dfs(1,0);
dfs2(1,0,0);
cout<<res+1<<"\n";//叶节点0没有子节点,但是有非负值1,最后加上
}
return 0;
}
Loop
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 224 Accepted Submission(s): 113
Problem Description
You are given an array a of length n. You must perform exactly k times operations.
For each operation,
∙ First, you select two integers l,r (1≤l≤r≤n),
∙ Second, change a to b, satisfy :
∘ For each i (1≤i
Find the lexicographically largest possible array after k times operations.
Array x is lexicographically greater than array y if there exists an index i ( 1≤i≤n ) such that xi > yi and for every j(1≤j
Input
The first line of the input contains one integer T (1≤T≤100 ) — the number of test cases. Then T test cases follow.
The first line of the test case contains two integers n,k (1≤n,k≤300000)
The second line of the test case contains n integers a1,a2,…,an(1≤ai≤300000)
The sum of n over all testcases doesn’t exceed 106.
The sum of k over all testcases doesn’t exceed 106.
Output
For each testcase,one line contains n integers ,a1,a2,…,an — the lexicographically largest possible array after k times operations.
Don‘t have spaces at the end of the line
Sample Input
2
7 3
1 4 2 1 4 2 4
5 2
4 3 5 4 5
Sample Output
4 4 2 4 2 1 1
5 4 5 4 3
Source
2022“杭电杯”中国大学生算法设计超级联赛(6)
题意:
思路:
//1012 - priority_queue & AC
#include
using namespace std;
const int maxn = 3e5+10;
int a[maxn], b[maxn], c[maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
int T; cin>>T;
while(T--){
int n, k; cin>>n>>k;
for(int i = 1; i <= n; i++)cin>>a[i];
priority_queue<int>q; //默认大根堆
int cnt = 0;
for(int i = 1; i <= n; i++){
while(cnt && b[cnt]<a[i] && k){ //a[i-1]
q.push(b[cnt]); //a[i-1]
k--;
cnt--;
}
b[++cnt] = a[i]; //维护剩余数组
}
//归并
int num = 0, t = 1;
for(int i = 1; i <= n; i++){
if(t > cnt){
c[++num] = q.top(); q.pop();
}else if(q.size()==0){
c[++num] = b[t]; t++;
}else{
if(q.top()>b[t]){
c[++num] = q.top(); q.pop();
}else{
c[++num] = b[t]; t++;
}
}
}
for(int i = 1; i <= n; i++){
cout<<c[i]<<" \n"[i==n];
}
}
return 0;
}
//1012 - list & TLE
#include
using namespace std;
const int maxn = 3e5+100;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
int a[maxn];
// void Print(auto first, auto last){
// for (; first != last; ++first)
// cout << *first << " ";
// cout << endl;
// }
void Print2(auto first, auto last){
last--; last--;
for (; first != last; ++first)
cout << *first << " ";
cout<< (*last)<<"\n";
}
int main(){
IOS;
int T; cin>>T;
while(T--){
int n, k; cin>>n>>k;
for(int i = 1; i <= n; i++){
cin>>a[i];
}
a[n+1] = -1;
n++;
list<int>lst(a+1,a+n+1);
// Print(lst.begin(),lst.end());
int nk = 0;
for(auto i = lst.begin(); ; ){
auto x = i, y = i; y++;
if((*i) == -1)break;
if((*x) < (*y)){
//找到第一个
auto cur = y;
while((*cur) >= (*x))cur++;
//在cur前面插入x
lst.insert(cur,1,(*x));
lst.erase(i);
i = y;
if(i!=lst.begin())i--;
nk++;
// Print(lst.begin(),lst.end());
if(nk==k)break;
}else{
i++;
}
}
Print2(lst.begin(),lst.end());
}
return 0;
}
Planar graph
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Others)
Total Submission(s): 289 Accepted Submission(s): 112
Problem Description
We say an undirected graph is a planar graph, if it exists a way to draw it on a planar, such that no two edges have intersection except the endpoint. For example, the graph below is a planar graph:
But this graph below is not a planar graph, since it can be proved that no matter how to draw this graph on a planar, at least two edges have intersection which is not an endpoint:
For a planar graph, it has some areas separated by edges. For example, the planar graph below has 4 areas (Note that the area 1 is the infinite planar outside):
Give you a planar graph with n vertices and m edges. Each area sets a country. You are the designer and you want to build some tunnels on the edges such that: From one city, you can travel to any other city by going through some tunnels or passing some cities(i.e. you can’t cross one edge unless it sets a tunnel). For example, for the graph above, you can build tunnels like this:
In the picture above, you can travel from city 2 to city 3 by going through tunnel 1, passing the city 1, then going through tunnel 3, passing the city 4, finally going through tunnel 2, and you can arrive to city 3. You can check that from any city you can travel to any other city.
You want the number of tunnels as small as possible. Print the minimum number of tunnels and the ID of edges you build tunnel on.
Input
The first line contains one integer T(1≤T≤15), described the number of test cases.
For each test case:
The first line contains two integers n,m(1≤n≤105,0≤m≤2×105) separated by a space, described the number of vertices and edges.
Next m lines, the i-th line contains two integers u,v(1≤u,v≤n), separated by a space, described the endpoint of the i-th edge. The ID of the i-th edge is i.
It is guaranteed that the given graph is a planar graph. But this graph may have self-loop, parallel edge and the graph may not connected.
Output
The output contains 2T lines.
For each test case:
The first line contains one integer f, described the minimum number of tunnels you have to build.
The second lines contains f integers separated by spaces, the i-th integer described the ID of edges the i-th tunnel built on.
If for a fixed minimum number of tunnels, it has many ways to build the tunnels, print the lexicographically smallest answer.
Sample Input
1
5 7
1 1
1 2
1 3
3 4
3 4
2 4
2 5
Sample Output
3
1 2 4
Source
2022“杭电杯”中国大学生算法设计超级联赛(6)
题意:
思路:
//1010
#include
using namespace std;
const int maxn = 2e6+10;
int fa[maxn+10];
void init(int n){for(int i = 1; i <= n; i++)fa[i]=i;}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x, int y){x=find(x);y=find(y);if(x!=y)fa[x]=y;}
int count(int n){int cnt=0; for(int i = 1; i <= n; i++)if(fa[i]==i)cnt++;return cnt;}
struct edge{ int u, v, w;}e[maxn];
bool cmp(edge a, edge b){return a.w>b.w; }
int main(){
ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
int T; cin>>T;
while(T--){
int n, m; cin>>n>>m;
init(n+1);
for(int i = 1; i <= m; i++){
cin>>e[i].u>>e[i].v; e[i].w = i;
}
sort(e+1,e+m+1,cmp);
vector<int>ans(m+1, 0);
int res = 0; //Kruskal
for(int j = 1; j <= m; j++){
int x = find(e[j].u), y = find(e[j].v);
if(x != y){
merge(x,y);
ans[e[j].w] = 1;
res++;
}
}
cout<<m-res<<'\n'; //不在生成树中的边
vector<int>vc;
for(int i = 1; i <= m; i++){
if(!ans[i])cout<<i<<" ";
}
cout<<"\n";
}
return 0;
}
Map
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Others)
Total Submission(s): 328 Accepted Submission(s): 119
Special Judge
Problem Description Input Each test case contains eight lines. Each line has two integers x,y(−103≤x,y≤103) separated by one space. The first four lines are the coordinates of the upper left corner, the upper right corner, the lower right corner and the lower left corner of M. The last four lines are the coordinates of the upper left corner, the upper right corner, the lower right corner and the lower left corner of m. It is guaranteed that m is within M, both of the them are in the shape of rectangle, and m is compressed from M. Please note that the upper left corner, the upper right corner, the lower right corner and the lower left corner of m and M are one-to-one corresponding. For example, in the picture of Hint below, the correspondence of points is A−a, B−b, C−c, D−d. But A−c, B−d, C−a, D−b is not allowed. Output Sample Input Sample Output Hint Source 题意: 思路: Shinobu loves trip Problem Description There are P countries in total, numbered 0,1,…,P−1.(It is guaranteed that P is a prime number) It is known that when Shinobu is in the country numbered i, the next country she visits must be the country numbered (i⋅a)modP (a is a constant parameter), and it takes Shinobu 1 day to go from one country to another. In order to travel smoothly, Shinobu has customized n travel plans, and the i-th travel plan is represented by the starting country si and the travel days di. For example, if P=233, a=2, a plan’s starting country is 1 and travel days is 2, then Shinobu will visit the city {1,2,4} according to this plan. Playf knows these travel plans and the value of parameter a, now he wants to ask you q questions. The i-th question asks how many travel plans will make shinobu visit the country xi. Input For each testcase, the first line contains four integers P, a, n, q(2≤a
Each of the next n lines contains two integers si, di(0≤si
Each of the next q lines contains one integer xi(0≤xi
It is guaranteed that P is a prime number. Output Sample Input Sample Output Source 题意: 思路:
Sakuyalove has a large world map M and a small world map m. Both of the them are in the shape of rectangle. The small map m is compressed from the large map M. If the length of M is a and the width of M is b, then the length of m is ka and the width of m is kb, where 0
The first line contains one integer T(1≤T≤105), described the number of test cases.
Your output should contains T lines. Each line contains two real numbers x,y separated by one space, represents the coordinates of the point P. Your absolute error should not exceed 10−6.
1
0 5
15 5
15 0
0 0
3 2
9 5
10 3
4 0
6.000000 2.000000
In the first example, the picture is like this:
2022“杭电杯”中国大学生算法设计超级联赛(6)
#include
7.Shinobu loves trip
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Others)
Total Submission(s): 960 Accepted Submission(s): 219
As a cold-blooded, hot-blooded, and iron-blooded vampire, Shinobu loves traveling around.
The first line of the input contains one integer T (1≤T≤5 ) — the number of test cases. Then T test cases follow.
For each testcase, print q lines, the i-th line contains one integer — the answer to the i-th question.
2
3 2 1 1
1 1
2
5 4 3 2
1 4
4 3
2 100000
4
2
1
2
1
2022“杭电杯”中国大学生算法设计超级联赛(6)#include