ATcoder KEYENCE Programming Contest 2019 部分题解

比赛网址:https://keyence2019.contest.atcoder.jp/assignments

A - Beginning


Time limit : 2sec / Memory limit : 1024MB

Score : 100 points

Problem Statement

You are given four digits N1,N2,N3 and N4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

  • 0≤N1,N2,N3,N4≤9
  • N1,N2,N3 and N4 are integers.

Input

Input is given from Standard Input in the following format:

N1 N2 N3 N4

Output

If N1,N2,N3 and N4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.


Sample Input 1

Copy

1 7 9 4

Sample Output 1

Copy

YES

We can get 1974 by swapping N2 and N3.


Sample Input 2

Copy

1 9 7 4

Sample Output 2

Copy

YES

We already have 1974 before doing anything.


Sample Input 3

Copy

1 2 9 1

Sample Output 3

Copy

NO

Sample Input 4

Copy

4 9 0 8

Sample Output 4

Copy

NO

水题

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.StringTokenizer;
 
public class Main {
	
	 static Scanner in = new Scanner(System.in);
	 
   
	public static void main(String args[])throws IOException  {
    while(in.hasNext())
    {
    	
  
    	int []a=new int[4+1];
    	int []vis=new int[11];
    	for(int i=1;i<=4;i++)
    	{
          a[i]=in.nextInt();
          vis[a[i]]++;
    	}
    	if(vis[1]==1&&vis[7]==1&&vis[9]==1&&vis[4]==1)
     	System.out.println("YES");
    	else
    		System.out.println("NO");	
     	
    }
 
	}
	
 
	
}

B - KEYENCE String


Time limit : 2sec / Memory limit : 1024MB

Score : 200 points

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

Constraints

  • The length of S is between 7 and 100 (inclusive).
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a KEYENCE string, print YES; otherwise, print NO.


Sample Input 1

Copy

keyofscience

Sample Output 1

Copy

YES

keyence is an abbreviation of key of science.


Sample Input 2

Copy

mpyszsbznf

Sample Output 2

Copy

NO

Sample Input 3

Copy

ashlfyha

Sample Output 3

Copy

NO

Sample Input 4

Copy

keyence

Sample Output 4

Copy

YES

Submit

这题其实也很水,但是我在一种方法上卡死了,到底不知道那种方法哪里错了,结果比完赛后,换了一种方法就过了,这题的关键就是肯定要删除连续的len-7个字符。暴力枚举即可

AC代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
	
	 static Scanner in = new Scanner(System.in);
	 
   
	public static void main(String args[])throws IOException  {
   
    	
        String s=in.nextLine(); 
        int len1=s.length();
        String s1=s;
        String str="keyence";
        int len=str.length();
        int pos=s.indexOf(str);
        if(pos!=-1&&(pos==0||pos==len1-len))
        {
    		System.out.println("YES");
    		return ;
    	}
        int t=len1-len;
        for(int i=1;i

 

不知道怎么错的代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.StringTokenizer;
 
public class Main {
	
	 static Scanner in = new Scanner(System.in);
	 
   
	public static void main(String args[])throws IOException  {
    while(in.hasNext())
    {
    	
        String s=in.nextLine(); 
        int len1=s.length();
       
        String str="keyence";
        int len=str.length();
        int pos=s.indexOf(str);
        int pos3=s.lastIndexOf(str);
        if(pos==0||pos==len1-len)
        {
    		System.out.println("YES");
    		return ;
    		}
        for(int i=1;i

C - Exam and Wizard


Time limit : 2sec / Memory limit : 1024MB

Score : 400 points

Problem Statement

A university student, Takahashi, has to take N examinations and pass all of them. Currently, his readiness for the i-th examination is Ai, and according to his investigation, it is known that he needs readiness of at least Bi in order to pass the i-th examination.

Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.

For Takahashi, find the minimum possible number of indices i such that Ai and Ci are different, for a sequence C1,C2,…,CN that satisfies the following conditions:

  • The sum of the sequence A1,A2,…,AN and the sum of the sequence C1,C2,…,CN are equal.
  • For every iBiCi holds.

If such a sequence C1,C2,…,CN cannot be constructed, print −1.

Constraints

  • 1≤N≤105
  • 1≤Ai≤109
  • 1≤Bi≤109
  • Ai and Bi are integers.

Input

Input is given from Standard Input in the following format:

N
A1 A2 … AN
B1 B2 … BN

Output

Print the minimum possible number of indices i such that Ai and Ci are different, for a sequence C1,C2,…,CN that satisfies the conditions. If such a sequence C1,C2,…,CN cannot be constructed, print −1.


Sample Input 1

Copy

3
2 3 5
3 4 1

Sample Output 1

Copy

3

(A1,A2,A3)=(2,3,5) and (B1,B2,B3)=(3,4,1). If nothing is done, he cannot pass the first and second exams. The minimum possible number of indices i such that Ai and Ci are different, 3, is achieved when:

  • (C1,C2,C3)=(3,5,2)

Sample Input 2

Copy

3
2 3 3
2 2 1

Sample Output 2

Copy

0

In this case, he has to do nothing in order to pass all the exams.


Sample Input 3

Copy

3
17 7 1
25 6 14

Sample Output 3

Copy

-1

In this case, no matter what is done, he cannot pass all the exams.


Sample Input 4

Copy

12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212

Sample Output 4

Copy

5

如果a[i]b[i]的位置来互换,但有时后可以互换三下来解决问题。

我们先设sum为当a[i]

当a[i]>b[i]时来消除,当然越大消除的sum越多,所以,排序一下,消除到sum为0.

答案ans:a[i]

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	
	 static Scanner in = new Scanner(System.in);
	 
   
	public static void main(String args[])throws IOException  {
   
    	
         int n=in.nextInt();
         long[] a=new long[n+1];
         long[] b=new long[n+1];
         ArrayList list=new    ArrayList();
         long sum=0;
         for(int i=0;ib[i])
        		 list.add((a[i]-b[i]));
         }
         Collections.sort(list);
         int pos=list.size();
       
         for(int i=pos-1;i>=0;i--)
         {if(sum<=0) break;
        	 ans++;
        	 sum-=list.get(i).longValue();
        	 
         }
         if(sum<=0)
        	 
    		System.out.println(ans);
         else
        	 System.out.println(-1);
     	
    }

	
	
 
	
}

 

你可能感兴趣的:(好题,比赛题解,ATcoder比赛)