Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3336 Accepted Submission(s): 523
Problem Description
Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (l≤i<j≤r), ai≠aj holds.
Chiaki would like to find a lexicographically minimal array which meets the facts.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m (1≤n,m≤105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1≤li≤ri≤n).
It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.
Output
For each test case, output n integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines.
Sample Input
3
2 1
1 2
4 2
1 2
3 4
5 2
1 3
2 4
Sample Output
1 2
1 2 1 2
1 2 3 1 1
补(但是mle/tle):
***减枝,还要增设上一组数据的右端点:
记住优先队列后面greater/less<///>后面有一个空格;
结构体的比值cmp函数书写;
短路运算符(小心数组越界)
千万记得清空队列;
#include<bits/stdc++.h>
using namespace std;
struct qaq {
int a;
int b;
};
bool cmp(qaq nod1,qaq nod2) {
if(nod1.a==nod2.a) {
return nod1.b<nod2.b;
} else
return nod1.a<nod2.a;
}
int main() {
priority_queue<int,vector<int>,greater<int> > q;//后面有一个空格;
int n,t,m,a,b,array[100010];
struct qaq nod[100010];
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i=1; i<=m; i++) {
scanf("%d %d",&nod[i].a,&nod[i].b);
}
sort(nod+1,nod+1+m,cmp);
int flag=-1;
for(int i=1; i<=n; i++) {
q.push(i);
}
memset(array,-1,sizeof(array));
for(int i=1; i<=m; i++) {
if(nod[i].a!=nod[i+1].a||i==m) {
if(flag!=-1) {
for(int j=flag; j<nod[i].a; j++) {
q.push(array[j]);
}
}
for(int j=nod[i].a; j<=nod[i].b; j++) {
if(array[j]==-1) {
array[j]=q.top();
q.pop();
flag = nod[i].a ;
}
}
}
}
for(int i=1; i<=n; i++) {
if(array[i]==-1){
if(!i)
printf("1");
else
printf(" 1");
}
else
if(i==1)
printf("%d",array[i]);
else
printf(" %d",array[i]);
}
printf("\n");
}
}
修改后终于正确的代码qwq:
#include<bits/stdc++.h>
using namespace std;
struct qaq {
int a;
int b;
} nod[100005];
bool cmp(qaq nod1,qaq nod2) {
if(nod1.a==nod2.a) {
return nod1.b<nod2.b;
} else
return nod1.a<nod2.a;
}
priority_queue<int,vector<int>,greater<int> > q;//后面有一个空格;
int n,t,m,ans[100005];
int main() {
scanf("%d",&t);
while(t--) {
while(!q.empty()) {
q.pop();
}
memset(ans,-1,sizeof(ans));
scanf("%d %d",&n,&m);
for(int i=1; i<=m; i++) {
scanf("%d %d",&nod[i].a,&nod[i].b);
}
sort(nod+1,nod+1+m,cmp);
for(int i=1; i<=n; i++) {
q.push(i);
}
int flag=-1,flag1;
for(int i=1; i<=m; i++) {
if(i==m||nod[i].a!=nod[i+1].a) {
if(flag!=-1) {
for(int j=flag; j<nod[i].a; j++) {
q.push(ans[j]);
}
}
else
flag1 = nod[i].a;
for(int j=flag1; j<=nod[i].b; j++) {
if(ans[j]==-1) {
ans[j]=q.top();
q.pop();
}
}
flag = nod[i].a ;
flag1 = nod[i].b;
}
}
for(int i=1; i<=n; i++) {
if(ans[i]==-1) {
if(i==1)
printf("1");
else
printf(" 1");
} else if(i==1)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
printf("\n");
}
return 0;
}
学长的代码:
#include<bits/stdc++.h>
using namespace std;
#define mem(a,x) memset(a,x,sizeof(a))
priority_queue<int, vector<int>, greater<int> > q;
struct node{
int l,r;
}a[100005];
bool cmp(node a,node b){
if(a.l == b.l){
return a.r < b.r;
}
return a.l < b.l;
}
int ans[100005];
int main(){
int T,n,m;
scanf("%d",&T);
while(T--){
while(!q.empty()){
q.pop();
}
mem(ans,-1);
scanf("%d %d",&n,&m);
for(int i = 1;i <= n;i++){
q.push(i);
}
for(int i = 1;i <= m;i++){
scanf("%d %d",&a[i].l,&a[i].r);
}
sort(a + 1,a + 1 + m,cmp);
int ll = 1,rr = 1;
for(int i = 1;i <= m;i++){
if(a[i].l != a[i + 1].l || i == m){
for(int j = ll;j < a[i].l;j++){
if(ans[j] == -1){
ans[j] = 1;
}else{
q.push(ans[j]);
}
}
for(int j = rr;j <= a[i].r;j++){
if(ans[j] == -1){
ans[j] = q.top();
q.pop();
}
}
ll = a[i].l;
rr = max(rr,a[i].r);
}
}
for (int i = 1; i <= n; i++) {
if (ans[i] == -1) {
ans[i] = 1;
}
}
for(int i = 1;i <= n;i++){
if(i == 1){
if(ans[i] == -1){
printf("1");
}else{
printf("%d",ans[i]);
}
}else{
if(ans[i] == -1){
printf(" 1");
}else{
printf(" %d",ans[i]);
}
}
}
printf("\n");
}
return 0;
}
标程:另一种解法:
显然可以从左往右贪心,问题转化成求一个区间里的mex,由于区间左右端点都是递增的,用个set维护下即可。
????????????????????????????????
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
int main() {
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; ++cas) {
int n, m;
scanf("%d%d", &n, &m);
std::vector<int> ends(n, -1);
for (int i = 0; i < n; ++i) ends[i] = i;
for (int i = 0; i < m; ++i) {
int l, r;
scanf("%d%d", &l, &r);
ends[l - 1] = std::max(ends[l - 1], r - 1);
}
std::set<int> unused;
for (int i = 1; i <= n; ++i) {
unused.insert(i);
}
std::vector<int> ret(n);
int l = 0, r = -1;
for (int i = 0; i < n; ++i) {
if (r >= ends[i]) continue;
while (l < i) {
unused.insert(ret[l++]);
}
while (r < ends[i]) {
ret[++r] = *unused.begin();
unused.erase(ret[r]);
}
}
for (int i = 0; i < n; ++i) {
if (i) putchar(' ');
printf("%d", ret[i]);
}
puts("");
}
return 0;
}