Codeforces Round #287 (Div. 2) 解题报告 A.B.C.D.E

这次的CF挺水的,当时B题犯了一个很SB的错误,浪费了好多时间,所以D和E也没来得及看。sad,。。

A - Amr and Music

水题,从小的开始选。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
struct node {
        int x, n;
} fei[400];
int cmp(node f1, node f2)
{
        return f1.x<f2.x;
}
int main()
{
        int n, m, i, sum=0, j, pos;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++) {
                scanf("%d",&fei[i].x);
                fei[i].n=i+1;
        }
        sort(fei,fei+n,cmp);
        pos=n-1;
        for(i=0; i<n; i++) {
                if(sum+fei[i].x>m) {
                        pos=i-1;
                        break;
                } else {
                        sum+=fei[i].x;
                }
        }
        printf("%d\n",pos+1);
        for(j=0; j<=pos; j++) {
                printf("%d ",fei[j].n);
        }
        return 0;
}

B - Amr and Pins

只要距离小于2*r,那么肯定能找到一个点可以正好将圆心移到目标点。所以只要看距离是2*r的多少倍就可以了。该死的精度。。取10^-9就挂了。。换成别的都过了。。无语。。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-11;
int main()
{
        LL r, x1, y1, x2, y2;
        LL a;
        double d, c;
        while(scanf("%I64d%I64d%I64d%I64d%I64d",&r,&x1,&y1,&x2,&y2)!=EOF){
                        r*=2;
                d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                c=d/r;
                a=(LL)c;
                if(c==a){
                        printf("%I64d\n",a);
                }
                else
                        printf("%I64d\n",a+1);
        }
        return 0;
}

C - Guess Your Way Out!

我的做法是先建树,记录到目标点的最短路径,然后再从根节点出发开始走,如果下一步偏离了路径,那么就加上下一步的所有子树节点的个数。(因为它肯定会遍历完所有子树然后返回)

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
LL ans, c, num[100], cnt, p[100], H;
void update(LL n, LL l, LL r, LL rt)
{
        num[cnt++]=rt;
        if(l==r) return ;
        LL mid=l+r>>1;
        if(n<=mid) update(n,lson);
        else update(n,rson);
}
void dfs(int f, int h, LL l, LL r, LL rt)
{
        if(l==r) return ;
        LL next;
        if(f==-1) next=rt<<1;
        else next=rt<<1|1;
        if(next!=num[h+1]) {
                ans+=p[H-h-1];
                f=-f;
        }
        c>>=1;
        LL mid=l+r>>1;
        if(f==-1) {
                dfs(-f,h+1,lson);
        } else {
                dfs(-f,h+1,rson);
        }
}
void init()
{
        p[0]=1;
        for(int i=1; i<=60; i++) {
                p[i]=p[i-1]+((LL)1<<i);
                //printf("%I64d\n",p[i]);
        }
}
int main()
{
        LL n, i;
        while(scanf("%I64d%I64d",&H,&n)!=EOF) {
                ans=0;
                c=(LL)1<<H;
                cnt=0;
                init();
                update(n-1,0,c-1,1);
                /*for(i=0;i<cnt;i++){
                        printf("%I64d\n",num[i]);
                }*/
                dfs(-1,0,0,c-1,1);
                printf("%I64d\n",ans+H);
        }
        return 0;
}

D - The Maths Lecture

水题啊。。。普通的数位DP而已,用dp[i][j][k]记录第i位(从低位开始数),模为j时,k记录是否已经存在模为0的后缀的状态时的个数。因为要算后缀,所以应该从低位向高位开始搜。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
int mod, k, n;
const int INF=0x3f3f3f3f;
const double eqs=1e-6;
LL dp[1002][101][3], mo[1002];
LL dfs(int cnt, int mods, int in, int zero)
{
        if(cnt==-1) return in;
        if(zero&&dp[cnt][mods][in]!=-1) return dp[cnt][mods][in];
        int l=(cnt==0), i, m, z;
        LL ans=0;
        for(i=l;i<=9;i++){
                m=(mods+i*mo[n-cnt])%k;
                if(i||zero) z=1;
                else z=0;
                ans+=dfs(cnt-1,m,in||(m==0&&z),z);
        }
        ans%=mod;
        if(zero)    {
                        dp[cnt][mods][in]=ans;
        }
        return ans;
}
void init()
{
        int i;
        mo[1]=1%k;
        for(i=2;i<=1000;i++){
                mo[i]=(mo[i-1]*10)%k;
        }
}
int main()
{
        memset(dp,-1,sizeof(dp));
        scanf("%d%d%d",&n,&k,&mod);
        init();
        printf("%I64d\n",dfs(n-1,0,0,0));
        return 0;
}

E - Breaking Good

还是水题。。求一次最短路,将距离标记成一个比较大的数,然后将花费标记成1,这样就能找到了最短而且花费最短的路径了。然后再根据状态标记找到需要改动的路径即可。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
int mod, k, n;
const int INF=0x3f3f3f3f;
const double eqs=1e-6;
int head[110000], cnt, vis[110000], dd[110000], pre[110000], c[110000];
struct node
{
        int u, v, w, next, f, s;
}edge[210000];
void add(int u, int v, int w, int f)
{
        edge[cnt].v=v;
        edge[cnt].w=w;
        edge[cnt].f=f;
        edge[cnt].next=head[u];
        head[u]=cnt++;
}
void spfa()
{
        queue<int>q;
        memset(vis,0,sizeof(vis));
        q.push(1);
        dd[1]=0;
        while(!q.empty()){
                int u=q.front();
                q.pop();
                vis[u]=0;
                for(int i=head[u];i!=-1;i=edge[i].next){
                        int v=edge[i].v;
                        if(dd[v]>dd[u]+edge[i].w){
                                dd[v]=dd[u]+edge[i].w;
                                pre[v]=u;
                                if(!vis[v]){
                                        vis[v]=1;
                                        q.push(v);
                                }
                        }
                }
        }
}
void init()
{
        memset(head,-1,sizeof(head));
        cnt=0;
        memset(dd,INF,sizeof(dd));
        memset(pre,-1,sizeof(pre));
}
int main()
{
        int n, m, i, u, v, w, j, sum=0, cnt1=0;
        scanf("%d%d",&n,&m);
        init();
        while(m--){
                scanf("%d%d%d",&u,&v,&w);
                add(u,v,10000+1-w, w);
                add(v,u,10000+1-w, w);
        }
        spfa();
        for(i=n;i!=-1;i=pre[i]){
                c[cnt1++]=i;
        }
        for(i=1;i<=n;i++){
                for(j=head[i];j!=-1;j=edge[j].next){
                        edge[j].s=0;
                }
        }
        for(i=cnt1-1;i>=1;i--){
                u=c[i];
                for(j=head[u];j!=-1;j=edge[j].next){
                        int v=edge[j].v;
                        if(v==c[i-1]){
                                edge[j].s=1;
                                edge[j^1].s=1;
                                //printf("%d %d %d\n",u,v,edge[j].w);
                        }
                }
        }
        for(i=1;i<=n;i++){
                for(j=head[i];j!=-1;j=edge[j].next){
                        if(edge[j].s+edge[j].f==1){
                                edge[j^1].f=1-edge[j^1].f;
                                //printf("%d %d %d %d\n",i,edge[j].v,edge[j].s,edge[j].w);
                                sum++;
                        }
                }
        }
        printf("%d\n",sum);
        for(i=1;i<=n;i++){
                for(j=head[i];j!=-1;j=edge[j].next){
                        if(edge[j].s+edge[j].f==1){
                                printf("%d %d %d\n",i,edge[j].v,edge[j].s);
                        }
                }
        }
        return 0;
}
时间: 2024-07-31 21:27:31

Codeforces Round #287 (Div. 2) 解题报告 A.B.C.D.E的相关文章

Codeforces Round #287 (Div. 2) 解题报告

好久没写题了,底下代码都比较糟糕,将就着看吧.. 507A  Amr and Music 要学最多的乐器,所以我们贪心选择时间花费少的.注意这里可以直接用pair,就不必写struct的compare函数了 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 struct ins{ 7 int v, id; 8 } a[110]; 9 bool c

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #262 (Div. 2)解题报告

详见:http://robotcator.logdown.com/posts/221514-codeforces-round-262-div-2 1:A. Vasya and Socks   http://codeforces.com/contest/460/problem/A 有n双袜子,每天穿一双然后扔掉,每隔m天买一双新袜子,问最多少天后没有袜子穿.. 简单思维题:以前不注重这方面的训练,结果做了比较久,这种题自己边模拟边想.不过要多考虑trick ```c++ int main(){ i

Codeforces Round #616 (Div. 2)解题报告

Codeforces Round #616 (Div. 2)解题报告 A. Even But Not Even 找两个奇数就行了. #include<bits/stdc++.h> using namespace std; void solve() { int n; string s; cin >> n >> s; string ans = ""; for(int i = 0; i < n; i++) { if(int(s[i] - '0')%2

Codeforces Round #479 (Div. 3)解题报告

题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直接写,手速题,没啥好说的 B. Two-gram 题意 求出现次数最多的连续两个字符 还是签到题,我居然很麻烦地用了map,= =算了,思路畅通都无所谓了 #include <iostream> #include<stdio.h> #include<algorithm> #

Codeforces Round #515 (Div. 3) 解题报告(A~E)

题目链接:http://codeforces.com/contest/1066 A题: 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在路上从l到r处停着别的火车,它挡着Vova的视线使他看不到灯笼.给定L,v,l,r求Vova能看到的灯笼数. 分析:从1到x上所有的灯笼数量为x/v个.则路上所有的灯笼数为L/v个,被挡住的则为 r/v - (l-1)/v 个,相减即为答案. #include<iostream> #include<cstdio> #inc

Codeforces Round #401 (Div. 2)解题报告

A - Shell Game 1 #include <iostream> 2 #include<bits/stdc++.h> 3 #include <stack> 4 #include <queue> 5 #include <map> 6 #include <set> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10

Codeforces Round #390 (Div. 2) 解题报告

时隔一个月重返coding…… 期末复习了一个月也不亏 倒是都过了…… 就是计组61有点亏 复变68也太低了 其他都还好…… 假期做的第一场cf 三道题 还可以…… 最后room第三 standing383简直人生巅峰…… 看楼上楼下都是两道题的 如果A题不错那么多估计能进前300了吧…… 这场倒是把之前两场的分加回来了 开头不错 这个假期争取紫名~ A.Lesha and array splitting 把给定的数组分割成几个区间 要求各个区间和不能为0 一开始没注意到分割之后的区间重新合成之

Codeforces Round #394 (Div. 2) 解题报告

开始补题,今天下午virtual参赛,过了ABC,D题因为一点小错误而没能在比赛时间中AC,时间到了之后几分钟就发现了问题所在,略有遗憾.之后一直冥思苦想E题,在提示下终于明白,真的是给这样组合题画风的题目跪了,只能说继续加油,扩展思路吧. A题 题目地址 只有奇偶数个数相差小于等于1时可以,需要特判不能使二者均为0的情况. 参考代码 1 #include<stdio.h> 2 #include<bits/stdc++.h> 3 #include <iostream>