Codeforces Round #493 (Div. 2) (1800分紀念)
阿新 • • 發佈:2019-02-08
A.Balloons
題意:給出長度為n的陣列,問是否可以分成兩組和不一樣的陣列,n<=10
題解:由於n很小,所以列舉子集就好了
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define CLR(a) memset(a, 0, sizeof(a))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, a, b) for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000009 ;
const int N = 1e3 + 10;
int a[N], n, cnt, t, t2;
int sum;
int main() {
cin>>n;
REP(i, 1, n) {
cin>>a[i];
sum+=a[i];
}
REP(i, 1, (1<<n)-2) {
t=0;
cnt=0;
for(int j=0; j<n; j++) {
if ((1<<j)&i) {
t+=a[j+1 ];
cnt++;
}
}
t2=sum-t;
if (t!=t2) {
cout<<cnt<<endl;
for(int j=0; j<n; j++) {
if ((1<<j)&i) {
cout<<j+1<<endl;
return 0;
}
}
}
}
cout<<-1<<endl;
//cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
return 0;
}
B.Cutting
題意:給出一個長度為n的好陣列和一個數B,定義好陣列為偶數個數等於奇數個數的陣列,問最多能切多少刀,切一刀的代價為相鄰兩元素的差的絕對值
題解:偶數看成+1,奇數看成-1,能切的地方為字首和為0的地方,把所有能切的地方的代價排個序就好,從小切到大就好了
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define CLR(a) memset(a, 0, sizeof(a))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, a, b) for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000009;
const int N = 1e3 + 10;
int n, b, a[N];
vector<int> t;
int cnt, ans;
int main() {
cin>>n>>b;
REP(i, 1, n) {
cin>>a[i];
}
REP(i, 1, n) {
if (a[i]%2==0) cnt++;
if (a[i]%2==1) cnt--;
if (cnt==0&&i!=n) {
t.pb(abs(a[i]-a[i+1]));
}
}
sort(t.begin(), t.end());
FOR(i, 0, t.size()) {
int u=t[i];
if (b>=u) {
b-=u;
ans++;
}
else break;
}
cout<<ans<<endl;
//cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
return 0;
}
C.Convert To Ones
題意:給出一個01字串,給出兩種操作和兩種操作的花費,第一種操作reverse一段序列,另一種操作為翻轉一段序列,問全部變成1的最小花費
題解:01序列一般可以壓縮,比如000111001,可以變成0101,簡單算一下樣例,可以注意到答案應該只與0的段數有關係,消除一段0的花費為x,將一段0變為1的花費為y,列舉一下消除多少次就好了
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define CLR(a) memset(a, 0, sizeof(a))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, a, b) for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000009;
const int N = 3e5 + 10;
ll n, x, y, t;
char s[N];
ll ans;
int main() {
cin>>n>>x>>y;
scanf("%s", s+1);
int len=strlen(s+1);
for(int i=1; i<=len; i++) {
if (s[i]=='1') continue;
int j=i;
while(s[j+1]=='0'&&j<=len-1) j++;
t++;
i=j;
}
if (t==0) {
cout<<0<<endl;
return 0;
}
ans=LL_INF;
REP(i, 0, t-1) {
ll tmp=i*x+(t-i)*y;
ans=min(tmp, ans);
}
cout<<ans<<endl;
//cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
return 0;
}
D.Roman Digits
題意:用n位字元可以表示多少個不同數字,每位字元可以表示1,5,10,50其中一個
題解:遇事不決先打表。。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define CLR(a) memset(a, 0, sizeof(a))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, a, b) for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000009;
const int N = 1e3 + 10;
ll n;
ll ans;
set<int> S;
vector<int> V;
int main() {
V.pb(0);
REP(i, 1, 50) {
S.clear();
REP(a, 0, i) {
REP(b, 0, i) {
REP(c, 0, i) {
REP(d, 0, i) {
if ((a+b+c+d)!=i) continue;
S.insert(a+5*b+10*c+50*d);
}
}
}
}
// cout<<S.size()<<endl;
V.pb(S.size());
}
// FOR(i, 0, V.size()) {
// cout<<V[i+1]-V[i]<<endl;
// }
cin>>n;
if (n>20) {
ans=(n-20)*49+V[20];
}
else ans=V[n];
cout<<ans<<endl;
//cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
return 0;
}
迷茫中