题目
B
题意: 给定[l,r]、a,求[l,r]中最大的f(x). f(x) = x/a + x%a.
思路: 结果最大肯定是令t = k*a-1. f(t) = k-1 + a-1. (k是a的倍数,k肯定越大越好,k = r/a.)
如果t不在[l,r]之间,只能说明r < a了,直接取f®就好了。如果a是1的话需要注意一下,直接取f®。
时间复杂度: O(T)
代码:
// Problem: B. DIV + MOD
// Contest: Codeforces - Codeforces Round #776 (Div. 3)
// URL: https://codeforces.com/contest/1650/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
void solve()
{
int l,r,a;
read(l),read(r),read(a);
int k = r/a;
int tmp = k*a-1;
int ans = r/a + r%a;
if(tmp>=l&&tmp<=r) ans = max(ans,k-1+a-1);
write(ans); puts("");
}
signed main(void)
{
// T = 1;
// OldTomato; cin>>T;
read(T);
while(T--)
{
solve();
}
return 0;
}
C
题意: 给定n个点,需要选出m对点,满足权值和最小。而且每两对点之间形成的区间是完全包含的。
思路: 按照权值排序,之后统计权值和。再按坐标排回去,只要每次都取首尾的点即满足题意。
时间复杂度: O(n*logn)
代码:
// Problem: C. Weight of the System of Nested Segments
// Contest: Codeforces - Codeforces Round #776 (Div. 3)
// URL: https://codeforces.com/contest/1650/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
struct node{
int x;
int w;
int id;
}a[N];
bool cmp(node a,node b)
{
return a.w < b.w;
}
bool cmp2(node a,node b)
{
return a.x < b.x;
}
void solve()
{
read(m); read(n);
for(int i=1;i<=n;++i)
{
read(a[i].x),read(a[i].w);
a[i].id = i;
}
ll ans = 0;
sort(a+1,a+n+1,cmp); //按照w从小到大排序
vector<node> va;
for(int i=1;i<=2*m;++i)
{
ans += a[i].w;
va.push_back(a[i]);
}
sort(va.begin(),va.end(),cmp2); //重新按照x轴坐标排序
int i=0,j=va.size()-1;
printf("%lld\n",ans);
while(i<j)
{
printf("%d %d\n",va[i++].id,va[j--].id);
}
puts("");
}
signed main(void)
{
// T = 1;
// OldTomato; cin>>T;
read(T);
while(T--)
{
solve();
}
return 0;
}
D
题意: 给定一个长度为n的permutation,把长度为n、a[i] = i的数组变成他。有n种操作,第i种操作为选择前i个数,循环右移x次。求循环右移次数最少为多少,并输出第i种操作使用了多少次。(n <= 2e3)
思路: 观察样例发现最大的数排完以后就没有影响,可以无视。所以从大到小排,排完一个数实际需要排的数组长度–。第i个数需要移动的次数为: idx[i] % i. idx[i]是需要转变成的对应下标,i是当前实际的数组长度,大的数不考虑了,不可能移动>=i次的。i移动后所有小于i的数也要移,因为他们还没有定位。
时间复杂度: O(n*n)
代码:
// Problem: D. Twist the Permutation
// Contest: Codeforces - Codeforces Round #776 (Div. 3)
// URL: https://codeforces.com/contest/1650/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int a[N];
int ans[N];
void solve()
{
read(n);
for(int i=1;i<=n;++i)
{
int x; read(x);
a[x] = i; //x对应的下标
}
for(int i=n;i>1;--i)
{
ans[i] = a[i] % i;
for(int j=1;j<i;++j)
{
a[j] = (a[j] - ans[i] + i) % i; //只考虑i个数,大于i的数已经定了.
if(a[j] == 0) a[j] = i;
}
}
for(int i=1;i<=n;++i)
{
printf("%d%c",ans[i],i==n?'\n':' ');
}
}
signed main(void)
{
// T = 1;
// OldTomato; cin>>T;
read(T);
while(T--)
{
solve();
}
return 0;
}