2018 Multi-University Training Contest 1 1004Distinct 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≠ajholds.
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

总结:看了很久才明白题意,读题水平也有待提高。总的来说是一个贪心算法,也有dalao使用set维护的算法并没有看懂。。。先把标程贴上慢慢看。

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
    int l,r;
}op[100002];
int a[100002];
int cmp(node a,node b)
{
    if(a.l!=b.l)
        return a.l<b.l;
    else return a.r>b.r;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&op[i].l,&op[i].r);

        }
        sort(op+1,op+1+m,cmp);
        int j  = 1;
        int book[100005];
        //记录要填的每一个数出现的最后位置
        memset(book,0,sizeof book);
        int p = 1;			//将要填入的数字(会不断更新 ) 
        for(int i=1;i<=n;i++)
        {
            while( op[j].r<i)
             //当前要填的数跳过了当前区间的右端点
            //说明当前区间已经填过了

            {
                p = 1;
                j++;
            }
            if(j>m||op[j].l>i)
            //当前要填的数要填到当前区间的右边
            //否则说明要填的位置无限制
            {
                a[i] = 1;
                book[1] = i;
            }
            else
            {
                while(book[p]>=op[j].l&&book[p]<=op[j].r) //判断p是否出现过 
                {
                    p++;
                }
                a[i] = p;
                book[p] = i;
            }
        }
        int top = 1;
        for(int i=1;i<=n;i++)
        {
            if(top) top=0;
            else printf(" ");
            printf("%d",a[i]);
        }
        cout<<endl;
    }
    return 0;
}

dls大佬的代码:

// D
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=101000;
int _,n,m,pre[N],l,r,ret[N];
int main() {
	for (scanf("%d",&_);_;_--) {
		scanf("%d%d",&n,&m);
		rep(i,1,n+1) pre[i]=i;
		rep(i,0,m) {
			scanf("%d%d",&l,&r);
			pre[r]=min(pre[r],l);
		}
		per(i,1,n) pre[i]=min(pre[i],pre[i+1]);
		int pl=1;
		set<int> val;
		rep(i,1,n+1) val.insert(i);
		rep(i,1,n+1) {
			while (pl<pre[i]) {
				val.insert(ret[pl]);
				pl++;
			}
			ret[i]=*val.begin();
			val.erase(ret[i]);
		}
		rep(i,1,n+1) printf("%d%c",ret[i]," \n"[i==n]);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值