题意 给你n个学生 第 i 个学生会给主角的对手投 a[i]票 问你最小一个m使得 主角获得 m - a[i]票并胜过对手 二分即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 125;
int arr[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,sum = 0,maxx = -1;
scanf("%d",&n);
for(int i = 1;i<=n;++i)
{
scanf("%d",&arr[i]);
maxx = max(maxx,arr[i]);
sum += arr[i];
}
int l = maxx,r = 1000;
while(l<=r)
{
int mid = (l+r)>>1;
int ans = 0;
for(int i =1;i<=n;++i) ans+=(mid-arr[i]);
if(ans>sum) r = mid - 1;
else l = mid + 1;
}
printf("%d\n",l);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
题意 问你n个数他们的差分数组是否满足循环关系
做法 只要用set暴力跑每个可能长度 判断即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 1025;
int arr[MAX_N],a[MAX_N];
set<int > st[MAX_N];
vector<int > ans;
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,cnt;
bool flag = true;
scanf("%d",&n);
for(int i = 1;i<=n;++i)
scanf("%d",&arr[i]);
for(int i = 1;i<=n;++i)
{
bool flag = true;
for(int j = 0;j<n;++j)
st[j].clear();
st[0].insert(arr[1]);
for(int j = 2;j<=n;++j)
{
st[(j-1)%i].insert(arr[j]-arr[j-1]);
}
for(int j = 0;j<i;++j)
{
if(st[j].size()!=1)
{
flag = false;
break;
}
}
if(flag) ans.push_back(i);
}
printf("%d\n",ans.size());
for(int i = 0;i<ans.size();++i)
i==ans.size()-1?printf("%d\n",ans[i]):printf("%d ",ans[i]);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
给你 有一个字符串 你可以操作他的前缀使得他翻转 答案为了让字典序最小
我们有一个原则 最右边的a一定要翻转 然后a之前的b要反转 (为了让a反转到这个a前面
例如 abba 变成 bbaa
根据这个原则去翻转即可解决这题
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 1025;
char str[MAX_N];
int suf,ans[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
scanf("%s",str+1);
int len = strlen(str+1);
for(int i =len;i>=1;--i)
{
if(str[i]=='a')
{
if(suf)
{
ans[i] = 0;
}
else
{
ans[i] = 1;
suf = 1;
}
}
else
{
if(suf)
{
ans[i] = 1;
suf = 0;
}
else
{
ans[i] = 0;
}
}
}
for(int i = 1;i<=len;++i)
i==len?printf("%d\n",ans[i]):printf("%d ",ans[i]);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
给你 m个 n长度的 1 - n串 问你去掉任意一段前缀 任意一段后缀 的子串在m个串都出现过
我们知道 如果第m个串的 [ L, R ] 子串在其他 m-1串中出现过 那么 [ L ,R - 1] , [ L + 1,R ] 都是需要增加的答案 所以我们只要把任意两个相等的答案统计出来 累加即可
pre[i][j] 是 第 i 个串 j 前面的数是多少
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 100025;
int arr[MAX_N],pre[15][MAX_N],cnt[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,m;
scanf("%d%d",&m,&n);
for(int i = 1;i<=n;++i)
{
for(int j = 1;j<=m;++j)
{
scanf("%d",&arr[j]);
pre[i][arr[j]] = arr[j-1];
}
}
ll ans = m;
for(int i = 2;i<=m;++i)
{
int ck = 0;
for(int j = 1;j<n;++j)
{
if(pre[j][arr[i]]==pre[n][arr[i]])
ck++;
}
if(ck==n-1) cnt[i] = cnt[i-1] + 1;
ans+=cnt[i];
}
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
题意 给你n个人的 x 值 和 y 值 定义两个人组队的贡献为 min(x[i] + y[j],y[i] + x[j]) 然后给你m个关系 表示这m对关系不能组队 问你每个人能组队的所有贡献和
我们观察 a 和 b 之间 如果 取a做 x 取 b 做 y 就是要满足 x[a] + y[b] < x[b] + y[a] 那么 就是 x[a] - y[a] < x[b] - y[b] 这个顺序之前的都是 b做x 这个顺序后面就是 a 做x
所以我们排个序 前缀和x 前缀和y 减去不能组队的关系即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 300025;
struct node
{
long long x,y,id;
}arr[MAX_N],arr_[MAX_N];
bool cmp(node a,node b)
{
return a.x - a.y >b.x - b.y;
}
ll sumx[MAX_N],sumy[MAX_N],ans[MAX_N];
vector<int > G[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,m,a,b;
scanf("%d%d",&n,&m);
for(int i = 1;i<=n;++i)
{
scanf("%lld%lld",&arr[i].x,&arr[i].y);
arr[i].id = i;
arr_[i].x = arr[i].x ,arr_[i].y = arr[i].y;
}
while(m--)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
sort(arr+1,arr+1+n,cmp);
for(int i = 1;i<=n;++i) sumx[i] = sumx[i-1] + arr[i].x,sumy[i] = sumy[i-1] + arr[i].y;
for(int i = 1;i<=n;++i)
{
ans[arr[i].id] += sumy[i-1] + (i-1)*arr[i].x + (n-i)*arr[i].y + sumx[n] - sumx[i];
for(int j = 0;j<G[arr[i].id].size();j++)
{
int to = G[arr[i].id][j];
ll minn = min(arr[i].x+arr_[to].y,arr[i].y+arr_[to].x);
ans[arr[i].id] -= minn;
}
}
for(int i = 1;i<=n;++i)
i==n?printf("%lld\n",ans[i]):printf("%lld ",ans[i]);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}