链接:戳这里
Each minute can be either interesting or boring. If 15 consecutive minutes are boring then Limak immediately turns TV off.
You know that there will be n interesting minutes t1, t2, ..., tn. Your task is to calculate for how many minutes Limak will watch the game.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 90) — the number of interesting minutes.
The second line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... tn ≤ 90), given in the increasing order.
Output
Print the number of minutes Limak will watch the game.
Examples
input
3
7 20 88
output
35
input
9
16 20 30 40 50 60 70 80 90
output
15
input
9
15 20 30 40 50 60 70 80 90
output
90
Note
In the first sample, minutes 21, 22, ..., 35 are all boring and thus Limak will turn TV off immediately after the 35-th minute. So, he would watch the game for 35 minutes.
In the second sample, the first 15 minutes are boring.
In the third sample, there are no consecutive 15 boring minutes. So, Limak will watch the whole game.
思路:
额 标记一下时间段,然后遍历(1-90) 两个相邻时间段之间相差超过15就 break
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int a[110],num[110];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
num[a[i]]=1;
}
int cnt=1,ans=0;
for(int i=1;i<=90;i++){
///cout<<ans<<" "<<cnt<<endl;
if(num[i]==0) cnt++;
else cnt=1;
ans++;
if(cnt>=16) break;
}
cout<<ans<<endl;
return 0;
}
Problemset of each division should be non-empty.
Each problem should be used in exactly one division (yes, it is unusual requirement).
Each problem used in division 1 should be harder than any problem used in division 2.
If two problems are similar, they should be used in different divisions.
Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other.
Note, that the relation of similarity is not transitive. That is, if problem i is similar to problem j and problem j is similar to problem k, it doesn't follow that i is similar to k.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively.
Each of the following m lines contains a pair of similar problems ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). It's guaranteed, that no pair of problems meets twice in the input.
Output
Print one integer — the number of ways to split problems in two divisions.
Examples
input
5 2
1 4
5 2
output
2
input
3 3
1 2
2 3
1 3
output
0
input
3 2
3 1
3 2
output
1
Note
In the first sample, problems 1 and 2 should be used in division 2, while problems 4 and 5 in division 1. Problem 3 may be used either in division 1 or in division 2.
In the second sample, all pairs of problems are similar and there is no way to split problem between two divisions without breaking any rules.
Third sample reminds you that the similarity relation is not transitive. Problem 3 is similar to both 1 and 2, but 1 is not similar to 2, so they may be used together.
思路:
分成的两个组满足任意的 div2>div1 分别表示里面的元素
且 u v不能再同一组
所以直接找出div1中最大的u和div2中最小的v
ans=max(v-u,0)
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int main(){
scanf("%d%d",&n,&m);
int mx=1,mn=n;
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
mx=max(mx,x);
mn=min(mn,y);
}
cout<<max(mn-mx,0)<<endl;
return 0;
}
For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.
There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of balls.
The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ n) where ti is the color of the i-th ball.
Output
Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.
Examples
input
4
1 2 1 2
output
7 3 0 0
input
3
1 1 1
output
6 0 0
Note
In the first sample, color 2 is dominant in three intervals:
An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color.
An interval [4, 4] contains one ball, with color 2 again.
An interval [2, 4] contains two balls of color 2 and one ball of color 1.
There are 7 more intervals and color 1 is dominant in all of them.
思路:
n<=5*1e3 直接n*n搞
第一层for枚举左边界l 第二层for枚举右边界r
算出区间[l,r]对哪个颜色的贡献
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int a[5050],anw[5050],num[5050];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
mst(num,0);
int pos=a[i];
for(int j=i;j<=n;j++){
num[a[j]]++;
if(i==j){
anw[a[i]]++;
continue;
}
if(num[a[j]]>num[pos]){
pos=a[j];
} else {
if(num[a[j]]==num[pos]){
pos=min(pos,a[j]);
}
}
anw[pos]++;
}
}
for(int i=1;i<=n;i++) cout<<anw[i]<<" ";
cout<<endl;
return 0;
}
Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally:
There is no road between a and b.
There exists a sequence (path) of n distinct cities v1, v2, ..., vn that v1 = a, vn = b and there is a road between vi and vi + 1 for .
On the other day, the similar thing happened. Limak wanted to travel between a city c and a city d. There is no road between them but there exists a sequence of n distinct cities u1, u2, ..., un that u1 = c, un = d and there is a road between ui and ui + 1 for .
Also, Limak thinks that there are at most k roads in Bearland. He wonders whether he remembers everything correctly.
Given n, k and four distinct cities a, b, c, d, can you find possible paths (v1, ..., vn) and (u1, ..., un) to satisfy all the given conditions? Find any solution or print -1 if it's impossible.
Input
The first line of the input contains two integers n and k (4 ≤ n ≤ 1000, n - 1 ≤ k ≤ 2n - 2) — the number of cities and the maximum allowed number of roads, respectively.
The second line contains four distinct integers a, b, c and d (1 ≤ a, b, c, d ≤ n).
Output
Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain n distinct integers v1, v2, ..., vn where v1 = a and vn = b. The second line should contain n distinct integers u1, u2, ..., un where u1 = c and un = d.
Two paths generate at most 2n - 2 roads: (v1, v2), (v2, v3), ..., (vn - 1, vn), (u1, u2), (u2, u3), ..., (un - 1, un). Your answer will be considered wrong if contains more than k distinct roads or any other condition breaks. Note that (x, y) and (y, x) are the same road.
Examples
input
7 11
2 4 7 3
output
2 7 1 3 6 5 4
7 1 5 4 6 2 3
input
1000 999
10 20 30 40
output
-1
Note
In the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length n between 2 and 4, and a path between 7 and 3.
![](https://i-blog.csdnimg.cn/blog_migrate/8a471cddda3b05d74227a1747478c293.png)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,k,A,B,C,D;
int a[10010],b[10010],vis[10010];
int main(){
scanf("%d%d",&n,&k);
scanf("%d%d%d%d",&A,&B,&C,&D);
if(k<=n || n==4) {
cout<<-1<<endl;
return 0;
}
vis[A]=vis[B]=vis[C]=vis[D]=1;
int cnt=3;
a[1]=A;
a[2]=C;
for(int i=1;i<=n;i++){
if(!vis[i] && k){
a[cnt++]=i;
k--;
}
}
a[cnt++]=D;
a[cnt]=B;
for(int i=1;i<=cnt;i++) cout<<a[i]<<" ";
cout<<endl;
b[1]=C;
b[2]=A;
for(int i=3;i<=cnt-2;i++) b[i]=a[i];
b[cnt-1]=B;
b[cnt]=D;
for(int i=1;i<=cnt;i++) cout<<b[i]<<" ";
cout<<endl;
return 0;
}