hihocoder50

http://hihocoder.com/contest/offers50/problem/
题目1 : 循环数组:
如果sum大于0,肯定存在,遍历一遍就OK了

n = int(raw_input())
a = map(int, raw_input().split(' '))
if sum(a)<=0:
    print -1
else:
    res, i, su = 0, 0, a[0]
    while i+1

题目2 : 座位问题
优先队列,定义排序规则,Python会TLE,哎,还得重新写Java了,C++都忘光了

import heapq

n,m,k = map(int, raw_input().strip().split(' '))
b=map(int, raw_input().split(' '))
b.sort()
q=[]
for i in range(m-1):
    if b[i+1]-b[i]>1:
        d = b[i+1]-b[i]-1
        heapq.heappush(q, (-(d/2), -(d+1)//2,b[i]+1,b[i+1]-1))
if b[0]>1:  heapq.heappush(q, (-(b[0]-1)/2, -(b[0])/2, 1, b[0]-1))
if b[-1]0:
    k-=1
    _,_,l,r=heapq.heappop(q)
    mid=(l+r)/2
    heapq.heappush(q, (-(mid-2-l)/2, -(mid-1-l)/2, l, mid-1))
    heapq.heappush(q, (-(r-mid-2)/2, -(r-mid-1)/2, mid+1, r))
    res.append(mid)
print('\n'.join(map(str, res)))
package l502;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt(), m=sc.nextInt(), k=sc.nextInt();
        int[] b = new int[m];
        for(int i=0; i pq = new PriorityQueue(n, new Comparator(){
            public int compare(int[] a1, int[] a2) {
                int d1=a1[1]-a1[0], d2=a2[1]-a2[0];
                if((d1+1)/2!=(d2+1)/2) return -(d1+1)/2+(d2+1)/2;
                if((d1+2)/2!=(d2+2)/2) return -(d1+2)/2+(d2+2)/2;
                return a1[0]-a2[0];
            }
        });
        
        for(int i=0; i1) {
                pq.add(new int[]{b[i]+1, b[i+1]-1});
            }
        if(b[0]>1) pq.add(new int[]{1, b[0]-1});
        if(b[m-1] l = new ArrayList();
        while(k-- > 0) {
            int[] t = pq.remove();
            int mid = (t[0]+t[1])/2;
            pq.add(new int[]{t[0], mid-1});
            pq.add(new int[]{mid+1, t[1]});
            l.add(mid);
        }
        
        for(int i:l)
            System.out.println(i);
    }
}

题目3 : 末尾有最多0的乘积
统计2,5的个数,然后DP,2和5之间是有牵连的,所以不能分2个DP数组来算,
dp[i][j][k]表示到i位置时,选了j个,有k个5的时候最多有多少个2,其实就是在枚举5的个数啦
仔细想想确实也符合逻辑,所以不要拘泥于之前的套路,跳出来,按照枚举的思路正常考虑一遍

package l503;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt(), m=sc.nextInt();
        int[]a=new int[n];
        for(int i=0;i0 && a[i]%2==0)  {a[i]/=2; a2[i]++;}
            while(a[i]>0 && a[i]%5==0) {a[i]/=5; a5[i]++;}
        }
        
        int s=0;
        for(int i:a5) s+=i;
        
        int[][][] dp = new int[n][m+1][s+1];
        // 不合规则的最好开始就设为一个负数,不要在循环里面额外判断
        for(int[][] dp1: dp) for(int[] dp2: dp1) Arrays.fill(dp2, -99999999);
        dp[0][0][0] = 0;
        for(int k=0; k<=a5[0]; k++) dp[0][1][k]=a2[0];
        for(int i=1; i=0 && k-a5[i]>=0))
                        dp[i][j][k]=Math.max(dp[i][j][k], dp[i-1][j-1][k-a5[i]]+a2[i]);
                }
        
        int res = 0;
        for(int i=0; i

你可能感兴趣的:(hihocoder50)