- Banana
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=,mi=;
vector<int>mon[],pla[],s;
scanf("%d%d",&m,&n);
for(i=;i<m;i++){
scanf("%d%d",&x,&y);
mon[x].push_back(y);
mm=max(mm,x);
mi=min(mi,x);
}
for(i=;i<n;i++){
scanf("%d%d",&x,&y);
pla[x].push_back(y);
}
int a[],k=,j,l;
for(i=mi;i<=mm;i++){
memset(a,,sizeof a);
for(j=;j<mon[i].size();j++){
for(l=;l<pla[mon[i][j]].size();l++){
a[pla[mon[i][j]][l]]=;
}
}
for(j=;j<;j++){
if(a[j])
printf("%d %d\n",i,j);
}
}
puts("");
}
}
- Coconut
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[],t,i,j,n,m,s,x,k;
scanf("%d",&t);
while(t--)
{
k=s=;
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(i=;i<n-;i++)
{
s+=a[i];
scanf("%d",&x);
s-=x*m;
if(s<)
{
k=;
continue;
}
}
if(k)puts("No");
else puts("Yes");
}
}
- Half-consecutive Numbers
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~(1\le N\le 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[]={,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
};
int main()
{
ios::sync_with_stdio(false);
int T,k=;
scanf("%d",&T);
while(T--)
{
LL n;
scanf("%lld",&n);
int pos=lower_bound(a,a+,n)-a;
printf("Case #%d: %lld\n",k++,a[pos]);
}
return ;
}
- Islands
- 31.41%
- 1000ms
- 131072K
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=;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()
{
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=;i<=scc_cnt;i++)
in[i]=out[i]=;
for(i=;i<=n;i++)
{
for(j=;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=;i<=n;i++)
g[i].clear();
while(m--)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(sccon,,sizeof(sccon));
memset(instack,false,sizeof(instack));
dfs_clock=scc_cnt=;
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i,-);
soudian();
if(scc_cnt==)
{
printf("0\n");
continue;
}
int si=,so=;
for(int i=;i<=scc_cnt;i++)
{
if(in[i]==)
si++;
if(out[i]==)
so++;
}
printf("%d\n",max(si,so));
}
}
- Query on a string
- 19.46%
- 500ms
- 131072K
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[];
#define NN 100010 struct node{
int l, r, cover;
int sum;
int key;
}st[NN * ]; long long f[NN + ]; void Init(int l, int r, int id){
st[id].l = l;
st[id].r = r;
st[id].cover = ;
st[id].key = ; if (r - l <= ){
st[id].cover = ;
st[id].sum = f[l];
return;
} int mid = (l + r) >> ; Init(l, mid, id * );
Init(mid, r, id * + );
st[id].sum = st[id * ].sum + st[id * + ].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 = ;
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) >> ; if (r <= mid){
Update(l, r, key, id * );
}else if(l >= mid){
Update(l, r, key, id * + );
}else{
Update(l, mid, key, id * );
Update(mid, r, key, id * + );
}
} int Search(int l, int r, int id){
int ans = ;
//key4
if (st[id].cover > ){
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) >> ; if (r <= mid){
return ans + Search(l, r, id * );
}else if(l >= mid){
return ans + Search(l, r, id * + );
}else{
return ans + Search(l, mid, id * ) + Search(mid, r, id * + );
}
}
int main()
{
int t,i,j,s,x,r,m,n,y,k;
char c[],b[],e[];
scanf("%d",&t);
while(t--)
{
memset(f,,sizeof(f));
scanf("%d",&n);
scanf("%s%s",a,b);
r=strlen(a);
m=strlen(b);
for(i=;i+m-<r;i++)
{
for(j=;j<m;j++)
{
if(a[i+j]!=b[j])
break;
}
if(j==m)f[i]=;
}
Init(,r,);
//printf("%d\n",s);
while(n--)
{
scanf("%s",c);
if(c[]=='Q')
{
scanf("%d%d",&x,&y);
if(y-x+<m)s=;
else s=Search(x-,y-m+,);
printf("%d\n",s);
}
else if(c[]=='C')
{
scanf("%d%s",&x,e);
a[x-]=e[];
if(x-m<)i=;
else i=x-m;
for(;i<x&&i+m-<r;i++)
{
for(j=;j<m;j++)
{
if(a[i+j]!=b[j])
break;
}
if(f[i]==&&j==m||f[i]==&&j!=m)
continue;
int u=i+;
if(j!=m)
{
f[i]=;
Update(u-,u,-,);
}
if(j==m)
{
f[i]=;
Update(u-,u,,);
}
}
}
}
puts("");
}
}
- Skiing
- 23.59%
- 1000ms
- 131072K
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=;
struct edge
{
int to,w;
};
vector<edge>G[N];
ll dis[N];
ll dfs(int u)
{
if(dis[u])return dis[u];
ll res=;
for(int i=;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=;i<=n;i++)
G[i].clear();
for(int i=;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].push_back({v,w});
}
ll ans=;
memset(dis,,sizeof(ll)*(n+));
for(int i=;i<=n;i++)
{
ans=max(dfs(i),ans);
}
printf("%lld\n",ans);
}
return ;
}