题解

327 条题解

  • 0
    @ 2006-05-21 18:43:00

    注意要把起始值设为0

    如:a:array[0..1000] of longint;

  • 0
    @ 2006-04-29 21:30:27

    太简单了 !!!!

    只要 把区域排除 !!!!!

  • -1
    @ 2022-07-08 07:10:04

    #include <iostream>
    using namespace std;
    int A[10005]={0};
    int main() {
    int L,M;
    cin>>L>>M;
    for (int i=0; i<M; i++) {
    int a,b;cin>>a>>b;
    for (int j=a; j<=b; j++) {
    A[j]=1;
    }
    }
    int k=0,sum=0;
    for (; k<L+1; k++) {
    sum+=(1-A[k]);
    }
    cout<<sum;
    return 0;
    }

  • -1
    @ 2022-05-06 20:30:01

    #include<cstdio>
    #include<iostream>
    using namespace std;
    #include<cstring>
    //以上不解释
    int L,M;
    //变量名称与原题相同
    int cnt=0;
    //剩余树的数目
    int vis[10000+10];
    //用来判断树木有没有被访问过
    int main()
    {
    memset(vis,0,sizeof(vis));//初始化该数组
    cin>>L>>M;//标准输入,不解释
    for(int i=0;i<=L;i++)
    vis[i]=0;//从0这个树开始,将这条路上所有的树标记为未访问过
    for(int i=1;i<=M;i++)
    {
    int head,tail;//定义区间的头和尾
    cin>>head>>tail;
    for(int j=head;j<=tail;j++)
    if(vis[j]==0)
    vis[j]=1;//从这个区间的头和尾开始,将所有区间内的树标记为访问过
    }
    for(int i=0;i<=L;i++)
    {
    if(vis[i]==0)
    cnt++;//计算未被访问过的树的数目
    }
    cout<<cnt<<endl;
    return 0;
    }

  • -1
    @ 2022-03-03 20:12:43
    #include<iostream>//使用头文件
    using namespace std;
    int a[10001];//标记数组
    int main()
    {
        int l,m,i,j,left,right;
        cin>>l>>m;//l是路的长度,m是建地铁站的地方个数
        for(i=0;i<=l;i++) a[i]=1;//全部标记为1
        for(i=1;i<=m;i++)
        {
            cin>>left>>right;//输入左右
            for(j=left;j<=right;j++)
            {
                a[j]=0;//把不能种树的地方标记为0
            } 
        } 
        int cnt=0;
        for(i=0;i<=l;i++)
        {
            if(a[i]==1) cnt++;//如果有树,cnt++
        } 
        cout<<cnt;//输出cnt
        return 0;
    }
    
    

    这么漂亮的题解,难道不点个赞再走嘛??

  • -1
    @ 2021-12-11 21:07:41

    ......先模拟吧.......

    #include<bits/stdc++.h>
    using namespace std;
    bool li[10000];
    int main()
    {
       int l,m,k,o,ans=0;
       cin>>l>>m;
       for(int j=1;j<=m;j++)
       {
        cin>>k>>o;
        for(int i=k;i<=o;i++)li[i]=1;
       }
       for(int i=0;i<=l;i++)
       if(li[i]==0)
        ans++;
       cout<<ans<<endl;
    }//我是小蒟蒻
    
    

    加1点点优化

    1.终点 - 起点 = 要砍的棵树;

    2.把重叠的加回来;//留给你们吧

    好像变慢了哈......【狗头】

  • -1
    @ 2020-11-28 19:58:33

    右手就行好吗?
    貌似是个红提呀,
    #include<iostream>
    #include<cstdio>
    #define lch now<<1
    #define rch ((now<<1)+1)
    #define smid ((l+r)>>1)
    #define MAXN 40000
    using namespace std;
    struct sgt_node{
    int sum;
    int l,r;
    }sgt[MAXN];
    int l,m;
    void build(int now,int l,int r){
    sgt[now].l=l;
    sgt[now].r=r;
    if(l==r){
    sgt[now].sum = 1;
    return;
    }
    build(lch,l,smid);
    build(rch,smid+1,r);
    sgt[now].sum=sgt[lch].sum+sgt[rch].sum;
    }
    void update(int now,int l,int r,int x,int y){ if(l>y||r<x||sgt[now].sum==0) return;
    if(x<=l&&y>=r){

    sgt[now].sum=0;
    return;
    }
    update(lch,l,smid,x,y);
    update(rch,smid+1,r,x,y);
    sgt[now].sum = sgt[lch].sum+sgt[rch].sum;
    }
    int main()
    {
    int left, right;
    cin>>l>>m;
    build(1,1,l+1);
    for(int i=1;i<=m;i++){
    cin>>left>>right;
    update(1,1,l+1,left+1,right+1);
    }
    cout<<sgt[1].sum;
    return 0;
    }

  • -1
    @ 2020-08-15 15:47:48

    蒟蒻用的诡异贪心法orz

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int l,m,st=0,sum=0;
    struct qwq{int s,e;}out[110];
    int read()
    {
        int x=0;
        char ch=getchar();
        while(ch<'0'||ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
        return x;
    }
    bool cmp(qwq x,qwq y)
    {
        if(x.s==y.s) return x.e>y.e;
        else return x.s<y.s;
    }
    int main()
    {
        l=read();m=read();
        for(int i=1;i<=m;++i) {out[i].s=read();out[i].e=read();}
        sort(out+1,out+m+1,cmp);
        //for(int i=1;i<=m;++i) printf("%d %d\n",out[i].s,out[i].e);
        for(int i=1;i<=m;++i)
        {
            if(st<out[i].s) sum=sum+out[i].s-st;
            if(out[i].e>=st) st=out[i].e+1;
        }
        if(st<=l) sum=sum+l-st+1;
        printf("%d",sum);
        return 0;
    }
    
  • -1
    @ 2020-07-22 11:38:37

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    int l;
    int m;
    int z,y;
    int ans=0;
    cin>>l>>m;
    int tree[l];
    int area_left[m]={0};
    int area_right[m]={0};
    for(int i=0;i<m;i++){
    cin>>area_left[i]>>area_right[i];
    }
    for(int j=0;j<=l;j++){
    tree[j]=1;
    }
    for(int k=0;k<m;k++){
    z=area_left[k];
    y=area_right[k];
    for(z=z;z<=y;z++){
    if(tree[z]==1)tree[z]=0;
    }
    }

    for(int n=0;n<=l;n++){
    if(tree[n]==1)ans++;
    }
    cout<<ans<<endl;
    }

  • -1
    @ 2020-06-27 13:47:58
    #include<iostream>
    int line[10001] = { 0 };
    int main(void) {
        int L, M;
        scanf("%d %d", &L, &M);
        getchar();
        
        int b, e;
        for (int i = 0; i < M; i++) {
            scanf("%d %d", &b, &e);
            getchar();
            for (int t = b; t <= e; t++) {
                line[t] = 1;
            }
        }
        int c = 0;
        for (int i = 0; i <= L; i++) {
            if (line[i] == 0)
                c++;
        }
        printf("%d\n", c);
        return 0;
    }
    
  • -1
    @ 2020-03-17 22:26:21
    #include<iostream>
    using namespace std;
    
    int tree[100005];
    int main(){
        int L, M;
        cin >> L >> M;
        
        int left;
        int right;
        int count = 0;
        
        for(int i = 0 ; i < M ; ++i) {
            cin >> left >> right;   
            for(int i = left ; i <= right ; ++i) {
                tree[i] = 1;
            } 
        }
        
        for(int i = 0 ; i <= L ; ++i) {
            if(tree[i] == 0)
                count++;    
        }
        
        cout << count << endl;
        return 0;   
    }
    
  • -1
    @ 2020-03-01 12:01:23

    水到炸

    var
      a:array[1..10000] of 0..1;
      l,n,i,x,y,j,s:longint;
    begin
      readln(l,n);
      for i:=0 to l do a[i]:=1;
      for i:=1 to n do
        begin
          readln(x,y);
          for j:=x to y do a[j]:=0;
        end;
      for i:=0 to l do if a[i]=1 then inc(s);
      writeln(s);
    end.
    
  • -1
    @ 2020-02-09 11:11:28

    #include<stdio.h>
    bool s[10005];
    int main(){
    int l,m,j=0,a,b;
    scanf("%d%d",&l,&m);
    for(int i=0;i<m;i++){
    scanf("%d%d",&a,&b);
    for(int c=a;c<=b;c++) s[c]=1;
    }
    for(int i=0;i<=l;i++) if(!s[i]) j++;
    printf("%d",j);
    return 0;
    }

  • -1
    @ 2019-07-29 21:13:43

    给个行数很少的:
    #include<iostream>
    #include<memory.h>
    #include<algorithm>

    using namespace std;

    int main(){
    int L,M;
    cin>>L>>M;
    bool *tree = new bool[L+1];
    memset(tree,0,L+1);

    int start,end;
    for(int i=0;i<M;i++){
    cin>>start>>end;
    memset(tree+start,-1,end-start+1);
    }

    cout<<count(tree,tree+L+1,0)<<endl;
    return 0;
    }

  • -1
    @ 2019-06-17 15:21:08

    #include <iostream>
    using namespace std;
    int main(void)
    {
    int L,M;
    int M_begin[10000]={0},M_end[10000]={0};
    int tree[10001]={0};
    int i=0;
    cin>>L;
    cin>>M;
    for(i=0;i<=L;++i)tree[i]=1;
    i=0;
    while(i<M){
    cin>>M_begin[i];
    cin>>M_end[i];
    ++i;
    }
    for(i=0;i<M;++i){
    while(M_begin[i]<=M_end[i]){
    tree[M_begin[i]]=0;
    ++M_begin[i];
    }
    }
    M=0;
    for(i=0;i<=L;++i){
    if(tree[i])++M;
    }
    cout<<M;
    return 0;
    }

  • -1
    @ 2019-03-16 13:32:48
    #include<bits/stdc++.h>
    using namespace std;
    
    struct Sec {
        int l, r;
        Sec(int l_, int r_): l(l_), r(r_) {}
        friend bool operator<(const Sec& s1, const Sec& s2) {
            return s1.l < s2.l;
        }
    };
    vector<Sec> sec;
    
    int main() {
        int L, m;
        cin >> L >> m;
        for (int i = 0; i < m; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            sec.push_back(Sec(a, b));
        } 
        sort(sec.begin(), sec.end());
        int ans = 0, l = 0, r = -1;
        for (auto s: sec) {
            if (s.l <= r) {
                if (s.r <= r) continue;
                r = s.r;
            }
            else {
                ans += r - l + 1;
                l = s.l;
                r = s.r;
            }
        }
        ans += r - l + 1;
        cout << L - ans + 1 << endl;
        return 0;
    }
    
    
  • -1
    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    using namespace std;  
    int l,m,x,y,num; bool b[10005];  
    int main(){  
        scanf("%d%d",&l,&m);  
        for (int i=1;i<=m;++i){  
            scanf("%d%d",&x,&y);  
            for (int j=x;j<=y;++j)  
              b[j]=true;  
        }  
        for (int i=0;i<=l;++i)  
          if (!b[i]) num++;  
        printf("%d",num);  
    }
    
  • -1
    @ 2018-12-30 14:10:03
    #Python
    L, M = map(int, input().split())
    
    treelist = set()
    
    for i in range(M):
        begin, end = map(int, input().split())
        tree = range(begin,end+1)
        treelist.update(tree)
        i += 1
    
    Llist = set(range(0, L + 1))
    Llist = Llist - treelist
    L = len(Llist)
    
    print(L)
    
    
  • -1
    @ 2018-11-03 21:07:55

    Pascal代码
    var
    l,m,i,j,x,y,s:longint;
    a:array[0..10000] of longint;
    begin
    read(l,m);
    for i:=1 to m do
    begin
    read(x,y);
    for j:=x to y do a[j]:=1;
    end;
    for i:=0 to l do
    if a[i]=0 then inc(s);
    write(s);
    end.

  • -1
    @ 2018-10-25 08:16:27

    #include<cstdio>
    #include<iostream>
    using namespace std;
    #include<cstring>
    //以上不解释
    int L,M;
    //变量名称与原题相同
    int cnt=0;
    //剩余树的数目
    int vis[10000+10];
    //用来判断树木有没有被访问过
    int main()
    {
    memset(vis,0,sizeof(vis));//初始化该数组
    cin>>L>>M;//标准输入,不解释
    for(int i=0;i<=L;i++)
    vis[i]=0;//从0这个树开始,将这条路上所有的树标记为未访问过
    for(int i=1;i<=M;i++)
    {
    int head,tail;//定义区间的头和尾
    cin>>head>>tail;
    for(int j=head;j<=tail;j++)
    if(vis[j]==0)
    vis[j]=1;//从这个区间的头和尾开始,将所有区间内的树标记为访问过
    }
    for(int i=0;i<=L;i++)
    {
    if(vis[i]==0)
    cnt++;//计算未被访问过的树的数目
    }
    cout<<cnt<<endl;
    return 0;
    }

信息

ID
1103
难度
4
分类
模拟 点击显示
标签
递交数
14316
已通过
6534
通过率
46%
被复制
54
上传者