前言
一大波原题来袭!!!(大雾 考得不太好吧0+100+0=100(T3数据出锅了
题意
有$n$组蛇,每一组蛇有$a_i$条蛇,你有一张网,需要将蛇全部抓住。一次抓一组蛇,因此每次要使网比当前组的蛇的数量大。你可以改变$k$次网的大小,问抓住所有蛇的总浪费空间的最小值? 对于$100 \% $的数据$1\leq n \leq 400,0\leq a_i \leq 10^6$
思路
考虑$DP$,设$f[i][j]$表示前$i$组蛇,改变了$j$次网的大小的最小浪费空间值。 那么$f[i][j]=min(f[i][j],f[k][j-1]+(i-k)*Max[k+1][i]-(sum[i]-sum[k]))$ 其中$sum$表示前缀和,$Max$表示区间最大值。 所以只要先维护前缀和与区间最大值即可。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stack> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> using namespace std;
#define re register
class Quick_Input_Output{ private: static const int S=1<<21; #define tc() (A==B&&(B=(A=Rd)+fread(Rd,1,S,stdin),A==B)?EOF:*A++) char Rd[S],*A,*B; #define pc putchar public: #undef gc #define gc getchar inline int read(){ int res=0,f=1;char ch=gc(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=gc();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=gc(); return res*f; } inline void write(int x){ if(x<0) pc('-'),x=-x; if(x<10) pc(x+'0'); else write(x/10),pc(x%10+'0'); } #undef gc #undef pc }I; #define File freopen("pa.in","r",stdin);freopen("pa.out","w",stdout); int n,K,a[410],f[410][410],sum[410],Max[410][410],ans=2e9; int main(){ File n=I.read();K=I.read();K++; for(int i=1;i<=n;i++) a[i]=I.read(); for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++){ if(i==j) Max[i][j]=a[i]; else Max[i][j]=max(Max[i][j-1],a[j]); } memset(f,63,sizeof(f));f[0][0]=0; for(int i=1;i<=n;i++){ for(int j=1;j<=K;j++){ for(int k=0;k<i;k++){ f[i][j]=min(f[i][j],f[k][j-1]+(i-k)*Max[k+1][i]-(sum[i]-sum[k])); } } } for(int i=1;i<=K;i++) ans=min(ans,f[n][i]); I.write(ans);putchar('\n'); }
|
题意
(你自己点链接看,(╯▔皿▔)╯
思路
乱搞贪心,比赛时候竟然过了?( ̄▽ ̄)” 贪心将其分组成一下形式:$(1),(2),(3),(4),…,(P),(P+1,P+2,P+3,…,N-1,N)$ 然后跑一遍求值。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stack> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> using namespace std;
#define re register #define int long long
class Quick_Input_Output{ private: static const int S=1<<21; #define tc() (A==B&&(B=(A=Rd)+fread(Rd,1,S,stdin),A==B)?EOF:*A++) char Rd[S],*A,*B; #define pc putchar public: #undef gc #define gc getchar inline int read(){ int res=0,f=1;char ch=gc(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=gc();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=gc(); return res*f; } inline void write(int x){ if(x<0) pc('-'),x=-x; if(x<10) pc(x+'0'); else write(x/10),pc(x%10+'0'); } #undef gc #undef pc }I; #define File freopen("pb.in","r",stdin);freopen("pb.out","w",stdout); int n,k,ans=2e18; inline int dis(int x,int y){ return (2019201913*x%2019201997+2019201949*y%2019201997)%2019201997; } signed main(){ File n=I.read();k=I.read(); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(i>=k&&j>=k) ; else ans=min(ans,dis(i,j)); } } I.write(ans);putchar('\n'); return 0; }
|
题意
(还是自己看┗`O′┛
思路
(留个坑,似乎是个定理题 粘个题解慢慢看: 题解出自这里