2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)题解

来源:互联网 发布:推荐淘宝店知乎2016 编辑:程序博客网 时间:2024/05/22 10:49

题目连接:http://codeforces.com/gym/100851

A

维护一个set,遍历的时候如果set里已经有了则删除,否则插入,当set为空时则是一个段。

<span style="font-size:14px;">/* ***********************************************Author        :angon************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)#define lld %I64d#define REP(i,k,n) for(int i=k;i<n;i++)#define REPP(i,k,n) for(int i=k;i<=n;i++)#define scan(d) scanf("%d",&d)#define scanl(d) scanf("%I64d",&d)#define scann(n,m) scanf("%d%d",&n,&m)#define scannl(n,m) scanf("%I64d%I64d",&n,&m)#define mst(a,k)  memset(a,k,sizeof(a))#define LL long long#define N 100005#define mod 1000000007inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}set<int>s;int a[N],b[N];int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int t,n;    scan(t);    while(t--)    {        scan(n);        REPP(i,1,n) scan(a[i]);        REPP(i,1,n) scan(b[i]);        s.clear();        set<int>::iterator it;        int l=1;        REPP(i,1,n)        {            it = s.find(a[i]);            if(it!=s.end())                s.erase(a[i]);            else                s.insert(a[i]);            it = s.find(b[i]);            if(it!=s.end())                s.erase(b[i]);            else                s.insert(b[i]);            if(s.empty())            {                printf("%d-%d",l,i);                if(i==n) printf("\n");                else printf(" ");                l=i+1;            }        }    }    return 0;}</span>

D水

找区间里有多少个偶数多少个奇数

/* ***********************************************Author        :angon************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)#define lld %I64d#define REP(i,k,n) for(int i=k;i<n;i++)#define REPP(i,k,n) for(int i=k;i<=n;i++)#define scan(d) scanf("%d",&d)#define scanl(d) scanf("%I64d",&d)#define scann(n,m) scanf("%d%d",&n,&m)#define scannl(n,m) scanf("%I64d%I64d",&n,&m)#define mst(a,k)  memset(a,k,sizeof(a))#define LL long long#define N 1005#define mod 1000000007inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int n,t;    scan(t);    while(t--)    {        scan(n);        int x1 = (n-1)/3;        int x2 = n/2;        int ans;        if(n&1)            ans = (n-x1+1)/2 + n - x2;        else            ans = (n-x1)/2 + n - x2;        printf("%d\n",ans);    }    return 0;}
G

这题困难在对于不懂打斯诺克的人来说理解题目有点困难。其实是一道水题,判断一下剩下可能得到的最多分数给另一个人他是不是也赢不了了。比赛的时候代码写丑了……

/* ***********************************************Author        :angon************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)#define lld %I64d#define REP(i,k,n) for(int i=k;i<n;i++)#define REPP(i,k,n) for(int i=k;i<=n;i++)#define scan(d) scanf("%d",&d)#define scanl(d) scanf("%I64d",&d)#define scann(n,m) scanf("%d%d",&n,&m)#define scannl(n,m) scanf("%I64d%I64d",&n,&m)#define mst(a,k)  memset(a,k,sizeof(a))#define LL long long#define N 1005#define mod 1000000007inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}int ball[N];int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T,n;    scan(T);    while(T--)    {        scan(n);        REPP(i,1,n) scan(ball[i]);        int a=0,b=0,x=15,t=27;        REPP(i,1,n)        {            if(ball[i]==0)            {                swap(a,b);                //continue;            }            a += ball[i];            if(x>1)            {                if(ball[i]==1) x--;                if(a > b + x*8 + t)                {                    printf("%d\n",i);                    //printf("%d %d %d\n",x,a,b);                    break;                }                if(ball[i]==1) x++;            }            else if(x==1)            {                if(a > b + t + (ball[i]==1?0:8))                {                    printf("%d\n",i);                    break;                }                if(ball[i]==1) t += ball[i+1];            }            else if(x==0)            {                t -= ball[i];                if(a > b + t)                {                    printf("%d\n",i);                    break;                }            }            if(ball[i]==1) x--;        }        if(a==b) printf("%d\n",n);    }    return 0;}
I 队友写的,应该也是水题

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <bitset>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())#define all(a) a.begin(), a.end()typedef pair<int, int> pii;typedef pair<long long, long long> pll;typedef vector<int> vi;typedef vector<long long> vll;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")char s[1000005];char ans[1000005];int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);int t;t = read();while(t--)    {        scanf("%s",s);        int len = strlen(s);        stack<char> a;        stack<char> b;        for(int i=0;i<len;i++)        {            if(s[i]=='<')            {                if(!a.empty())                {                    char x = a.top();                    a.pop();                    b.push(x);                }            }            else if(s[i]=='>')            {                if(!b.empty())                {                    char x = b.top();                    b.pop();                    a.push(x);                }            }            else if(s[i]=='-')            {                if(!a.empty())                {                    a.pop();                }            }            else                a.push(s[i]);        }        while(!a.empty())        {            char x = a.top();            a.pop();            b.push(x);        }        while(!b.empty())        {            printf("%c",b.top());            b.pop();        }        printf("\n");    }return 0;}

J,应该是这几题里比较有难度的……一开始我卡在贪心+拓扑排序上不能自拔……把每个串缩成一个点,给有重叠的点连上边。每次选当前重叠最多的串删去,并且和它相连的边度数-1,直到所以点度数都为0就结束。但是贪心做法应该是有错的,不过我暂时还没发现为什么会错,哭。最后看了题解还是二分图最大匹配,虽然之前也往这面想了,但是抵不住贪心的诱惑啊((。

因为只有行和列会冲突重叠,所以冲突的时候,连一条行指向列的边;在“冲突图”里,让每个行尽量的匹配更多的列,即求图的最大匹配,答案即为h+v-最大匹配数。

/* ***********************************************Author        :angon************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)#define lld %I64d#define REP(i,k,n) for(int i=k;i<n;i++)#define REPP(i,k,n) for(int i=k;i<=n;i++)#define scan(d) scanf("%d",&d)#define scanl(d) scanf("%I64d",&d)#define scann(n,m) scanf("%d%d",&n,&m)#define scannl(n,m) scanf("%I64d%I64d",&n,&m)#define mst(a,k)  memset(a,k,sizeof(a))#define LL long long#define N 2005#define mod 1000000007inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}char mp[N][N],s[N];int belong[N][N],h,v;struct Edge{    int v,next;}edge[N*N*2];int head[N],tot;void addedge(int u,int v){    edge[tot].v=v; edge[tot].next=head[u]; head[u] = tot++;}int vis[N],link[N];bool dfs(int u){    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(!vis[v])        {            vis[v]=1;            if(link[v] == -1 || dfs(link[v]))            {                link[v]=u;                return 1;            }        }    }    return 0;}int hungry(int n){    int res=0;    mst(link,-1);    for(int i=1;i<=n;i++)    {        mst(vis,0);        if(dfs(i)) res++;    }    return res;  //res即为二分图最大匹配数}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int t,x,y;    scan(t);    while(t--)    {        mst(belong,0);  mst(mp,0);        mst(head,-1);   tot=0;        scann(h,v);        int k=1;        for(k;k<=h;k++)        {            scann(x,y);scanf("%s",s);            int len = strlen(s);            for(int i=0,j=x;i<len;i++,j++)            {                mp[y][j] = s[i];                belong[y][j] = k;            }        }        for(k;k<=h+v;k++)        {            scann(x,y); scanf("%s",s);            int len = strlen(s);            for(int i=0,j=y;i<len;i++,j++)            {                if(mp[j][x]=='0' || mp[j][x]==s[i])                    continue;                addedge(belong[j][x],k);            }        }        printf("%d\n",h+v-hungry(k));    }    return 0;}


L 队友写的

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <bitset>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())#define all(a) a.begin(), a.end()typedef pair<int, int> pii;typedef pair<long long, long long> pll;typedef vector<int> vi;typedef vector<long long> vll;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")char s[5000];int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);while(~scanf("%s",s))    {        double ans = 0;        int len = strlen(s);        int L = 0,R = len-1;        while(L<R)        {            while(s[L]=='0')            {                L++;            }            while(s[R]=='1')            {                R--;            }            if(L>=R)break;            double res = (R-L) * 1.0;            ans += sqrt(res);            L++;            R--;        }        printf("%.12f\n",ans);    }return 0;}





0 0
原创粉丝点击