CF #310 (Div. 2)

原创 2015年07月09日 11:48:37

这场做的还行。题目也容易懂。
A. Case of the Zeros and Ones
题目大意就是给你一串01字符串,如果其中任意相邻的两个字符串为01,那么就可以消去这两个,问你最后剩下字符串的长度。
其实只要统计下0和1这两个字符的个数,取个数少的2倍,用总的长度减去它就行。

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-10
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
int n,m;
int s;
#define N 200010
char ch[N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    while(scanf("%d",&n)!=EOF){
        sfs(ch);
        int zero=0,one=0;
        for(int i=0;i<n;i++){
            if(ch[i] == '0') zero++;
            else
                one++;
        }
        printf("%d\n",n-Min(zero,one)*2);
    }
return 0;
}

B. Case of Fake Numbers
题目大意不好解释,不过只要看懂了就很简单了。。无论怎么转,奇数标号的齿轮总是+1,偶数的总是-1,所以当第一个齿轮达到0的时候,看看全部齿轮的序列是否满足0,1,2….n-1即可。

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-10
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
int n,m;
int s;
#define N 200010
int a[1010];
bool check(){
    for(int i=0;i<n;i++){
        if(a[i]!=i)return false;
    }
    return true;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)
            sf(a[i]);
        while(a[0]){
            int s=1;
            for(int i=0;i<n;i++){
                a[i] += s;
                if(a[i] < 0)
                    a[i] = n-1;
                if(a[i] >= n)
                    a[i] = 0;
                s = -s;
            }
        }
        if(check())
            printf("Yes\n");
        else
            printf("No\n");
    }
return 0;
}

C. Case of Matryoshkas
C 题也是只要读懂就不难。。刚开始没读懂,错了一发。。其实只要统计从1开始的那一行,最大能够连续几个,其他行都要全部拆分,然后再依次序套上去。

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-10
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
int n,m;
int s;
#define N 200010
int a[1010];
//vector<int> v[100005];
pair<int,int> pos[100005];
int size[100005];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    int k;
    while(scanf("%d%d",&n,&k)!=EOF){
//      for(int i=0;i<n;i++)
//          v[i].clear();
        memset(pos,-1,sizeof pos);
        memset(size,0,sizeof size);
        int x;
        for(int i=0;i<k;i++){
            sf(m);
            for(int j=0;j<m;j++){
                sf(x);
//              v[i].push_back(x);
                pos[x].first = i;
                pos[x].second = j;
            }
            size[i] = m;
        }
        int ans = 0;
        int kk = 0;
        int tmpx = pos[1].first;
        int tmpy = pos[1].second;
        kk = 0;
        int p=2;
        while(p<=n&&pos[p].first == tmpx && pos[p].second == tmpy+1){
            tmpy = pos[p].second;
            p++;
        }
        kk += (size[tmpx]-1-(p-2));//统计1的那一行被拆分成了几个模块
        ans += kk+kk; //第一行重新组合需要的步骤数
        for(int i=0;i<k;i++){ //除了第一行以外其他行需要拆分和重新组合的步骤数
            if(i == tmpx) continue;
            ans += (size[i]-1)+size[i];
        }
        printf("%d\n",ans);
    }
return 0;
}

D. Case of Fugitive
D题其实也不算难。题目大意就是有n条线段,它们各自覆盖一个取余,然后他们之间有n-1端空白未被覆盖,现在有m段桥,问你是否能从中选出n-1条桥,使得未被覆盖的区域被覆盖且只被覆盖一次。
贪心思想,对于每个未被覆盖的区域,能够覆盖此区域的长度是有一个范围的,比如相邻覆盖区域为【1,4】和【7,8】那么桥的取值范围就是【7-4,8-1】,即【3,7】。一共有n-1个区间,我们先把它按照左边界的大小从小到大排序。我们还得把m条桥也从小到大排序,对于第i条桥,如果第k个区间存在seg[k].l <= bridge[i]的,即第i条桥长度是超过第k个区间的左边界的,那么它就符合条件,如果有多个符合条件的区间,根据贪心的思想,我们取那个右边界最小且大于等于第k条桥的区间,用第k条桥来覆盖它。这个可以用优先队列实现。

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-10
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
int n,m;
#define N 200010
struct Seg{
    int id;
    ll l,r;
    bool operator < (const Seg &x) const{ //优先队列,使得其按照右边界从小到大进行排序
        return r>x.r;
    }
}a[N];
struct Bridge{
    int id;
    ll len;
}bri[N];
bool cmp(Seg x,Seg y){
    return x.l<y.l;
}
bool cmp1(Bridge x,Bridge y){
    return x.len<y.len;
}
int ans[N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    int k;
    while(scanf("%d%d",&n,&m)!=EOF){
        ll pl,pr;
        ll nl,nr;
        scanf("%I64d%I64d",&pl,&pr);
        n--;
        for(int i=0;i<n;i++){
            scanf("%I64d%I64d",&nl,&nr);
            a[i].id=i;
            a[i].l = nl-pr; //左边界长度
            a[i].r = nr-pl; //右边界长度
            pl = nl;
            pr = nr;
        }
        for(int i=0;i<m;i++){
            scanf("%I64d",&bri[i].len);
            bri[i].id = i+1;
        }
        sort(bri,bri+m,cmp1);
        sort(a,a+n,cmp);
        priority_queue<Seg> q;
        k=0;
        int num=0;
        for(int i=0;i<m;i++){
            while(k<n && a[k].l<=bri[i].len){ //第i条桥大于等于第k个区间
                q.push(a[k]);
                k++;
            }
            if(q.empty() || q.top().r<bri[i].len) //如果q为空 或者 符合条件的最小的右边界小于当前桥的长度,那么该区间不可覆盖
                continue;
            ans[q.top().id] = bri[i].id; //记录每个空白区间被哪个标号的桥所覆盖
            q.pop();
            num++;
        }
//      printf("%d\n",num);
        if(num != n)      //n在上面有n--,其实这里相当于原来的n-1
            printf("No\n");
        else{
            printf("Yes\n");
            for(int i=0;i<n;i++) printf("%d%c",ans[i],i+1==n?'\n':' ');
        }
    }
return 0;
}

Codeforces Round #310 (Div. 2)-A. Case of the Zeros and Ones

ACM codeforces

Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandar...

Codeforces #310 div2 C. Case of Matryoshkas

C. Case of Matryoshkastime limit per test2 seconds memory limit per test256 megabytes inputstandar...
  • Rabbee
  • Rabbee
  • 2015年06月28日 11:16
  • 518

[Codeforces Round #310 (Div. 2)] #ABC题解

没想到时隔两天竟然又打了cf,做了3题就弃疗了。题目链接:Codeforces Round #309 (Div. 2)A. Case of Zeros and Ones [Description] ...

CF题解DIV2

  • 2012年07月09日 14:38
  • 2KB
  • 下载

cf#172div2 符合要求最近的分数

/* B题, 题意:给你x,y,n; a(a>=0),b(b

CF 141 Div2 C Fractal Detector(状态压缩DP)

C. Fractal Detector time limit per test 4 seconds memory limit per test 256 megabytes ...
  • fp_hzq
  • fp_hzq
  • 2012年10月23日 19:16
  • 830

CF#278 (Div. 2) A.(暴力枚举)

A. Giga Tower time limit per test 1 second memory limit per test 256 megabytes inpu...

CF #260 (Div. 2)A

Laptops time limit per test 1 second memory limit per test 256 megabytes 题目链接: ...

CF_#277.5_DIV2_A_B_C

http://codeforces.com/contest/489/problem/A A. SwapSort 给定一个n个序列
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CF #310 (Div. 2)
举报原因:
原因补充:

(最多只允许输入30个字)