5 条题解

  • 5
    @ 2023-12-10 12:34:47

    叮咚,鉴定结果为贪心,贪心策略:优先选结束时间造的。(课上有原题)

    #include <bits/stdc++.h>
    using namespace std;
    int n, m = -0x3f, ans; // n含义在题目中,m是用来记录开始时间的,目的是防止时间冲突,所以要把m设为极小值,ans是用来记录答案的
    struct T // 定义结构体,名字嘛——我随便取了一个
    {
        int begin, end; // begin是开始时间,end是结束时间
    }a[105]; // 有题目中可知,n的范围是1~100
    bool cmp(T x, T y) // cmp函数数,用来个结构体排序,排序目的是优先选结束时间早的
    {
        return x.end < y.end; // 自定义排序顺序的比较代码
    }
    int main() // 主函数
    {
        ios::sync_with_stdio(false); // 读入加速,没什么可讲吧
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i].begin >> a[i].end;
        // 上面是输入,没什么可讲吧,不会的话你学OI干啥啊?白学了
        sort (a + 1, a + n + 1, cmp); // 结构体排序,cmp功能见cmp中的注释
        for (int i = 1; i <= n; i++)
        {
            if (m <= a[i].begin) // 防止时间重叠
            {
                m = a[i].end; // 如果不重叠,那就把时间设为这个活动的开始时间
                ans++; // 如果能选,就把答案的计数器加一
            }
        }
        cout << ans << endl; // 输出,不讲
        return 0; // 好习惯
    } // 具体思路见注释
    

    End,撒花

    • 3
      @ 2024-2-15 21:18:02
      #include <bits/stdc++.h> 
      using namespace std;
      #define ll long long
      ll n,ans,now;
      struct A{
          ll l,r;
      }a[101];
      bool cmp(A x,A y){
          return x.r<y.r;
      }
      int main(){
          ios::sync_with_stdio(0);
          cin>>n;
          for(ll i=1;i<=n;i++){
              cin>>a[i].l>>a[i].r;
          }
          sort(a+1,a+n+1,cmp);
          for(ll i=1;i<=n;i++){
              if(a[i].l>=now){
                  now=a[i].r;
                  ans++;
              }
          }
          cout<<ans;
          return 0;
      }
      
      • 2
        @ 2022-7-1 17:29:13

        简单的贪心。

        思路就是按结束时间进行排序,越早结束排的越前面,然后计算可以安排的活动有几个即可。

        你们最爱的 AC 代码( 10ms):

        //无注释版
        #include <iostream>
        #include <cstdio>
        #include <algorithm>
        using namespace std;
        
        long long n , sum;
        
        struct sb{
            int bt , et;
        }dsb[100001];
        
        bool cmp(sb a,sb b){
        	return a.et < b.et;
        }
        
        int main(){
        	int t = -1;
        	cin >> n;
        	for(int i = 0;i < n;i++) cin >> dsb[i].bt >> dsb[i].et;
        	sort(dsb,dsb + n,cmp);
        	for(int i = 0;i < n;i++){
        		if(dsb[i].bt >= t){
        			sum++;
        			t = dsb[i].et;
        		}
        	}
        	printf("%d",sum);
        }
        

        轻松 AC 这道非常 㵘 的题,我发现总时间居然是 10ms ,这也太慢了吧。

        因此,我把 sort 替换成了手写快排,然后时间就被我优化到了 4ms。

        优化后 AC 代码( 4ms ):

        #include <cstdio> //printf
        using namespace std;
        
        int n , ans;
        int t = -1; //上一个活动的结束时间 
        
        struct QWQ{
            int bt , et;
            //起始时间,结束时间 
        }qwq[100005];
        
        void qsort(int l , int r){ //用二分优化的冒泡排序(快排) 
            int mid = qwq[(l + r) >> 1].et;
            // x >>= 1 位运算 等同于  x /= 2
            int i = l , j = r;
            do{
                while(qwq[i].et < mid) i++;
                while(qwq[j].et > mid) j--;
                if(i <= j){
                	QWQ t = qwq[i];
                	qwq[i] = qwq[j];
                	qwq[j] = t;
                    i++;
                    j--;
                }
            }while(i <= j);
            if(l < j) qsort(l , j); //递归 
            if(i < r) qsort(i , r); 
        }
        
        int main(void){
        	scanf("%d" , &n);
        	for(register int i = 0;i < n;i++)  //循环加速 
        		scanf("%d%d" , &qwq[i].bt , &qwq[i].et);
        	qsort(0 , n - 1); //快排 
        	for(register int i = 0;i < n;i++){
        		if(qwq[i].bt >= t){
        			ans++;
        			t = qwq[i].et;
        		}
        	}
        	printf("%d" , ans);
        }
        
        • -2
          @ 2022-8-23 15:17:41
          struct hh
          {
              int b,e;
          }a[1000005];
          bool cmp(hh x1,hh x2)
          {
              return x1.e<x2.e;
          }
          sort(a+1,a+n+1,cmp);
          int t=a[1].e;
          for (int i=2;i<=n;i++)
          {
              if (a[i].b>=t)
              {
                  t=a[i].e;
                  ans++;
              }
          }
          
          • -6
            @ 2022-4-24 16:14:17

            鼓励大家写题解,但注意题解格式。

            给代码两端加上这个会舒服一些

            ```cpp

            你的代码

            ```

            </span>

            这个点在键盘的左上角tab上面那个键,注意切换输入法

            #include<iostream>
            using namespace std;
            int main()
            {
                int n;
                cin>>n;//这是一个注释
                return 0;
            } 
            

            请注意严禁抄袭题解,写题解不要只放代码,需加上你的思路或代码注释。

            抄袭题解一经发现直接取消成绩。

            • 1

            信息

            ID
            369
            时间
            1000ms
            内存
            16MiB
            难度
            2
            标签
            递交数
            276
            已通过
            176
            上传者