2020-2-4赛

贪心
问题 J: Megalomania
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes Ai units of time for Kizahashi to complete Job i. The deadline for Job i is time Bi, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

·All values in input are integers.

·1≤N≤2×105

·1≤Ai,Bi≤109(1≤i≤N)
输入
Input is given from Standard Input in the following format:
N
A1 B1
.
.
.
AN BN

输出
If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.
样例输入
Copy
【样例1】
5
2 4
1 9
1 8
4 9
3 12
【样例2】
3
334 1000
334 1000
334 1000
【样例3】
30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720
样例输出 Copy
【样例1】
Yes
【样例2】
No
【样例3】
Yes
提示
样例1解释:

He can complete all the jobs in time by, for example, doing them in the following order:

·Do Job 2 from time 0 to 1.

·Do Job 1 from time 1 to 3.

·Do Job 4 from time 3 to 7.

·Do Job 3 from time 7 to 8.

·Do Job 5 from time 8 to 11.
Note that it is fine to complete Job 3 exactly at the deadline, time 8.
样例2解释:
He cannot complete all the jobs in time, no matter what order he does them in.

题目大意:输入n事件,事件i需要Ai个小时做完,截至时间为Bi。求能不能把这些事情做完。。。
思路:第一,输入所有数据,用数组和结构体都行,数据量不算复杂;第二,自然而然想到排序,先按截至日期小的在前,再按花费时间小的在前;第三,算是一种贪心的做法,用一个time记录从第一件事情到第i件事情花的总时间,然后比较time和对应的第i件事情的endline,主要是这道题没有start和end限制,所以很好做

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int use;
    int ed;
}A[200005];
bool cmp(struct node x,struct node y)
{
    if(x.ed==y.ed) return x.use<y.use;
    return x.ed<y.ed;
}
int main()
{
   int n;
   scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {
       scanf("%d %d",&A[i].use,&A[i].ed);
   }
   sort(A+1,A+1+n,cmp);
   long long int time=0;
   for(int i=1;i<=n;i++)
   {
      time+=A[i].use;
      if(time>A[i].ed)
      {
          printf("No");
          return 0;
      }
   }
   printf("Yes");
    return 0;
}

看似是排列组合问题,实则是dp
问题 E: NH序列
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
一个长度为k的整数序列b1,b2,…,bk(1≤b1≤b2≤…≤bk≤N)称为“好序列”当且仅当后一个数是前一个数的倍数,即bi+1是bi的倍数对任意的i(1≤i≤k-1)成立。
给定N和k,请算出有多少个长度为k的“好序列”,答案对1000000007取模。

输入
输入共1行,包含2个用空格隔开的整数N和k。
输出
输出共1行,包含一个整数,表示长度为k的“好序列”的个数对1000000007取模后的结果。
样例输入 Copy
3 2
样例输出 Copy
5
提示
“好序列”为:[1,1],[1,2],[1,3],[2,2],[3,3]。

对于40%的数据,1≤N≤30,1≤k≤10。
对于100%的数据,1≤N≤2000,1≤k≤2000。

排列组合类题:一般是不会重复,而且很明显,而此题,是dp
,积累不够造成的失误

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,kk,A[2005][2005];//[个数][最大值]
//加自己,所以不是排列组合,而且排列组合会崩
int main()
{
  scanf("%d %d",&n,&kk);
  for(int i=1;i<=n;i++) A[1][i]=1;
  for(int i=1;i<=kk;i++)
  {
      for(int j=1;j<=n;j++)
      {
          for(int k=1;k*j<=n;k++)//***
          {
              A[i][j*k]+=A[i-1][j];
              A[i][j*k]%=1000000007;
          }
      }
  }
  long long int ans=0;
  for(int i=1;i<=n;i++)ans=(ans+A[kk][i])%1000000007;
  printf("%lld",ans);
    return 0;
}

大数,gcd

问题 I: Anti-Division
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints
·1≤A≤B≤1018
·1≤C,D≤109
·All values in input are integers.

输入
Input is given from Standard Input in the following format:

A B C D

输出
Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.
样例输入 Copy
【样例1】
4 9 2 3
【样例2】
10 40 6 8
【样例3】
314159265358979323 846264338327950288 419716939 937510582
样例输出 Copy
【样例1】
2
【样例2】
23
【样例3】
532105071133627368
提示
样例1解释:5 **and 7 satisfy the condition.

做过类题,所以很快有思路了,但是gcd那个部分可能有问题
gcd相关知识
第一个是错误的

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,cnt3=0,cnt1=0,cnt2=0,cnt=0;
    scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
    cnt1=b/c-a/c;
    cnt2=b/d-a/d;
    long long int n=c,m=d,k;
    while(1)
    {
        k=n%m;
        n=m;
        m=k;
        if(n%m==0) break;
    }
    long long int ans=0;
    cnt3=b/(c/m*d)-a/(c/m*d);
    cnt=b-a+1-cnt1-cnt2+cnt3;
printf("%lld",cnt);
// printf("\n%lld",cnt1);
//    printf("\n%lld",cnt2);
//     printf("\n%lld",cnt3);
        return 0;
}

下面这个是正确的~

#include<bits/stdc++.h>
using namespace std;
 
long long int  gcd(long long int  a,long long int  b)
{
 return a == 0 ? b : gcd(b % a, a);
}
int main()
{
    long long int a,b,c,d,cnt3=0,cnt1=0,cnt2=0,cnt=0;
    scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
    cnt1=b/c-a/c;
    if(a%c==0)
        cnt1++;
    cnt2=b/d-a/d;
    if(a%d==0)
        cnt2++;
 
    cnt3=b/(c*d/gcd(c,d))-a/(c*d/gcd(c,d));
    if(a%(c*d/gcd(c,d))==0)
        cnt3++;
    cnt=b-a+1-cnt1-cnt2+cnt3;
    printf("%lld",cnt);
// printf("\n%lld",cnt1);
// printf("\n%lld",cnt2);
// printf("\n%lld",cnt3);
    return 0;
}
发布了32 篇原创文章 · 获赞 1 · 访问量 1348

猜你喜欢

转载自blog.csdn.net/QXK_Jack/article/details/104174179