Bananas are the favoured food of monkeys.
In the forest, there is a Banana Company that provides bananas from different places.
The company has two lists.
The first list records the types of bananas preferred by different monkeys, and the second one records the types of bananas from different places.
Now, the supplier wants to know, whether a monkey can accept at least one type of bananas from a place.
Remenber that, there could be more than one types of bananas from a place, and there also could be more than one types of bananas of a monkey's preference.
Input Format
The first line contains an integer TT, indicating that there are TT test cases.
For each test case, the first line contains two integers NN and MM, representing the length of the first and the second lists respectively.
In the each line of following NN lines, two positive integers i, ji,j indicate that the ii-th monkey favours the jj-th type of banana.
In the each line of following MM lines, two positive integers j, kj,k indicate that the jj-th type of banana could be find in the kk-th place.
All integers of the input are less than or equal to 5050.
Output Format
For each test case, output all the pairs x, yx,ythat the xx-the monkey can accept at least one type of bananas from the yy-th place.
These pairs should be outputted as ascending order. That is say that a pair of x, yx,y which owns a smaller xx should be output first.
If two pairs own the same xx, output the one who has a smaller yy first.
And there should be an empty line after each test case.
样例输入
1 6 4 1 1 1 2 2 1 2 3 3 3 4 1 1 1 1 3 2 2 3 3
样例输出
1 1 1 2 1 3 2 1 2 3 3 3 4 1 4 3
题目来源
#include<stdio.h> #include<vector> #include<string.h> #include<algorithm> using namespace std; int main() { int t; scanf("%d",&t); while(t--){ int m,n,i,x,y,mm=0,mi=100; vector<int>mon[500],pla[500],s; scanf("%d%d",&m,&n); for(i=0;i<m;i++){ scanf("%d%d",&x,&y); mon[x].push_back(y); mm=max(mm,x); mi=min(mi,x); } for(i=0;i<n;i++){ scanf("%d%d",&x,&y); pla[x].push_back(y); } int a[52],k=0,j,l; for(i=mi;i<=mm;i++){ memset(a,0,sizeof a); for(j=0;j<mon[i].size();j++){ for(l=0;l<pla[mon[i][j]].size();l++){ a[pla[mon[i][j]][l]]=1; } } for(j=0;j<51;j++){ if(a[j]) printf("%d %d ",i,j); } } puts(""); } }
Coconut is Captain Gangplank's favourite fruit. That is why he needs to drink coconut juice from bb coconuts each day.
On his next trip, he would pass through NNcitis.
His trip would begin in the 11-st city and end in the NN-th city.
The journey from the ii-th city to the (i+1)(i+1)-th city costs D_iDi days.
Initially, there is no coconut on his ship. Fortunately, he could get supply of C_iCicoconuts from the ii-th city.
Could you tell him, whether he could drink coconut juice every day during the trip no not?
Input Format
The first line contains an integer TT, indicating that there are TT test cases.
For each test case the first line contains two integers NN and bb as described above.
The second line contains NN integers C_1, C_2, cdots, C_NC1,C2,⋯,CN.
The third line contains N-1N−1 integers D_1, D_2, cdots, D_{N-1}D1,D2,⋯,DN−1.
All integers in the input are less than 10001000.
Output Format
For each case, output Yes if Captain Gangplank could drink coconut juice every day, and otherwise output No.
样例输入
2 4 1 3 2 1 4 1 2 3 4 2 2 4 6 8 3 2 1
样例输出
Yes No
题目来源
#include<stdio.h> int main() { int a[1005],t,i,j,n,m,s,x,k; scanf("%d",&t); while(t--) { k=s=0; scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { s+=a[i]; scanf("%d",&x); s-=x*m; if(s<0) { k=1; continue; } } if(k)puts("No"); else puts("Yes"); } }
The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545and t_i=frac{1}{2}i(i+1)ti=21i(i+1), are called half-consecutive.
For given NN, find the smallest rr which is no smaller than NN such that t_rtr is square.
Input Format
The input contains multiple test cases.
The first line of a multiple input is an integer TTfollowed by TT input lines.
Each line contains an integer N~(1le Nle 10^{16})N (1≤N≤1016).
Output Format
For each test case, output the case number first.
Then for given NN, output the smallest rr.
If this half-consecutive number does not exist, output -1−1.
样例输入
4 1 2 9 50
样例输出
Case #1: 1 Case #2: 8 Case #3: 49 Case #4: 288
题目来源
这几个数很少,打表找规律都行的
不过我读错了题意,这个锅我背
#include<bits/stdc++.h> using namespace std; typedef long long LL; LL a[100]={1, 8, 49, 288, 1681, 9800, 57121, 332928, 1940449, 11309768, 65918161, 384199200, 2239277041, 13051463048, 76069501249, 443365544448, 2584123765441, 15061377048200, 87784138523761, 511643454094368, 2982076586042449, 17380816062160328}; int main() { ios::sync_with_stdio(false); int T,k=1; scanf("%d",&T); while(T--) { LL n; scanf("%lld",&n); int pos=lower_bound(a,a+22,n)-a; printf("Case #%d: %lld ",k++,a[pos]); } return 0; }
On the mysterious continent of Tamriel, there is a great empire founded by human.
To develope the trade, the East Empire Company is set up to transport goods from place to place.
Recently, the company wants to start their business in Solstheim, which is consists of NNislands.
Luckily, there are already MM sea routes.
All routes are one-way, and the ii-th route can transport person and goods from island u_iui to v_ivi.
Now, the company nominates you a particular job to plan some new routes to make sure that person and goods can be transported between any two islands.
Furthermore, because the neighboring regions are under attack by an increasing number of dragons, limited resources can be used to set up new routes.
So you should plan to build new routes as few as possible.
Input Format
The first line contains an integer TT, indicating that there are TT test cases.
For each test case, the first line includes two integers N~(N leq 10000)N (N≤10000) and M~(M leq 100000)M (M≤100000), as described above.
After that there are MM lines. Each line contains two integers u_iui and v_ivi.
Output Format
For each test case output one integer, represent the least number of routes required to new.
样例输入
2 4 3 1 2 2 3 3 4 4 4 1 2 1 4 3 2 3 4
样例输出
1 2
题目来源
这个求lca的,找个模板就能a
#include<bits/stdc++.h> #define N 100000+10 using namespace std; vector<int>g[N]; int low[N],dfn[N],dfs_clock, sccon[N],scc_cnt; bool instack[N]; stack<int>s; int n,m; void tarjan(int u,int fa) { int v; low[u]=dfn[u]=++dfs_clock; s.push(u); instack[u]=true; for(int i=0;i<g[u].size();i++) { v=g[u][i]; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { scc_cnt++; while(1) { v=s.top(); s.pop(); instack[v]=false; sccon[v]=scc_cnt; if(v==u) break; } } } int in[N],out[N]; void soudian() { int i,j; for(i=1;i<=scc_cnt;i++) in[i]=out[i]=0; for(i=1;i<=n;i++) { for(j=0;j<g[i].size();j++) { int u=sccon[i]; int v=sccon[g[i][j]]; if(u!=v) { out[u]++; in[v]++; } } } } int main() { int t,x,y; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) g[i].clear(); while(m--) { scanf("%d%d",&x,&y); g[x].push_back(y); } memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(sccon,0,sizeof(sccon)); memset(instack,false,sizeof(instack)); dfs_clock=scc_cnt=0; for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i,-1); soudian(); if(scc_cnt==1) { printf("0 "); continue; } int si=0,so=0; for(int i=1;i<=scc_cnt;i++) { if(in[i]==0) si++; if(out[i]==0) so++; } printf("%d ",max(si,so)); } }
You have two strings SS and TT in all capitals.
Now an efficient program is required to maintain a operation and support a query.
The operation C~i~chC i ch with given integer ii and capital letter chch, changes the ii-th character of SS into chch.
The query Q~i~jQ i j asks the program to find out, in the substring of SS from the ii-th character to the jj-th one, the total number of TT appearing.
Input Format
The first line contains an integer TT, indicating that there are TT test cases.
For each test case, the first line contains an integer N~(N leq 100000)N (N≤100000).
The second line is the string S~(|S| leq 100000)S (∣S∣≤100000) and the third line is the string T~(|T| leq 10)T (∣T∣≤10).
Each of the following NN lines provide a operation or a query as above descriptions.
Output Format
For each query, output an integer correponding to the answer.
Output an empty line after each test case.
样例输入
1 5 AABBABA AA Q 1 3 C 6 A Q 2 7 C 2 B Q 1 5
样例输出
1 2 0
题目来源
这个题看起来挺简单的,大概就是区间修改区间查询
直接线段树就可以的,但是有些线段树的常数有点大,可以换个姿势比如树状数组水过的
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char a[100001]; #define NN 100010 struct node{ int l, r, cover; int sum; int key; }st[NN * 8]; long long f[NN + 2]; void Init(int l, int r, int id){ st[id].l = l; st[id].r = r; st[id].cover = 0; st[id].key = 0; if (r - l <= 1){ st[id].cover = 1; st[id].sum = f[l]; return; } int mid = (l + r) >> 1; Init(l, mid, id * 2); Init(mid, r, id * 2 + 1); st[id].sum = st[id * 2].sum + st[id * 2 + 1].sum; //key1 } void Update(int l, int r, int key, int id){ //key2 if (st[id].l == l && st[id].r == r){ st[id].key += key; st[id].cover = 1; return ; } st[id].sum += (r - l) * key; //key3 /* if (st[id].cover > 0){ st[id * 2].cover = 1; st[id * 2 + 1].cover = 1; st[id * 2].key += st[id].key; st[id * 2 + 1].key += st[id].key; st[id].cover = 0; st[id].key = 0; }*/ int mid = (st[id].l + st[id].r) >> 1; if (r <= mid){ Update(l, r, key, id * 2); }else if(l >= mid){ Update(l, r, key, id * 2 + 1); }else{ Update(l, mid, key, id * 2); Update(mid, r, key, id * 2 + 1); } } int Search(int l, int r, int id){ int ans = 0; //key4 if (st[id].cover > 0){ ans += st[id].key * (r - l); } //key5 if (st[id].l == l && st[id].r == r){ ans += st[id].sum; return ans; } int mid = (st[id].l + st[id].r) >> 1; if (r <= mid){ return ans + Search(l, r, id * 2); }else if(l >= mid){ return ans + Search(l, r, id * 2 + 1); }else{ return ans + Search(l, mid, id * 2) + Search(mid, r, id * 2 + 1); } } int main() { int t,i,j,s,x,r,m,n,y,k; char c[5],b[15],e[5]; scanf("%d",&t); while(t--) { memset(f,0,sizeof(f)); scanf("%d",&n); scanf("%s%s",a,b); r=strlen(a); m=strlen(b); for(i=0;i+m-1<r;i++) { for(j=0;j<m;j++) { if(a[i+j]!=b[j]) break; } if(j==m)f[i]=1; } Init(0,r,1); //printf("%d ",s); while(n--) { scanf("%s",c); if(c[0]=='Q') { scanf("%d%d",&x,&y); if(y-x+1<m)s=0; else s=Search(x-1,y-m+1,1); printf("%d ",s); } else if(c[0]=='C') { scanf("%d%s",&x,e); a[x-1]=e[0]; if(x-m<0)i=0; else i=x-m; for(;i<x&&i+m-1<r;i++) { for(j=0;j<m;j++) { if(a[i+j]!=b[j]) break; } if(f[i]==1&&j==m||f[i]==0&&j!=m) continue; int u=i+1; if(j!=m) { f[i]=0; Update(u-1,u,-1,1); } if(j==m) { f[i]=1; Update(u-1,u,1,1); } } } } puts(""); } }
In this winter holiday, Bob has a plan for skiing at the mountain resort.
This ski resort has MM different ski paths and NN different flags situated at those turning points.
The ii-th path from the S_iSi-th flag to the T_iTi-th flag has length L_iLi.
Each path must follow the principal of reduction of heights and the start point must be higher than the end point strictly.
An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag.
Now, you should help Bob find the longest available ski trail in the ski resort.
Input Format
The first line contains an integer TT, indicating that there are TT cases.
In each test case, the first line contains two integers NN and MM where 0 < N leq 100000<N≤10000and 0 < M leq 1000000<M≤100000 as described above.
Each of the following MM lines contains three integers S_iSi, T_iTi, and L_i~(0 < L_i < 1000)Li (0<Li<1000)describing a path in the ski resort.
Output Format
For each test case, ouput one integer representing the length of the longest ski trail.
样例输入
1 5 4 1 3 3 2 3 4 3 4 1 3 5 2
样例输出
6
题目来源
最长路,继续dfs维护
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=10003; struct edge { int to,w; }; vector<edge>G[N]; ll dis[N]; ll dfs(int u) { if(dis[u])return dis[u]; ll res=0; for(int i=0;i<(int)G[u].size();i++) { res=max(res,dfs(G[u][i].to)+G[u][i].w); } return dis[u]=res; } int main() { int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) G[i].clear(); for(int i=1;i<=m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); G[u].push_back({v,w}); } ll ans=0; memset(dis,0,sizeof(ll)*(n+5)); for(int i=1;i<=n;i++) { ans=max(dfs(i),ans); } printf("%lld ",ans); } return 0; }