• 20201020


    1. zdx看电影系列

    排序,然后比较求出每个时间段可以看电影数量的最大值的,每一组数据得出答案后要将数组再次清零。

    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <queue>
    using namespace std;
    inline int read(){
      int x=0,f=1;
      char ch=getchar();
      while(ch>'9'||ch<'0'){
        if(ch=='-') f=-1;
        ch=getchar();
      }
      while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
       ch=getchar();
      }
      return x*f;
    }
    struct node {
    	int num,s,e;
    }a[1000005];
    int t,k;
    bool cmp1(node x,node y){
    	return x.e<y.e;
    }
    int main(){
    	t=read();
    	while(t--){
    		k=read();
    		for(int i=1;i<=k;i++){
           a[i].num=read(),a[i].s=read(),a[i].e=read();
    }
        sort(a+1,a+k+1,cmp1);
    		int t1=a[1].e,ans=1;
    		for(int i=2;i<=k;i++){
    			if(a[i].s>=t1) ans++,t1=a[i].e;
    }
        printf("%d
    ",ans);
    		for(int i=1;i<=k;i++){
           a[i].s=0,a[i].e=0;
    	}
    }
    	return 0;
    }
    

    另一个只需要一组数据且不需要输入电影编号。

    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <queue>
    using namespace std;
    inline int read(){
      int x=0,f=1;
      char ch=getchar();
      while(ch>'9'||ch<'0'){
        if(ch=='-') f=-1;
        ch=getchar();
      }
      while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
       ch=getchar();
      }
      return x*f;
    }
    struct node {
    	int s,e;
    }a[1000005];
    int t,k;
    bool cmp1(node x,node y){
    	return x.e<y.e;
    }
    int main(){
    		k=read();
    		for(int i=1;i<=k;i++){
           a[i].s=read(),a[i].e=read();
    }
        sort(a+1,a+k+1,cmp1);
    		int t1=a[1].e,ans=1;
    		for(int i=2;i<=k;i++){
    			if(a[i].s>=t1) ans++,t1=a[i].e;
    }
        printf("%d
    ",ans);
    	return 0;
    }
    

    2.智力大闯关

    只需要保证单位时间内所扣除的钱最少即可。(就是在尽可能后的位置去处理最大的惩罚)

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define scy(x)   freopen(x".in","r",stdin);  freopen(x".out","w",stdout);
    inline int read(){
      int x=0,f=1;
      char ch=getchar();
      while(ch>'9'||ch<'0'){
        if(ch=='-') f=-1;
        ch=getchar();
      }
      while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
      }
      return x*f;
    }
    struct XX {
       int a,b;
     }a[100100];
     bool f[100100];
      bool cmp(const XX &x,const XX &y) {
         if (x.b>y.b) return(true);
          return(false);
         }
         int main() {
           int m,n,i,j,x;
           bool ff;
           m=read(); n=read();
            for (i=1; i<=n; i++) a[i].a=read();
            for (i=1; i<=n; i++) a[i].b=read();
             sort(a+1,a+n+1,cmp);
             x=n;
              memset(f,true,sizeof(f));
               for (i=1; i<=n; i++)
               if (f[a[i].a]==true) {
                 f[a[i].a]=false;
               } else {
                  ff=false;
                  for (j=a[i].a; j>=1; j--) {
                     if (f[j]) {
                        f[j]=false; ff=true;
                         break;
                        }
                       }
                        if (ff==false) {
                           m-=a[i].b;
                           f[x]=false;
                            x--;
                           }
                          }
                      printf("%d",m);
                           return 0;
                         }
    

    3.整数区间

    • 把区间先按右端点从小到大排序,开两个变量来记录位置(保证x < y)
      *如果区间右端点大于y,则这个区间至少有两个数被放进集合中了,continue掉就好了
      *如果区间的右端点在x和y之间,把x变成y,y变成这个区间的左端点,同时ans++
      *如果区间的右端点小于x,x变成区间的左端点,y变成区间的右端点,同时ans+=2
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    inline int read(){
      int x=0,f=1;
      char ch=getchar();
      while(ch>'9'||ch<'0'){
        if(ch=='-') f=-1;
        ch=getchar();
      }
      while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
       ch=getchar();
      }
      return x*f;
    }
    struct node{
        int a;
        int b;
    }m[1000];
    bool cmp(node a,node b){
        if(a.a != b.a)
        {
            return a.a < b.a;
        }
        else
        return a.b < b.b;
    }
    int main(){
        int n,f1,f2,tot = 0;
      n=read();
        for(int i = 1;i <= n;i++)
        {
            m[i].a=read(),m[i].b=read();
        }
        sort(m + 1,m + n + 1,cmp);
        f1 = m[n].a;
        f2 = m[n].a + 1;
        tot += 2;
        for(int i = n - 1;i > 0;i--){
            if(m[i].b < f1){
                tot += 2;
                f1 = m[i].a;
                f2 = f1 + 1;
            }
            else if(m[i].b < f2){
                tot ++;
                f2 = f1;
                f1 = m[i].a;
            }
        }
        printf("%d",tot);
        return 0;
    }
    

    4.雷达安装

    以每个点为圆心,d为半径做圆,每个点可以在x轴上得到一个区间,这样就转换成了在x轴上选点,使得每个区间内都至少有一个点。
    我们按照右端点排序,如果下一个区间的右端点在前一个里面,我们可以跳过,否则我们就选择右端点最旁边的那个点(也就是那个点),显然这样更优。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    using namespace std;
    inline int read(){
      int x=0,f=1;
      char ch=getchar();
      while(ch>'9'||ch<'0'){
        if(ch=='-') f=-1;
        ch=getchar();
      }
        while(ch>='0'&&ch<='9'){
          x=(x<<1)+(x<<3)+(ch^48);
          ch=getchar();
        }
        return x*f;
      }
    
    const int maxn=1e3+7;
    int n,m,ans,d,x[maxn],y[maxn];
    double l[maxn],r[maxn],temp;
    
    struct node {
      double l,r;
    }a[maxn];
    
      double cmp(node a,node b){
        return a.r<b.r;
      }
    
    int main(){
        n=read(),d=read();
        for(int i=1;i<=n;i++){
          x[i]=read(),y[i]=read();
        }
        for(int i=1;i<=n;i++){
          a[i].l=x[i]-sqrt(d*d-y[i]*y[i]);
          a[i].r=x[i]+sqrt(d*d-y[i]*y[i]);
        }
        sort(a+1,a+1+n,cmp);
        for(int i=1;i<=n;i++){
          if(i==1){
            temp=a[i].r;
            ans++;
          }else{
            if(temp>=a[i].l) continue;
            else ans++;
            temp=a[i].r;
          }
        }
      printf("%d",ans);
      return 0;
    }
    
  • 相关阅读:
    偏函数
    装饰器
    排序
    匿名函数
    参数传递
    pass语句
    不定长函数
    通用函数
    不定长参数(元祖)
    不定长参数(字典)
  • 原文地址:https://www.cnblogs.com/scy-fisheep/p/13847388.html
Copyright © 2020-2023  润新知