codeforce 224

博主回顾了参加算法竞赛的经历,从贪心算法、排序题到递归二分法和离线数据结构查询,分享了解题过程和遇到的问题。比赛期间因网络问题导致时间紧张,最终未能完全解决所有问题。通过分析,博主总结了算法竞赛中常见的策略和技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

好久没写csdn的博客了,自从今年的亚洲赛完就一直没有敲过有关算法方面的程序,发现现在都有点生疏了。昨天参加了场CF,惨败。。。

第一题,水贪心,没仔细读题,结果当成是博弈DP,敲完果断贡献了个WA。

第二题,水题,把数存数组里排序,左右扫一遍就OK了。

第三题,一直题意没弄明白,后来问了学妹题意,是个递归二分,开始敲代码,还算比较顺利,结果MLE了,数爆int了,结果就跪了。后来改long long 果断过了。

第四题,没看。

第五题,感觉是离线数据结构查询,不过没想太明白也没太多时间就没动,今天看了下,完全是可以做的。

开始没多久,中途断网近半小时,顿时好无语,加上不熟,果断时间不够,给跪了。

第三题,代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#define LL long long
using namespace std;
const int mm = 1e5+9;
//map<LL,LL>mp;
class ppp
{
public:
  LL l,r;
  LL typ;
  LL a,b;
}f[mm];
int n;
LL query(LL x)
{
  LL l = 0,r = n-1,mid;
  while(l<=r)
  {
    mid = (l+r)/2;
    if(f[mid].l<=x && f[mid].r>=x)
    {
      if(f[mid].typ == 1)
        return f[mid].a;//mp[x];
      else
      {
        int num = x-f[mid].l+1;
        num = num%f[mid].a;
        if(num == 0)
          num = f[mid].a;
        return query(num);
      }
    }
    else if(f[mid].l>x)
    {
      r = mid -1;
    }
    else if(f[mid].r<x)
    {
      l = mid + 1;
    }
  }
}
int main()
{
    while(~scanf("%d",&n))
    {
//      mp.clear();
      LL kai = 1;
      for(int i=0;i<n;++i)
      {
        int typ,a,b;
        scanf("%d",&typ);
        if(typ == 1)
        {
          scanf("%d",&a);
          f[i].l = kai; f[i].r = kai;
          f[i].a = a;
          f[i].typ = typ;
//          mp[kai] = a;
          ++kai;
        }
        else
        {
          scanf("%d%d",&a,&b);
          f[i].l = kai; f[i].r = kai + (LL)a*b-1;
          kai = f[i].r + 1;
          f[i].typ = typ;
          f[i].a = a; f[i].b = b;
        }
      }
//      for(int i=0;i<n;++i)
//      {
//        printf("%d %d %d %d\n",f[i].l,f[i].r,f[i].a,f[i].b);
//      }
      int m;
      LL qu;
      scanf("%d",&m);
      for(int i=0;i<m;++i)
      {
        scanf("%I64d",&qu);
        printf("%I64d ",query(qu));
      }
      printf("\n");
    }
    return 0;
}

第四题

#include <iostream>
#include <cstdio>
#include <set>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#define VI vector<int>
#define PB push_back
#define ll(x) (1<<x)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define DWN(i,a,b) for(int i=a;i>=b;--i)
#define clr(f,z) memset(f,z,sizeof(f))

using namespace std;

const int mm = 7009;

int d[mm*311],l[mm],r[mm],x[mm],vis[mm*311];
VI level[mm];
int main()
{
    d[0] = 0;
    FOR(i,1,111*mm-1)///i 对应右下一层的值
    {
        d[i] = d[i-1] + 1;
        if( (i&(i-1)) == 0)
            ++d[i];
    }
    int n,m;

    while(cin>>n>>m)
    {
        int res = 0,typ,id;
        clr(vis,0);
        FOR(i,0,mm-1)
        level[i].clear();
            FOR(i,1,m)
            {
                cin>>typ;
                if(typ == 1)
                {
                    cin>>id>>l[i]>>r[i]>>x[i];
                    level[id].PB(i);
                }
                else
                {
                    int t,v,ll,rr,ans;
                    cin>>t>>v;
//                    puts("uuu");
                    ++res; ans = 0;
                    ll = rr = v;
                    FOR(j,t,n)
                       {
                           int len = level[j].size()-1;
//                        FOR(k,0,level[j].size()-1) //BUG
                        FOR(k,0,len)
                        if(ll>r[ level[j][k] ] || rr < l[ level[j][k] ])
                             continue;
                        else
                        {
                            if(vis[ x[ level[j][k] ] ] != res)
                            {
                                vis[ x[ level[j][k] ] ] = res;
                                ++ans;
                            }
                        }
                         ll = d[ll-1] + 1;
                         rr = d[rr];
                       }
                    cout<<ans<<endl;
                }
            }
    }
    return 0;
}

/**
4 5
1 4 4 7 1
1 3 1 2 2
2 1 1
2 4 1
2 3 3
*/


第五题:

//在线线段树 265 MS
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#define lson (t<<1)
#define rson (t<<1|1)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define DWN(i,a,b) for(int i=a;i>=b;--i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)

using namespace std;
const int mm = 1e6+9;
char bracket[mm];
class Segment
{
public:
  int l,r,len,lbracket,rbracket;
}rt[mm*4];
void push(int t)
{
  int num = min(rt[lson].lbracket,rt[rson].rbracket);
  rt[t].len = rt[lson].len + rt[rson].len + num;
  rt[t].lbracket = rt[lson].lbracket + rt[rson].lbracket - num;
  rt[t].rbracket = rt[lson].rbracket + rt[rson].rbracket - num;
}
void build(int t,int l,int r)
{
  rt[t].l = l; rt[t].r = r;
  if(l == r)
  {
    rt[t].len = 0;
    rt[t].lbracket = rt[t].rbracket = 0;
    if(bracket[l-1] == '(')
        rt[t].lbracket++;
    else rt[t].rbracket++;
    return ;
  }
  int mid = (l+r)/2;
  build(lson,l,mid); build(rson,mid+1,r);
  push(t);
}
Segment query(int t,int l,int r)
{
    if(rt[t].l >= l && rt[t].r <= r)
      {
        return rt[t];
      }
    Segment ret = rt[t];
    ret.lbracket = 0;
    ret.rbracket = 0;
    ret.len = 0;
    if(rt[t].r < l || rt[t].l > r)
      return ret;
    else
    {
      Segment la = query(lson,l,r);
      Segment ra = query(rson,l,r);
      int num = min(la.lbracket,ra.rbracket);
      ret.len = la.len + ra.len + num;
      ret.lbracket = la.lbracket + ra.lbracket - num;
      ret.rbracket = la.rbracket + ra.rbracket - num;
      return ret;
    }

}
int main()
{

    while(~scanf("%s",bracket))
    {
        int len = strlen(bracket);
        build(1,1,len);
        int m,a,b;
        scanf("%d",&m);
        FOR(i,1,m)
        {
          scanf("%d%d",&a,&b);
          printf("%d\n",query(1,a,b).len*2);
        }
    }
  return 0;
}

/右离线版 93MS
//#include<cstdio>
//#include<cstring>
//#include<iostream>
//#include<cmath>
//#include<queue>
//#include<set>
//#include<vector>
//#include<string>
//#include<stack>
//#include<algorithm>
//#define PII pair<int,int>
//#define FOR(i,a,b) for(int i=a;i<=b;++i)
//#define DWN(i,a,b) for(int i=a;i>=b;--i)
//#define clr(f,z) memset(f,z,sizeof(f))
//#define ll(x) (1<<x)
//
//using namespace std;
//const int mm = 1e6+9;
//char bracket[mm];
//int sum[mm];
//
//int lowbit(const int x)
//{
//  return x&(-x);
//}
//
//void add(int*sum,int pos,const int num)
//{
//    for(int i=pos;i<mm;i+=lowbit(i))
//    {
//      sum[i] += num;
//    }
//}
//
//int query(int*sum,const int pos)
//{
//    int ans = 0;
//    for(int i=pos;i>0;i-=lowbit(i))
//      ans += sum[i];
//    return ans;
//}
//stack<int>st;
//vector<PII>q[mm];
//int ans[mm];
//int main()
//{
//
//    while(~scanf("%s",bracket))
//    {
//        int m,a,b;
//        scanf("%d",&m);
//        FOR(i,1,m)
//        {
//          scanf("%d%d",&a,&b);
//          --a; --b;
//          q[a].push_back(PII(b,i));
//        }
//        int len = strlen(bracket);
//        clr(sum,0);
//        DWN(i,len-1,0)
//        {
//          if(bracket[i] == ')')
//            st.push(i);
//          else
//          {
//            if(!st.empty())
//            {
//              add(sum,st.top(),1);
//              st.pop();
//            }
//          }
//          for(int j = 0;j<q[i].size();++j)
//          {
//            ans[ q[i][j].second ] = query(sum,q[i][j].first);
//          }
//        }
//      FOR(i,1,m)
//      printf("%d\n",ans[i]+ans[i]);
//    }
//  return 0;
//}



### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值