codeforces contest 357

http://codeforces.com/contest/357


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

C 356A Knight Tournament

题意:n个人,m场比赛。下面m行每行三个数l,r,x。表示x打败了l到r

题解:

解法一:丢进set,二分查找l,然后删除l到r。复杂度O(mlogn)

解法二:线段树倒着做。区间修改单点查询。复杂度O(mlogn)

解法三:并查集。(刷完并查集专题再来做)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=300005;
set<int> s;
int ans[N],tmp[N];
int main() {
	int n,m;
	while(~scanf("%d%d",&n,&m)) {
        s.clear();
        for(int i=1;i<=n;++i) s.insert(i);
        memset(ans,0,sizeof(ans));
        int l,r,x;
        for(int i=1;i<=m;++i) {
            scanf("%d%d%d",&l,&r,&x);
            int cnt=0;
            for(set<int>::iterator it=s.lower_bound(l);it!=s.end();++it) {
                if(*it>r) break;
                if(*it==x) continue;
                tmp[++cnt]=*it;
                ans[*it]=x;
            }
            for(int j=1;j<=cnt;++j) s.erase(tmp[j]);
        }
        for(int i=1;i<=n;++i) printf("%d ",ans[i]);puts("");
	}
    return 0;
}


#include<cstdio>
#include<cstring>
const int N=300005;
struct Node {
	int l,r,x;
}a[N];
int tree[N<<2],lazy[N<<2];
void init() {
	memset(tree,0,sizeof(tree));
	memset(lazy,-1,sizeof(lazy));
}
void pushdown(int now) {
	if(lazy[now]==-1) return ;
	tree[now<<1]=tree[now<<1|1]=lazy[now<<1]=lazy[now<<1|1]=lazy[now];
	lazy[now]=-1;
}
void update(int L,int R,int val,int l,int r,int now) {//把[L,R]的值变成val
	if(L<=l&&r<=R) {
		tree[now]=lazy[now]=val;
		return ;
	}
	pushdown(now);
	int mid=l+r>>1;
	if(mid>=L) update(L,R,val,l,mid,now<<1);
	if(mid+1<=R) update(L,R,val,mid+1,r,now<<1|1);
}
int query(int p,int l,int r,int now) {
	if(l==r) return tree[now];
	pushdown(now);
	int mid=l+r>>1;
	if(p<=mid) return query(p,l,mid,now<<1);
	else return query(p,mid+1,r,now<<1|1);
}
int main() {
	int n,m;
	while(~scanf("%d%d",&n,&m)) {
		for(int i=1;i<=m;++i) scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].x);
		init();
		for(int i=m;i>=1;--i) {
			if(a[i].l<=a[i].x-1) update(a[i].l,a[i].x-1,a[i].x,1,n,1);
			if(a[i].x+1<=a[i].r) update(a[i].x+1,a[i].r,a[i].x,1,n,1);
		}
		for(int i=1;i<=n;++i) printf("%d ",query(i,1,n,1));
		puts("");
	}
	return 0;
}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

D  356B  Xenia and Hamming
题意: s1 重复出现n次得到str1, s2 重复出现m次得到str2,len==len(str1)==len(str2)。求有多少个i(1~len),使得str1[i]!=str2[i]
题解:处理出LCM再去做肯定会超时。具体见代码。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=1000005;
char s1[N],s2[N];
ll gcd(ll a,ll b) {
    if(b==0) return a;
    return gcd(b,a%b);
}
int cnt[N][30];
int main() {
    ll n,m;
    while(~scanf("%I64d%I64d%s%s",&n,&m,s1,s2)) {
        int len1=strlen(s1);
        int len2=strlen(s2);
        int d=gcd(len1,len2);
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<len1;++i) ++cnt[i%d][s1[i]-'a'];
        ll ans=0;
        for(int i=0;i<len2;++i) ans+=cnt[i%d][s2[i]-'a'];
        ll len=1ll*len1*len2/d;
        ans=len-ans;
        ans=ans*(n*len1/len);
        //ans=n*len1-ans*n*d/len2;
        printf("%I64d\n",ans);
    }
    return 0;
}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E  356C  Compartments
题意:n个车厢每个车厢a[i]个学生(最多四个)。让最少的学生和别人交换座位,使得每个车厢学生数为0或3或4。
题解:贪心。可以考虑各种方案中满足x个车厢的花费。具体见代码。
#include<cstring>
#include<cstdio>
int a[5];
int main() {
    int n,x;
    while(~scanf("%d",&n)) {
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;++i) {
            scanf("%d",&x);
            ++a[x];
        }
        int ans=0;
        if(a[1]<=a[2]) {
            //1 2-->3
            a[2]-=a[1];
            a[3]+=a[1];
            ans+=a[1];
            //2 2 2-->3 3
            int t=a[2]/3;
            a[2]%=3;
            a[3]+=t*2;
            ans+=t*2;
            if(a[2]==1) {
                //2 4-->3 3
                if(a[4]) {
                    ++ans;
                //2 3 3-->4 4
                } else if(a[3]>=2) {
                    ans+=2;
                } else {
                    ans=-1;
                }
            //2 2-->4
            } else if(a[2]==2) {
                ans+=2;
            }
        } else {
            //1 2-->3
            a[1]-=a[2];
            a[3]+=a[2];
            ans+=a[2];
            //1 1 1-->3
            int t=a[1]/3;
            a[1]%=3;
            a[3]+=t;
            ans+=t*2;
            if(a[1]==1) {
                //1 3-->4
                if(a[3]) {
                    ++ans;
                //1 4 4-->3 3 3
                } else if(a[4]>=2) {
                    ans+=2;
                } else {
                    ans=-1;
                }
            } else if(a[1]==2) {
                //1 1 4-->3 3
                if(a[4]) {
                    ans+=2;
                //1 1 3 3-->4 4
                } else if(a[3]>=2) {
                    ans+=2;
                } else {
                    ans=-1;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值