思路:贪心先选价格小的巧克力,如果价格一样选保质期大的。维护天数数组,选了巧克力后从保质期最后一天往前食用。
#include
#include
#include
using namespace std;
const int N = 1e5+10;
set<int>se;
int x,n;
struct node{
int val,L,cnt;
bool operator < (const struct node &b)const{
if(val == b.val) return L > b.L;
return val < b.val;
}
}a[N];
int main( ){
cin>>x>>n;
for(int i=1;i<=n;i++)cin>>a[i].val>>a[i].L>>a[i].cnt;
sort(a+1,a+1+n);
for(int i=1;i<=x;i++)se.insert(i);
int p = 1,res=0;
while (se.size()&&p<=n) {
while(se.size()&&a[p].cnt&&*se.begin()<a[p].L ){
res+=a[p].val;
a[p].cnt--;
auto it = se.upper_bound(a[p].L);
it--;
se.erase(it);
}
p++;
}
if(se.size())cout<<-1<<'\n';
cout<<res<<'\n';
return 0;
}
思路:贪心每次选 s+a+e 最小的,能保证总体开始答疑时间和最小
#include
#include
using namespace std;
using ll = long long;
const int N = 1e3+10;
struct node{
int s,a,e;
bool operator <(const struct node &x) const{
return s+a+e < x.s + x.a + x.e;
}
}a[N];
int main( ){
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].s>>a[i].a>>a[i].e;
sort(a+1,a+1+n);
ll res = 0;
for(int i=1;i<=n;i++){
res+= (n + 1 - i)*(a[i].a+a[i].s) + ( n-i )*(a[i].e);
}
cout<<res<<'\n';
return 0;
}
#include
#include
#include
using namespace std;
using ll = long long;
map<pair<int,int>,int>mp;
ll p[40];
ll cala(ll k,ll x,ll y,map<pair<int,int>,int> mp){
ll x_ = x/p[k-1],y_ = y/p[k-1];
if(k==1)return mp[make_pair(x_, y_)]-1;
map<pair<int,int>,int> mp1;
ll ans = (mp[make_pair(x_, y_)]-1)*p[k-1]*p[k-1];
if((x_==0&&y_==1)||(x_==2&&y_==1))
for(int i=0;i<=2;i++)for(int j=0;j<=2;j++)mp1[make_pair(2-i,j)]=mp[make_pair(i,j)];
else if((x_==1&&y_==0)||(x_==1&&y_==2))
for(int i=0;i<=2;i++)for(int j=0;j<=2;j++)mp1[make_pair(i,2-j)]=mp[make_pair(i,j)];
else if(x_==1&&y_==1)
for(int i=0;i<=2;i++)for(int j=0;j<=2;j++)mp1[make_pair(2-i,2-j)]=mp[make_pair(i,j)];
else mp1 = mp;
return ans += cala(k-1,x_%p[k-1],y_%p[k-1],mp1);
}
int main( ){
ll k,x1,x2,y1,y2;cin>>k>>x1>>y1>>x2>>y2;
p[0]=1;
for(int i=1;i<=39;i++)p[i] = p[i-1]*3;
mp[make_pair(0,0)]=1;mp[make_pair(0,1)]=2;mp[make_pair(0,2)]=3;
mp[make_pair(1, 2)]=4;mp[make_pair(1, 1)]=5;mp[make_pair(1, 0)]=6;
mp[make_pair(2, 0)]=7;mp[make_pair(2, 1)]=8;mp[make_pair(2, 2)]=9;
ll a =cala(k,x1,y1,mp);
ll b =cala(k,x2,y2,mp);
cout<<abs(a-b)<<'\n';
return 0;
}