CodeForces 740C Alyona and mex 简单构造题
2016-11-24 21:50
417 查看
C - Alyona and mex
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
740C
Description
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th
subarray is described with two integers li and ri,
and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the
smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by
Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n),
that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should
be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Sample Input
Input
Output
Input
Output
Hint
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of
the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is
equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
先找到一个最小段的,然后其他的循环构造
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
ll n,m,l,r;
ll a[100005];
ll b[100005][2];
int main()
{
ll re = inf_ll,r1,l1;
cin >> n >> m;
for(int i=1;i<=m;i++)
{
cin >> b[i][0] >> b[i][1];
if(re>b[i][1]-b[i][0]+1)
{
re = b[i][1]-b[i][0]+1;
l1 = b[i][0];
r1 = b[i][1];
}
}
ll num = r1-l1+1;
for(int i=0;i<num;i++)
{
a[i] = i;
}
cout << num <<endl;
ll t1 = n%(num);
ll t2 = n/(num);
for(int i=1;i<=t2;i++)
{
for(int i=0;i<num;i++)
{
cout << a[i] << " ";
}
}
for(int i=0;i<t1;i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
740C
Description
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th
subarray is described with two integers li and ri,
and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the
smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by
Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n),
that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should
be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Sample Input
Input
5 3 1 3 2 5 4 5
Output
2 1 0 2 1 0
Input
4 2 1 4 2 4
Output
3 5 2 0 1
Hint
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of
the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is
equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
先找到一个最小段的,然后其他的循环构造
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
ll n,m,l,r;
ll a[100005];
ll b[100005][2];
int main()
{
ll re = inf_ll,r1,l1;
cin >> n >> m;
for(int i=1;i<=m;i++)
{
cin >> b[i][0] >> b[i][1];
if(re>b[i][1]-b[i][0]+1)
{
re = b[i][1]-b[i][0]+1;
l1 = b[i][0];
r1 = b[i][1];
}
}
ll num = r1-l1+1;
for(int i=0;i<num;i++)
{
a[i] = i;
}
cout << num <<endl;
ll t1 = n%(num);
ll t2 = n/(num);
for(int i=1;i<=t2;i++)
{
for(int i=0;i<num;i++)
{
cout << a[i] << " ";
}
}
for(int i=0;i<t1;i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
相关文章推荐
- 【打CF,学算法——二星级】Codeforces 584C Marina and Vasya (简单构造)
- Codeforces 791C Bear and Different Names 简单构造
- Codeforces 489C 简单构造
- 【Codeforces 631 B Print Check 】+ 简单构造
- codeforces 359B B. Permutation(简单构造)
- Windows+cygwin下构造arm-linux交叉编译环境最简单的方法
- 如何构造一个简单的USB过滤驱动程序
- C#中类的构造方法的简单说明
- 双数组trie树基本构造及简单优化
- 双数组trie树的基本构造及简单优化
- 双数组trie树的基本构造及简单优化
- string类的简单构造。
- C#中类的构造方法的简单说明
- 如何构造一个简单的USB过滤驱动程序(转载)
- 双数组trie树基本构造及简单优化
- 基于tcp/ip协议的Socket网络通讯 --> 简单的数据传送和库构造
- 【转帖】如何构造一个简单的USB过滤驱动程序
- (Delphi) TStringList的用法 构造简单数据库
- 使用gtype构造的一个简单学生管理程序其他属性自己加
- 双数组trie树的基本构造及简单优化