2.2
2.2
[P2527 SHOI2001] Panda的烦恼 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
-
发现n小k大,复杂度为O(n * k),大概会吃到1e7的复杂度,姑且可以低空飘过
-
考虑运用set,java中的话就是TreeSet,自动提供排序和排重的功能
-
ok,那么先放入一个1,然后根据p依据放入set
-
那么以下有几个问题‘
-
- 1 WA,原因是没开long,ok
Set<Long> - 2 MLE, 原因是如果set开long,超出空间要求了,改进为只要超过2147483647就跳过,int范围$2^{31} - 1 $ ,那么问题解决
- 3 TLE,考虑到可能加入了许多已经超过k号的数,所以进行优化
- 1 WA,原因是没开long,ok
-
建议本题用cpp跑,java太慢了,容易被卡常数
#include<bits/stdc++.h>
using namespace std;
int n,k,f=1,a[105];long long mx,INF=2000000000;set<long long>s;
//f表示当前set中是否有k个数
int main()
{
scanf("%d%d",&n,&k),s.insert(1);
for(int i=1;i<=n;i++) scanf("%d",a+i),a[i]==1?(i--,n--):0;
sort(a+1,a+n+1),mx=1;//最大值变成当前唯一的一个1
for(int i=1;i<=k;i++)
{
long long t=*s.begin();s.erase(s.begin());
for(int i=1;i<=n&&t*a[i]<=INF;i++) if(f||a[i]*t<=mx) s.insert(t*a[i]),mx=max(mx,t*a[i]);
//首先,刚刚加的优化仍然有(在循环条件中)
//然后,这个判断条件就是刚刚说的那个break的反条件QAQ
f=((int)s.size()+i<=k);
//更新f,检查加入后是否set中有k个数
}
return printf("%lld\n",*s.begin()),0;
}
- 贴个90分java代码
package shuati;
import java.io.*;
import java.util.*;
public class Main implements Runnable {
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int n, k;
static int[] p = new int[105];
static int ans;
static TreeSet<Integer>set = new TreeSet<>();
static int INX = 2147483647;
public static void main(String[] args) {
new Thread(null, new Main(), "", 1 << 29).start();
}
public static int nextInt() throws IOException {
cin.nextToken();
return (int) cin.nval;
}
@Override
public void run() {
try {
n = nextInt(); k = nextInt();
for(int i = 1; i <= n; i++) p[i] = nextInt();
set.add(1);
int mx = 1;
boolean f = true;
for(int i = 1; i <= k; i++) {
int begin = set.first(); set.remove(set.first());
for(int j = 1; j <= n; j++) {
if((long)begin * p[j] > INX) continue;
if(f || p[j] * begin <= mx) {
set.add(p[j] * begin);
mx=Math.max(mx,begin * p[j]);
}
set.add(begin * p[j]);
}
f=((int)set.size() + i <= k);
}
cout.println(set.first());
cout.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Smallest number - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 爆搜就完了,还有就是可以全排列,因为运算符要按顺序,只用全排数字,一共就4个数字,只有24种可能性
import java.io.*;
import java.util.*;
public class Main implements Runnable {
// static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static Scanner cin = new Scanner(System.in);
// static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static long[] arr = new long[5];
static char[] t = new char[4];
static boolean[] str = new boolean[5];
static long ans = 1000000000000000L;
public static void main(String[] args) {
new Thread(null, new Main(), "", 1 << 29).start();
}
public static void dfs(int cnt) {
if (cnt == 4) {
for (int i = 1; i <= 4; i++) {
if (!str[i]) {
long s = arr[i];
ans = Math.min(s, ans);
return;
}
}
}
for(int i = 1; i <= 4; i++) {
if (!str[i]) {
for(int j = 1; j <= 4; j++) {
if (i == j) continue;
if (str[j]) continue;
str[i] = true;
long k = arr[j];
if (t[cnt] == '+') {
arr[j] += arr[i];
} else {
arr[j] *= arr[i];
}
dfs(cnt + 1);
arr[j] = k;
str[i] = false;
}
}
}
}
@Override
public void run() {
arr[1] = cin.nextInt();arr[2] = cin.nextInt();arr[3] = cin.nextInt();arr[4] = cin.nextInt();
t[1] = cin.next().charAt(0);t[2] = cin.next().charAt(0);t[3] = cin.next().charAt(0);
dfs(1);
cout.println(ans);
cout.flush();
}
}
浙公网安备 33010602011771号